• Linking error with class pointer
    15 replies, posted
I'm trying to teach myself pointers, and I keep getting linking errors. I'm not good at programming, so this confuses me. Messages: [quote]1>Linking... 1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall test::test(void)" (??0test@@QAE@XZ) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall test::~test(void)" (??1test@@QAE@XZ) referenced in function "public: void * __thiscall test::`scalar deleting destructor'(unsigned int)" (??_Gtest@@QAEPAXI@Z)[/quote] main.cpp [cpp] #include <iostream> #include "he.h" int main() { test* xptr = new test(); xptr->x = 123; std::cout << xptr->x; delete xptr; } [/cpp] he.h [cpp] class test { public: test(); int x; ~test(); }; [/cpp]
You're declaring a default constructor and destructor but never defining them. If you don't want to use them, take out the declarations altogether, and it won't error. You might want to consider something like this: main.cpp [cpp] #include <iostream> #include "he.hpp" int main() { test* xptr = new test(123); std::cout << xptr->x; delete xptr; return 0; } [/cpp] he.hpp [cpp] #ifndef HE_HPP #define HE_HPP class test { public: test(int x); int x; }; #endif [/cpp] he.cpp [cpp] #include "he.hpp" test::test(int x) { this->x = x; } [/cpp] edit: Added include guards. So much for copy-paste </3
You need to implement that constructor and destructor. The class definition needs an implementation.
-snip- Nevermind, probable solution has been posted. :v:
[QUOTE=Siduron;16705251]I see 2 possible causes at the moment. -Is the header you're including the correct one? -Are the 2 files in different projects? If so, right click on the project of Main and click project dependencies.[/QUOTE] I'm sorry if this offends you, but please, if you don't know what you're talking about don't try and help. ( You don't know what you're talking about )
[QUOTE=blankthemuffin;16705276]I'm sorry if this offends you, but please, if you don't know what you're talking about don't try and help. ( You don't know what you're talking about )[/QUOTE] Wow fuck, who's offended here? It's late and i just thought of stuff that caused linker errors for me in the past.
[QUOTE=Siduron;16705321]Wow fuck, who's offended here? It's late and i just thought of stuff that caused linker errors for me in the past.[/QUOTE] You obviously don't understand what a linker error is, neither of the possible solutions you posted could cause linker errors. Compiler errors yes, linker errors no.
I'm sorry not everyone here lives up to your godly programming standards.
Still a programming newbie, what does the "this" pointer do?
[QUOTE=Siduron;16705398]I'm sorry not everyone here lives up to your godly programming standards.[/QUOTE] Don't worry about it, blankthemuffin is a horrible programmer and he knows it ^^v [QUOTE=raccoon12;16705462]Still a programming newbie, what does the "this" pointer do?[/QUOTE] "this" is a pointer to the current instance of the class that the function is being run with. In this case, it points to the same location as the pointer you define in main. (because you only ever have one instance of "test")
[QUOTE=jA_cOp;16705471]Don't worry about it, blankthemuffin is a horrible programmer and he knows it ^^v[/QUOTE] I can talk the talk though!
[QUOTE=raccoon12;16705462]Still a programming newbie, what does the "this" pointer do?[/QUOTE] 'this' makes sure that you're accessing the object of a class instead of the class itself.
[QUOTE=Siduron;16705510]'this' makes sure that you're accessing the object of a class instead of the class itself.[/QUOTE] No, because in a member function you're accessing the "object of a class" (you probably want to say instance, not object) regardless of whether you use this-> or not. The only exception is when you have global variables (which you really shouldn't) or something of the sort that will pollute your namespace.
[QUOTE=nullsquared;16705591]No, because in a member function you're accessing the "object of a class" (you probably want to say instance, not object) regardless of whether you use this-> or not. The only exception is when you have global variables (which you really shouldn't) or something of the sort that will pollute your namespace.[/QUOTE] Exactly like i was saying.
@ OP: Don't forget include guards. Or the compiler will bite you when you get into more than 1 source file.
Thanks everyone, going to give all of you Hearts
Sorry, you need to Log In to post a reply to this thread.