只是一个简单的问题:是否可以在另一个 .cpp 文件中使用一个 .cpp 文件 - 比如调用它。
例如 文件 1.cpp:
#include < iostream >
#include < string >
using namespace std;
void method(string s);
int main()
{
method("Hello World");
return 0;
}
void method(string s)
{
//get this contents from file2.cpp
}
和 File2.cpp:
#include <iostream>
using namespace std;
void method(string s)
{
cout << s << endl;
}
因此能够按照该思路做一些事情。 所以我不会将我所有的代码都塞进 1 个 cpp 文件中
谢谢。
最佳答案
你需要制作一个头文件;例如 File2.h,您可以在其中放置要重用的每个函数的原型(prototype):
#ifndef FILE2_H_
#define FILE2_H_
void method(string s);
#endif /* FILE2_H_ */
然后您需要在 File2.cpp 和 File1.cpp 中包含此 header :
#include "File2.h"
现在在 File1.cpp 中你可以调用这个函数而不用声明它:
int main()
{
method("Hello World");
return 0;
}
关于C++ - 在另一个中使用 .cpp 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23086945/