0

I have a class named Foo with static deque of pointers:

Foo.h:

class Foo : public Base
{
public:
    static std::deque <Aircraft*> aircrafts;
    void updateAircrafts();
...
}

Foo.cpp:

#include "Foo.h"
void Foo::updateAircrafts()
{
    for (int i = 0; i < Foo::aircrafts.size(); i++) {
    ...
    }
}

When I'm compiling, I get the following error:

Error LNK2001 unresolved external symbol "private: static class std::deque > Foo::aircrafts" (?aircrafts@Foo@@0V?$deque@PEAVAircraft@@V?$allocator@PEAVAircraft@@@std@@@std@@A) QtGuiApplication1 C:\Users\a_kadirov\source\repos\QtGuiApplication1\Foo.obj 1

and

Error LNK1120 1 unresolved externals QtGuiApplication1 C:\Users\a_kadirov\source\repos\QtGuiApplication1\x64\Debug\QtGuiApplication1.exe 1 Where could be the problem?

I also need to access deque from outside the class, so it must be accessible to other classes. So moving static deque declaration to .cpp file may not work.

1 Answers1

0

If you're building C++17 or above then you could use the inline specifier :

static inline std::deque <Aircraft*> aircrafts;

Otherwise, you can define aircrafts in Foo.cpp :

std::deque <Aircraft*> Foo::aircrafts;
void Foo::updateAircrafts()
{
    for (int i = 0; i < Foo::aircrafts.size(); i++) {

    }
}

Note that the above still requires a declaration in the header. And aircrafts should only be defined once, other files will be able to link it.

George
  • 2,101
  • 1
  • 13
  • 27
  • Thanks! defining deque in cpp file worked! Before, I didn't know how to declare deque in header and define it in cpp, now it's clear! – Azizbek Kadirov Nov 11 '19 at 10:48