c++ - Error LNK2001 when instantiating a class with parameters within a constructor -
good afternoon,
issue #1
i have mysterious issue, can call class i've defined without issues, if try add parameter constructor, , instantiate class argument, gives 2 lnk2001 errors, 1 constructor , 1 destructor.
error lnk2001: unresolved external symbol "public: __thiscall socket_h::~socket_h(void)" (??1socket_h@@$$fqae@xz) error lnk2001: unresolved external symbol "public: __thiscall socket_h::socket_h(char const *)" (??0socket_h@@$$fqae@pbd@z) the code follows:
class header:
class socket_h{ protected: ;//... public: socket_h(const char*); int receive_data(char* szbuffer); int send_data(char* szmessage); ~socket_h(void); }; class source:
class socket_h{ protected: ;//... public: socket_h() { socket_h("192.168.5.100"); } socket_h(const char* ip_address) { ;//...; } //... ~socket_h(void) { closesocket(sclient); wsacleanup(); } }; calling function:
private: system::void read_socket_click(system::object^ sender, system::eventargs^ e) { socket_h accelerometer("192.168.5.100"); } issue #2
i have follow error.
i still looking above issue, in order save time, added method class , changed calling function to:
private: system::void read_socket_click(system::object^ sender, system::eventargs^ e) { socket_h accelerometer(); accelerometer->setaddress("192.168.5.100", 80); } however, error:
error c2227: left of '->setaddress' must point class/struct/union/generic type 1664 1 i seem have done correctly, , don't understand why may happening. thank you.
you have whole bunch of errors , misconceptions. starting hello world program , reading c++ tutorials you. problems.
issue #1
you forgot declare parameterless constructor in header file:
class socket_h { ... socket_h(); ... }; your source file looks suspicious too. shouldn't using class socket_h { ... }; in source file anymore. instead define methods , constructors using scope resolution operator:
socket_h::socket_h() { } socket_h::socket_h(const char* ip_address) { } socket_h::~socket_h(void) { closesocket(sclient); wsacleanup(); } furthermore, have semantic error constructor chain call:
socket_h::socket_h() { socket_h("192.168.5.100"); // <--- illegal } in c++11 can use delegating constructors follows:
socket_h::socket_h(): socket_h("192.168.5.100") { } however, feature seems not implemented in vc++ 11 yet.
issue #2
change to:
socket_h accelerometer; i.e. remove (). otherwise, compiler confuses parameterless function declaration, have name accelerometer , return type socket_h. ambiguity issue known the vexing problem.
change to:
accelerometer.setaddress("192.168.5.100", 80); explanation simple: accelerometer not pointer, therefore should not using -> access members , methods. accelerometer instance of socket_h, access members , methods, should use . operator.
Comments
Post a Comment