我有两个类。
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
以下是正确的方法:
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();
}