我不明白尝试使用字符串作为函数参数时遇到的错误

I do not understand the error I am getting when trying to use a string as a function parameter

本文关键字:参数 函数 遇到 错误 字符串 明白      更新时间:2023-10-16
#ifndef MENU_H
#define MENU_H
#include <cstring>
#include <string>
#include <cctype>
class Menu{
public:
Menu();
void run();
int selectOptions();
void rotate90();
void rotate180();
void rotate270();
void flipVert();
void flipHoriz();
void convertHigh(int threshold);
void saveImage(string saveName);
void loadImage(string loadName);
void displayMenu();
private:
}
#endif // MENU_H
C:UsersWattenphulDocumentsalg-prog designproject04>g++ -std=c++11 -o main.exe main.cpp menu.cpp
In file included from menu.cpp:6:
menu.h:22:20: error: 'string' has not been declared
void saveImage(string saveName);
^~~~~~
menu.h:22:35: error: expected ',' or '...' before 'saveName'
void saveImage(string saveName);
^~~~~~~~
menu.h:23:20: error: 'string' has not been declared
void loadImage(string loadName);
^~~~~~
menu.h:23:35: error: expected ',' or '...' before 'loadName'
void loadImage(string loadName);
^~~~~~~~
menu.h:28:2: error: expected ';' after class definition
}

这是编译的一部分后跟的代码。它只是头文件,其余代码尚未实现。我不明白为什么字符串不起作用。我也尝试使用分辨率运算符,但这并没有改变结果。

它不叫string。它被称为std::string

您可能已经看到一些称为string的例子。那是因为作者在某个地方走了捷径和错误的using namespace std,尽管我们不一定建议这样做。

字符串是在namespace std内部定义的,为了使用你需要用std前缀,所以使用std::string而不仅仅是string。或者,您可以避免使用 using 指令或 using-声明为其添加前缀

要么使用 std::string 而不是 string,要么在类外部打开命名空间"使用命名空间 std;" 但作为良好的编码实践,至少在 .h 文件中打开命名空间不是首选。