无法将字符串用作另一个类的参数

Unable to use a string as a parameter on an another class

本文关键字:另一个 参数 字符串      更新时间:2023-10-16

我在cpp中创建了一个简单的函数来打印文本。代码如下:

#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally()
{
}
void Sally::tell(string t){
cout << t << endl;
}

以下是我在 .h 文件上对其进行原型设计的方式:

#ifndef SALLY_H
#define SALLY_H
class Sally
{
public:
Sally();
void tell(string t);
protected:
private:
};
#endif // SALLY_H

以下是错误:

Sally.h|8|error: 'string' has not been declared|
Sally::tell(std::__cxx11::string)' does not match any in class 'Sally'|
Sally.h|8|error: candidate is: void Sally::tell(int)|

每当我构建它时,它都会给我一个错误。如果我使用 int 作为参数,它根本没有任何问题。所以问题是我希望使用字符串作为参数。

对.h文件进行一些更改:

#include <string>

并在字符串前面添加std::

void tell(std::string t);