【发布时间】:2015-10-18 17:22:41
【问题描述】:
我只是想测试将代码拆分成多个文件。
我有:
//testMultiple.cpp
#include <string>
#include <iostream>
#include "testInclude.cpp"
int main(){
std::cout << "hi";
}
和
//testInclude.cpp
class testClass{
public:
string x;
};
这是给testInclude.cpp:3:9: error: ‘string’ does not name a type
我想既然它在包含 testInclude.cpp 之前就包含了,那么将定义字符串以在 testInclude.cpp 中使用。
【问题讨论】:
-
为什么要包含
.cpp文件,而不是将其与testMultiple.cpp链接?这是非常不寻常的,并且会破坏大多数构建系统和 IDE。 -
因为据我所知,如果你想把你的程序分成多个文件,你
#include他们 -
不,那是错误的。您将它们拆分为包含类声明的头文件和包含相应定义的
.cpp文件。其他翻译单元(.cpp文件)使用#include从标题中获取声明。 -
@DavidBandel 或许你应该先看几个问题,比如this one。
-
@DavidBandel _"我不知道这是一个无用的包含,字符串实际上是在 iostream 中定义的。"_不,你不能依赖这个。如果您使用
std::string,请使用#include <string>。
标签: c++ dependencies header-files