error when compiling - linking .cpp & header file

后端 未结 2 1927
旧时难觅i
旧时难觅i 2021-01-23 08:32

I\'m attempting to link my .cpp implementation file with my header file - I get this error message from my mac terminal -

rowlandev:playground rowlandev$ g++ ma         


        
相关标签:
2条回答
  • 2021-01-23 09:06

    The link error says that the linked cannot find the constructor. The constructor is not in main.cpp, it is in your other file, which is unnamed in your example. Try putting all of it in a single cpp file to get it working.

    0 讨论(0)
  • 2021-01-23 09:17

    Here's a good read to understand what's going on when you try to create an executable from source code: How does the compilation/linking process work?

    What's going on here is that the linker doesn't know where Person::Person(), which is being called in main(), is located. Notice how when you called g++, you never gave it the file where you wrote code for Person::Person().

    The right way to call g++ is: $ g++ -o main main.cpp person.cpp

    0 讨论(0)
提交回复
热议问题