1
// Main.cpp
#include <iostream>
#include "common.h"
#include "second.cpp"
#include <vector>
int main(){
global = 10;
ip.push_back("TestTest");
std::cout << global << std::endl;
TestClass t;
t.print();
}
//common.h
#ifndef GLOBAL_H
#define GLOBAL_H
#include <vector>
#include <string>
extern int global;
extern std::vector<std::string> ip ;
#endif
// second.cpp
#include <iostream>
#include "common.h"
int global;
class TestClass{
public:
void print();};
void TestClass::print(){
global++;
std::cout << "Global: "<<global << std::endl;
std::cout << "IP String: "<<ip[0] << std::endl;
}
// Console Error
ubuntu:deleteme$ g++ main.cpp
/tmp/ccoJpYRl.o: In function `TestClass::print()':
main.cpp:(.text+0x55): undefined reference to `ip'
/tmp/ccoJpYRl.o: In function `main':
main.cpp:(.text+0xdd): undefined reference to `ip'
collect2: error: ld returned 1 exit status
上面的作品当我刚刚使用int global
变量时。但是,当我添加一个向量ip
common.h
我得到显示错误。当在多个文件中使用时包含全局向量的错误
这似乎是一个基本的东西,但无法得到答案。
感谢提前:)
啊!笨。谢谢。我爱Stackoverflow!在不到5分钟的时间内得到答案。 – 2013-04-18 05:11:49