[英]How can I call a constructor in main() c++?
I have two classes. 我有两节课。
fileInfo.cpp: 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 main.cpp中
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]){
fileInfo f("test");
std::cout << f.getName();
}
The fileInfo object "f" is not being initialized and I get an error saying that fileInfo is not in scope. fileInfo对象“ f”未初始化,并且我收到一条错误消息,指出fileInfo不在范围内。 I am using a makefile to compile my code which looks like.
我正在使用一个makefile来编译我的代码,如下所示。
all: main.cpp fileInfo.cpp
g++ main.cpp fileInfo.cpp -o out
Here's the correct way to do it: 这是正确的方法:
fileInfo.h: 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: fileInfo.cpp:
#include "fileInfo.h"
fileInfo::fileInfo(string s){
fileName = s;
fileType = "hellooo";
}
string fileInfo::getName() {
return fileName;
}
main.cpp 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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.