我不理解尝试将字符串用作函数参数时遇到的错误

问题描述 投票:2回答:3
    #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:\Users\Wattenphul\Documents\alg-prog design\project04>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

 }

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

c++ string compiler-errors g++
3个回答
2
投票

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

[您可能已经看到一些示例,其中将其称为string。这是因为作者虽然在using namespace std的某个地方走了一条捷径且错误的we don't necessarily recommend doing that


0
投票

string是在namespace std内部定义的,要使用该字符串,需要在其前面加上std作为前缀,因此请使用std::string而不是仅使用string。另外,您可以避免使用using-directive or using-declaration

作为前缀

0
投票

要么使用std :: string代替字符串,要么使用外部类打开的命名空间“使用命名空间std;”但是,按照良好的编码习惯,最好不要至少在.h文件中打开名称空间。

© www.soinside.com 2019 - 2024. All rights reserved.