Errors by access on extern declared vector

Dear Community,
I have a Problem to get access to a extern Variable.

This is my Situation:
I'm defined a vector about a self written class:

#ifndef Data
#define Data
...
typedef std::vector<TrackParamDat> TPDataTy;
#endif
extern TPDataTy TPData;

A would build the Object like:

TPData = TPDataTy();

But when ever I try this, I get a Error Message like this:

/usr/bin/ld: /tmp/ccsgNs1i.o: in function `MyFrame::MyFrame(wxString const&, wxPoint const&, wxSize const&)':
test.cpp:(.text+0x5b9): undefined reference to `TPData'
/usr/bin/ld: /tmp/ccDZ4gpU.o: in function `TrackControl::TrackControl(wxPanel*, int)':
TrackControl.cpp:(.text+0x9e0): undefined reference to `TPData'
/usr/bin/ld: TrackControl.cpp:(.text+0xa1a): undefined reference to `TPData'
collect2: error: ld returned 1 exit status
Declaring a variable as extern does not allocate space for it.
You still have to declare the variable in exactly one translation unit.
e.g. TPDataTy TPData;
Ok,
but:
1. It is allowed to make a vector / this vector extern
2. It is correct to define "extern TPDataTy TPData" in the header File.
3. I should only give in one .cpp File the Command "TPDataTy TPData = TPDataTy();"

Have I understand this correctly
Yes to all.

See also, the second half of this post:
http://www.cplusplus.com/forum/beginner/263053/#msg1135535


1
2
// header.hpp
extern int* global_var;


1
2
3
4
// bar.cpp
#include "header.hpp"

void bar() { *global_var = 42; }


1
2
3
4
5
6
// foo.cpp
#include "header.hpp"

int* global_var = nullptr;

int main() {  }
Last edited on
Must it be a pointer, or can I define a vector in natural
It was a pointer in the example I gave to a previous thread, but the type doesn't matter (as long as you're consistent). Yes, it can be a vector.
Last edited on
Oh dear,
I'm too rubbisch.
The difficult things are all clear to this point.
But the simplest thing you can imagine are my fatal error.
I must define the variable once in a *.cpp File but out of any method, and not in main().

So pleace hunt me not to hell ... but I'm so stuppid.

A stuppid
Mungo1981
Topic archived. No new replies allowed.