Cal*_*Cal 32 c++ class header-files visual-studio-2010
我在写作课时遇到问题.我已将类拆分为定义类的.h文件和实现该类的.cpp文件.
我在Visual Studio 2010 Express中收到此错误:
错误C2039:'string':不是'std'的成员
这是标题FMAT.h
class string;
class FMAT {
public:
FMAT();
~FMAT();
int session();
private:
int manualSession();
int autoSession();
int mode;
std::string instructionFile;
};
Run Code Online (Sandbox Code Playgroud)
这是实现文件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;
}
Run Code Online (Sandbox Code Playgroud)
这是使用此类的主文件
#include "FMAT.h"
int main(void)
{
FMAT fmat; //create instance of FMAT class
fmat.session(); //this will branch to auto session or manual session
}
Run Code Online (Sandbox Code Playgroud)
我无法修复此错误可能是因为我无法理解如何将类正确地构建为单独的文件.随意提供有关如何在c ++程序中处理多个文件的一些提示.
shu*_*e87 27
你需要拥有
#include <string>
Run Code Online (Sandbox Code Playgroud)
在头文件中也是如此.它自己的前向声明做得不够.
还要强烈考虑头文件的头文件,以避免在项目增长时可能出现的问题.所以在顶部做类似的事情:
#ifndef THE_FILE_NAME_H
#define THE_FILE_NAME_H
/* header goes in here */
#endif
Run Code Online (Sandbox Code Playgroud)
这将防止头文件被多次#included,如果你没有这样的后卫,那么你可能会遇到多个声明的问题.
Mar*_*som 26
您的FMAT.h需要std :: string的定义才能完成FMAT类的定义.在FMAT.cpp中,您之前已经完成了这项#include <string>
工作#include "FMAT.h"
.您还没有在主文件中完成此操作.
您string
在两个级别上转发声明的尝试不正确.首先,您需要一个完全限定的名称std::string
.其次,这仅适用于指针和引用,而不适用于声明类型的变量; 前向声明不会给编译器足够的信息,说明要在您定义的类中嵌入什么.
小智 11
注意不要包括
#include <string.h>
Run Code Online (Sandbox Code Playgroud)
但只有
#include <string>
Run Code Online (Sandbox Code Playgroud)
我花了 1 小时才在我的代码中找到它。
希望这可以帮助
归档时间: |
|
查看次数: |
109272 次 |
最近记录: |