我通过编写小程序是 C++。我也是处理多个文件的重点。我一直坚持使用另一个文件中的类。我做了一个简单的测试项目来演示我的问题。我有3个文件。
测试头文件
#ifndef __testheader_H_INCLUDED__ // if Node.h hasn't been included yet...
#define __testheader_H_INCLUDED__ // #define this so the compiler knows it has been included
#include <string>
#include <iostream>
class testheader {
public:
testheader(std::string name){}
void write(){}
};
#endif
Run Code Online (Sandbox Code Playgroud)
测试头文件
#include <string>
#include <iostream>
using namespace std;
class testheader {
public:
testheader(string name){
cout << name << endl;
}
void write(){
cout << "stuff" << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
另一个文件.cpp
#include <iostream>
#include "testheader.h"
using namespace std;
int main () { …Run Code Online (Sandbox Code Playgroud)