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

class Tree{ public: void add_element(Tree); vector<Tree> get_vector(); }; class Directory: public Tree{ private: string name; vector<Tree> element; public: Directory(string name){ this->name = name; } void add_element(Tree tree){ element.push_back(tree); } }; int main(){ Tree *root = new Directory("root"); Tree *d1 = new Directory("klk"); root->add_element(*d1);// тут он дает ошибку, добавлю в конце кода } //error LNK2019: unresolved external symbol "public: void __thiscall Tree::add_element(class Tree)" (?add_element@Tree@@QAEXV1@ @Z ) referenced in function _main 

Tried to wrap in shared_ptr, ..., tell me where I’m dumb, thanks. Maybe I'm doing something wrong with the initialization of the vector? ..., thank you for your attention.

    2 answers 2

    Here is a minimally reworked version that compiles

     #include <iostream> #include <vector> using namespace std; class Tree{ public: // здесь должно быть virtual, потому что здесь абстрактный виртуальный метод // если этого не будет, компилятор не сможет выбрать правильную реализацию // и будет ругаться, что не может найти реализацию метода (это как в Вашей // ошибке - компилятор пытался найти реализацию виртуального метода) virtual void add_element(Tree* tree) = 0; virtual vector<Tree*> get_vector() = 0; }; class Directory: public Tree{ private: string name; vector<Tree*> element; public: Directory(string name){ this->name = name; } void add_element(Tree* tree){ element.push_back(tree); } vector<Tree*> get_vector() { return element; } }; int main(){ Tree *root = new Directory("root"); Tree *d1 = new Directory("klk"); root->add_element(d1); } 
    • I tried, I get a curse to transfer the object to the function, apparently only the pointer can be passed there - perfect
    • Apparently you have a bigger example :) My version is compiled? - KoVadim
    • no removed all the excess left only this function and her body was cleaned - perfect
    • your compile will understand - perfect
    • four
      If the function is not "completely virtual", then it must have an implementation. You do not have it, so the linker (not the compiler) also swears - he cannot find it. - KoVadim

    Try to change the linker, 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 properties page.
    4. In the SubSystem put / SUBSYSTEM: CONSOLE instead of / SUBSYSTEM: WINDOWS.

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