#include <iostream> using namespace std; class Cat { public: void GetName(); }; class Dog: public Cat { public: void GetName(); }; void Cat::GetName() { cout << "name\n"; } int main() { Dog MyDog; MyDog.GetName(); return 0; } 

Errors:

ExConstr.obj : error LNK2001: unresolved external symbol "public: void __thiscall Dog::GetName(void)" (?GetName@Dog@@QAEXXZ) Debug/ExConstr.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe.

ExConstr.exe - 2 error(s), 0 warning(s)

What's my mistake?

  • C Builder or Visual? - BlackOverlord
  • Visual ... I don't understand what's going on ... - Xyanight
  • cool, class kopopes - andrybak
  • Do dogs come from cats? cool! - skegg
  • But what the difference is, who descended from whom? This is a naked example. - Xyanight

4 answers 4

The linker cannot find the implementation of Dog::GetName(void) .

Add to code

 void Dog::GetName() { cout << "name\n"; } 
  • This is ridiculous! What then is the meaning of inheritance if I rewrite the member functions that I want to inherit? Rave! - Xyanight
  • In this case, you need to use virtual functions and work through pointers. Get what you want. - fogbit
 #include <iostream> using namespace std; class Cat { public: void GetName(); }; class Dog: public Cat { }; void Cat::GetName() { cout << "Cat name\n"; } int main() { Dog MyDog; MyDog.GetName(); // Cat name //system("pause"); return 0; } 

    And what do you want? You declare a method, but do not describe. Is it possible to write in words what you want? You can add a description of Dog :: GetName, as suggested by @fogbit . The Cat class will have its own GetName method, and the Dog will have its own. You can remove the description public void Dog::GetName(); from the description of the class Dog, and then the method of the parent Cat :: GetName () will be called (by the way, when were dogs born of cats?).

    • Thank you very much, figured out! - Xyanight
    • I put it wrong here. If you add you will have two methods GetName (canine and feline, and both are available for dogs) - alexlz

    The dog did not inherit the GetName () function from Kota, because you redefined it in the Dog class. Now it is completely independent, pesya, and not inherited. However, they have not yet written an implementation. Remove the Dog's GetName () declaration and everything will be fine. If you still want to write something for your Dog, then declare the cat's GetName () virtual.

    • That is, inherited methods are not declared in the class of the heir, I understand correctly? - Xyanight
    • Nearly. If you want a dog to have its own implementation of GetName (), and, at the same time, it would still inherit from Kota, then Kota must have a virtual void GetName (). Then you can declare it to the Dog and write your own implementation. And it can be used including through the pointer to the base class. hashcode.ru/questions/49735/… - BuilderC
    • Those. if you ask a name for a dog that has pretended to be a cat, then it will say its damned name. (In the case of two methods, without virtual, she says the dog's name in dog's guise, and in the cat's cat's name) alexlz