包含包含文件的文件时出现问题

0 c++

我一直在尝试将文件包含在包含的文件中,例如

main.cpp文件

#include <includedfile.cpp>
int main(){
     cout<<name<<endl;
}
Run Code Online (Sandbox Code Playgroud)

includedfile.cpp

#include <iostream>
using namespace std;
string name;
name = "jim";
Run Code Online (Sandbox Code Playgroud)

这个代码不起作用,debuger说名字没有定义.

San*_*der 8

你不能在方法之外存在语句!

name = "jim"; // This is outside of any method, so it is an error.
Run Code Online (Sandbox Code Playgroud)

你可以重构你的代码,所以变量声明也是一个初始赋值,它应该是有效的(我的C++有点生疏,所以我可能在这一点上错了).

string name = "jim";
Run Code Online (Sandbox Code Playgroud)