我不明白尝试使用字符串作为函数参数时遇到的错误
I do not understand the error I am getting when trying to use a string as a function parameter
#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 文件中打开命名空间不是首选。
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类