I'm trying to declare a vector as a global variable, and am getting a linker error. I read up on linker problems with multi-file projects, pretty sure I've included the proper headers, used extern, and #ifdef properly, but I can't get it yet. Here is what we have:

//in customclass.h
#ifndef _customclass
#define _customclass

class customclass {
private: int a;
customclass() : a(0) {}

//in globals.h
#ifndef _globals
#define _globals
#include <vector>
#include "customclass.h"

extern std::vector <customclass> vectA;


//in globals.cpp
#include <vector>
#include "globals.h"
#include "customclass.h"
using namespace std;

vector <customclass> vectA(0);

//in main.cpp
#include "customclass.h"
#include "globals.h"
#include <vector>
using namespace std;
cout << vectA.size() << endl;

Linker error cites unresolved symbol for vector customclass.
and the error goes away when I make it a vector of double or int.
Any ideas???

I don't believe that you can call VectA.reserve() out in the middle of nowhere like that. I'm assuming this is a paraphrase of your code. Moving the reserve call to main() (after creating a main) I was able to get it to compile and give a size of 0.

Edit: Also make sure if you are compiling on the command line that you present them in the order globals.cpp and then main.cpp on the list of files.

Thanks for your help,
After double checking everything again and moving VectA.reserve(), I was still getting the same error.
I looked closer and found I added inline to customclass constructor, which was actually in customclass.cpp (You're right, its paraphrased).
delete inline, and the problem goes away, not sure why...but it does.


This question has already been answered. Start a new discussion instead.