C++ 错误 C2039:“string”:不是“std”的成员,头文件问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3121825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error C2039: 'string' : is not a member of 'std', header file problem
提问by Cal
I am having problems with a class I am writing. I have split the class into a .h file that defines the class and an .cpp file that implements the class.
我正在写的课程有问题。我已将类拆分为定义类的 .h 文件和实现类的 .cpp 文件。
I receive this error in Visual Studio 2010 Express:
我在 Visual Studio 2010 Express 中收到此错误:
error C2039: 'string' : is not a member of 'std'
错误 C2039:“字符串”:不是“标准”的成员
This is the header FMAT.h
这是标题 FMAT.h
class string;
class FMAT {
public:
FMAT();
~FMAT();
int session();
private:
int manualSession();
int autoSession();
int mode;
std::string instructionFile;
};
This is the implementation file FMAT.cpp
这是实现文件 FMAT.cpp
#include <iostream>
#include <string>
#include "FMAT.h"
FMAT::FMAT(){
std::cout << "manually (1) or instruction file (2)\n\n";
std::cin >> mode;
if(mode == 2){
std::cout << "Enter full path name of instruction file\n\n";
std::cin >> instructionFile;
}
}
int FMAT::session(){
if(mode==1){
manualSession();
}else if(mode == 2){
autoSession();
}
return 1;
}
int FMAT::manualSession(){
//more code
return 0;
}
this is the main file that uses this class
这是使用这个类的主文件
#include "FMAT.h"
int main(void)
{
FMAT fmat; //create instance of FMAT class
fmat.session(); //this will branch to auto session or manual session
}
My inability to fix this error is probably a result of me not understanding how to properly structure a class into separate files. Feel free to provide some tips on how to handle multiple files in a c++ program.
我无法修复此错误可能是因为我不了解如何将类正确构建到单独的文件中。请随意提供一些有关如何在 C++ 程序中处理多个文件的提示。
回答by shuttle87
You need to have
你需要有
#include <string>
in the header file too.The forward declaration on it's own doesn't do enough.
在头文件中也是如此。它自己的前向声明还不够。
Also strongly consider header guards for your header files to avoid possible future problems as your project grows. So at the top do something like:
还要强烈考虑头文件的头保护,以避免随着项目的增长而可能出现的未来问题。因此,在顶部执行以下操作:
#ifndef THE_FILE_NAME_H
#define THE_FILE_NAME_H
/* header goes in here */
#endif
This will prevent the header file from being #included multiple times, if you don't have such a guard then you can have issues with multiple declarations.
这将防止头文件被多次#include,如果您没有这样的保护,那么您可能会遇到多个声明的问题。
回答by Mark Ransom
Your FMAT.h requires a definition of std::string in order to complete the definition of class FMAT. In FMAT.cpp, you've done this by #include <string>
before #include "FMAT.h"
. You haven't done that in your main file.
您的 FMAT.h 需要 std::string 的定义才能完成类 FMAT 的定义。在FMAT.cpp,你通过这样做#include <string>
之前#include "FMAT.h"
。你还没有在你的主文件中做到这一点。
Your attempt to forward declare string
was incorrect on two levels. First you need a fully qualified name, std::string
. Second this works only for pointers and references, not for variables of the declared type; a forward declaration doesn't give the compiler enough information about what to embed in the class you're defining.
您尝试转发声明string
在两个层面上是不正确的。首先,您需要一个完全限定的名称,std::string
. 其次,这只适用于指针和引用,不适用于声明类型的变量;前向声明没有为编译器提供足够的信息,说明要在您定义的类中嵌入什么。
回答by Stephane
Take care not to include
注意不要包括
#include <string.h>
but only
但只有
#include <string>
It took me 1 hour to find this in my code.
我花了 1 小时才在我的代码中找到它。
Hope this can help
希望这可以帮助