0

So my goal is to make a constructor which makes the object and then puts it in a static list which stores pointers on that objects.

This is my default constructor in cpp file:

Vozac::Vozac()
{
  imevoz=string("");
  brzinavoz=0;
  polozaj=0;
  put=0;
  stanje=0;
  vrsta=0;
  imunost=0;
  vozaci.push_back(this);
}

This is the regular constructor in cpp file:

Vozac::Vozac(string naziv,int x)
{
  imevoz=naziv;
  brzinavoz=x;
  polozaj=0;
  put=0;
  stanje=1;
  vrsta=0;
  imunost=0;
  vozaci.push_back(this);
}

And this is how I defined class Vozac in h file:

class Vozac{
public:
  string imevoz;
  int brzinavoz;
  int polozaj;
  int put;
  int stanje;
  int vrsta;
  int imunost;
  static list<Vozac*> vozaci;
  Vozac();
  Vozac(string naziv,int x);
};

The code compiles alright on its own, but when i try to compile it and link it to one main I get this message:

/tmp/ccT2pGRj.o: In function `Vozac::Vozac()':
vozac.cpp:(.text+0xca): undefined reference to `Vozac::vozaci[abi:cxx11]'
/tmp/ccT2pGRj.o: In function `Vozac::Vozac(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
vozac.cpp:(.text+0x1b1): undefined reference to `Vozac::vozaci[abi:cxx11]'

I am not really sure what does that message mean because as I said the cpp file compiles and the main also compiles on its own. My guess is that I can't use "this" in this situation or did I maybe do something wrong while making the list?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Nomite
  • 1
  • 1
  • Your class definition does not contain any constructor declarations – UnholySheep May 28 '20 at 19:46
  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – UnholySheep May 28 '20 at 19:48
  • "This is the regular constructor" - Why is it assigning all the members in the constructor body after they have all been default initialized? Why doesn't it do actual *initialization*? As in `Vozac::Vozac(string naziv,int x) : imevoz(naziv) ...` ? – Jesper Juhl May 28 '20 at 19:50
  • The problem is that while you have a declaration for `vozaci` in the header, you don't have a _defnition_ of it in a source file. Just adding `list vozaci;` into vozac.cpp should take care of it. – 1201ProgramAlarm May 29 '20 at 01:11
  • Thank you!! I added the definition in a source file and it works now. – Nomite May 29 '20 at 21:50

0 Answers0