我一直在尝试在包含的文件中包含一个文件,例如
main.cpp 文件
#include <includedfile.cpp>
int main(){
cout<<name<<endl;
}
includedfile.cpp
#include <iostream>
using namespace std;
string name;
name = "jim";
此代码不起作用,调试器说名称未定义。
最佳答案
不能在方法之外存在语句!
name = "jim"; // This is outside of any method, so it is an error.
你可以重构你的代码,这样变量声明也是一个初始赋值,这应该是有效的(我的 C++ 有点生疏,所以我在这一点上可能是错的)。
string name = "jim";
关于c++ - 包含包含文件的文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/268069/