Home c++ Error: error LNK2019: unresolved external symbol

Error: error LNK2019: unresolved external symbol

Author

Date

Category

Good morning, I’m dumb, I can’t figure out what’s wrong, that is, I know in which line, but what exactly and how to fix is ​​not clear, here’s a simplified version of what I need.

class Tree {
public:
  void add_element (Tree);
  vector & lt; Tree & gt; get_vector ();
};
class Directory: public Tree {
private:
  string name;
  vector & lt; Tree & gt; element;
public:
  Directory (string name) {
    this- & gt; name = name;
  }
  void add_element (Tree tree) {
    element.push_back (tree);
  }
};
int main () {
  Tree * root = new Directory ("root");
  Tree * d1 = new Directory ("klk");
  root- & gt; add_element (* d1); // here it gives an error, I will add at the end of the code
}
// error LNK2019: unresolved external symbol "public: void __thiscall Tree :: add_element (class Tree)" (? add_element @ Tree @@ QAEXV1 @@ Z) referenced in function _main

Answer 1, authority 100%

Here is a minimally tweaked version that compiles

# include & lt; iostream & gt;
#include & lt; vector & gt;
using namespace std;
class Tree {
public:
  // must be virtual here because this is an abstract virtual method
  // if this is not the case, the compiler will not be able to choose the correct implementation
  // and will swear that it cannot find the implementation of the method (it's like in your
  // error - the compiler tried to find the implementation of the virtual method)
  virtual void add_element (Tree * tree) = 0;
  virtual vector & lt; Tree * & gt; get_vector () = 0;
};
class Directory: public Tree {
private:
  string name;
  vector & lt; Tree * & gt; element;
public:
  Directory (string name) {
    this- & gt; name = name;
  }
  void add_element (Tree * tree) {
element.push_back (tree);
  }
  vector & lt; Tree * & gt; get_vector () {
    return element;
  }
};
int main () {
  Tree * root = new Directory ("root");
  Tree * d1 = new Directory ("klk");
  root- & gt; add_element (d1);
}

Answer 2, authority 40%

Try changing the linker to put / SUBSYSTEM: CONSOLE instead of / SUBSYSTEM: WINDOWS.

Setting this linker option in the Visual Studio Development Environment
1. Open the Project Property Pages dialog box.
2. Select the Linker folder.
3. Select the System property page.
4. In SubSystem put / SUBSYSTEM: CONSOLE instead of / SUBSYSTEM: WINDOWS.

https://msdn.microsoft.com/en-us/library/799kze2z. aspx

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions