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;
public:
customclass() : a(0) {}
};
#endif



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

extern std::vector <customclass> vectA;

#endif

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

vector <customclass> vectA(0);
vectA.reserve(30);

//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???
Thanks!

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.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.