错误 C2039:“字符串” : 不是'std'的成员,头文件问题
我正在编写的课程遇到问题。我已将类拆分为定义该类的 .h 文件和实现该类的 .cpp 文件。
我在 Visual Studio 2010 Express 中收到此错误:
error C2039: 'string' : is not a member of 'std'
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
#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
}
My无法修复此错误可能是因为我不了解如何将类正确构建到单独的文件中。请随意提供一些有关如何在 C++ 程序中处理多个文件的提示。
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.
I receive this error in Visual Studio 2010 Express:
error C2039: 'string' : is not a member of 'std'
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;
};
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;
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意不要包含
而仅
包含我花了 1 个小时才在我的代码中找到它。
希望这可以帮助
Take care not to include
but only
It took me 1 hour to find this in my code.
Hope this can help
您也需要
在头文件中包含此内容。仅靠前向声明还不够。
还要强烈考虑为您的头文件提供头保护,以避免随着项目的增长而出现未来可能出现的问题。所以在顶部做一些类似的事情:
这将防止头文件被 #included 多次,如果你没有这样的保护,那么你可能会遇到多个声明的问题。
You need to have
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:
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.
您的 FMAT.h 需要 std::string 的定义才能完成 FMAT 类的定义。在 FMAT.cpp 中,您已在
#include "FMAT.h"
之前通过#include
完成了此操作。您还没有在主文件中这样做。您尝试转发声明
string
在两个层面上都是不正确的。首先,您需要一个完全限定的名称,std::string
。其次,这只适用于指针和引用,不适用于声明类型的变量;前向声明没有为编译器提供足够的信息来说明要在您定义的类中嵌入什么内容。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.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.