// 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
, я получаю показанную ошибку.Ошибка при включении глобального вектора при использовании в нескольких файлах
Это похоже на элементарную вещь, но не получило ответа.
Заранее спасибо :)
ahh !! Глупый. Благодарю. Мне нравится Stackoverflow! Получил ответ менее чем за 5 минут. –