将向量的字符串定义为全局 [英] Defining the Vectors for strings as global
问题描述
我在x.cpp文件中将容器定义为
Hi,
I am defining a container in a x.cpp file as
vector<string> somevariable;
我正在尝试在另一个y.cpp文件中使用它.我在y.cpp文件中将向量声明为extern,如
I am trying to use it in another y.cpp file. I declare the vector as extern in y.cpp file like
extern vector<string> somevariable;
但是,当我编译程序时,出现如下错误:
1)somevariable:找不到标识符;甚至具有依赖于参数的查找
2)错误C2143:语法错误:在``<''
之前缺少``;''
我在文件中包括了<string>, <vector>
,还包括了using namespace std;
我缺少什么?
But when I compile the program I get the errors like:
1) somevariable : identifier not found; even with argument dependent lookup
2) error C2143: syntax error : missing '';'' before ''<''
I have included <string>, <vector>
in my files and also using namespace std;
what am I missing?
推荐答案
您是否在项目中包含了x.cpp
文件?
您是否在y.cpp
和x.cpp
源文件中都包含了必需的标头?
以下测试:
文件b.cpp
Did you includex.cpp
file in your project?
Did you include the required headers in bothy.cpp
andx.cpp
source files?
The following test:
fileb.cpp
#include <vector>
#include <string>
using namespace std;
vector <string> v;
文件main.cpp
file main.cpp
#include <vector>
#include <string>
#include <iostream>
using namespace std;
extern vector<string> v;
int main()
{
v.push_back(string("hi"));
cout << v[0] << endl;
}
可以编译(运行).
:)
compiles (and runs) fine.
:)
我认为您需要声明它extern
并在x.cpp中对其进行初始化.请参阅 MSDN参考 [
I think you need to declare itextern
and initialise it in x.cpp. See the MSDN reference[^] for further information.
I stand corrected; Signor Pallini has the answer.
这篇关于将向量的字符串定义为全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!