我有两节课。
fileInfo.cpp:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class fileInfo{
private:
string fileName;
string fileType;
public:
/**
**/
fileInfo(string s){
fileName = s;
fileType = "hellooo";
}
string getName() {
return fileName;
}
};
的main.cpp
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]){
fileInfo f("test");
std::cout << f.getName();
}
fileInfo对象“f”未初始化,我收到一条错误消息,指出fileInfo不在范围内。我正在使用makefile编译我的代码,看起来像。
all: main.cpp fileInfo.cpp
g++ main.cpp fileInfo.cpp -o out
答案 0 :(得分:1)
这是正确的方法:
fileInfo.h:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class fileInfo{
private:
string fileName;
string fileType;
public:
fileInfo(string s);
string getName();
};
fileInfo.cpp:
#include "fileInfo.h"
fileInfo::fileInfo(string s){
fileName = s;
fileType = "hellooo";
}
string fileInfo::getName() {
return fileName;
}
的main.cpp
#include <iostream>
#include <string>
#include "fileInfo.h"
using namespace std;
int main(int argc, char* argv[]){
fileInfo f("test");
std::cout << f.getName();
}