错误C2039:“字符串”:不是“ std”的成员,头文件问题 [英] error C2039: 'string' : is not a member of 'std', header file problem
问题描述
我正在编写的课程遇到问题。我将类拆分为定义该类的.h文件和实现该类的.cpp文件。
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.
在Visual Studio 2010 Express中收到此错误:
I receive this error in Visual Studio 2010 Express:
错误C2039:'string':不是'std'的成员
error C2039: 'string' : is not a member of 'std'
这是标头FMAT。 h
This is the header FMAT.h
class string;
class FMAT {
public:
FMAT();
~FMAT();
int session();
private:
int manualSession();
int autoSession();
int mode;
std::string instructionFile;
};
这是实现文件FMAT.cpp
This is the implementation file 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;
}
这是使用此类的主文件
#include "FMAT.h"
int main(void)
{
FMAT fmat; //create instance of FMAT class
fmat.session(); //this will branch to auto session or manual session
}
我的无法解决此错误,可能是由于我不了解如何正确地将一个类构造为单独的文件而导致的。随时提供有关如何在c ++程序中处理多个文件的提示。
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.
推荐答案
您需要拥有
#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
这将防止头文件不会被#include多次,如果您没有这样的保护,那么您可能会遇到多个声明问题。
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.
这篇关于错误C2039:“字符串”:不是“ std”的成员,头文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!