Good afternoon,
I have a mysterious issue, where I can call a class I've defined without any issues, however if I try to add a parameter to the constructor, and instantiate the class with an argument, it gives two LNK2001
errors, one for the constructor and one for the 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 is as 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");
}
I have a follow up error.
I am still looking for help with the above issue, but in order to save time, I added an extra method to the class and changed my 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, I now get the error:
error C2227: left of '->setAddress' must point to class/struct/union/generic type 1664 1
I seem to have done everything correctly, and I don't understand why this may be happening. Thank you.
You have the whole bunch of errors and misconceptions. Starting with Hello World program and reading some C++ tutorials would be good for you. Now to the problems.
You forgot to declare parameterless constructor in header file:
class socket_h {
...
socket_h();
...
};
Your source file looks suspicious too. You shouldn't be using class socket_h { ... };
in source file anymore. Instead define methods and 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, you have semantic error with constructor chain call:
socket_h::socket_h()
{
socket_h("192.168.5.100"); // <--- this is illegal
}
In C++11 you can use Delegating constructors as follows:
socket_h::socket_h(): socket_h("192.168.5.100")
{
}
However, this feature seems to be not implemented in VC++ 11 yet.
Change to:
socket_h accelerometer;
ie remove ()
. Otherwise, the compiler confuses it with parameterless function declaration, which would have name accelerometer
and return type socket_h
. This ambiguity issue is known as the most vexing problem .
Change to:
accelerometer.setAddress("192.168.5.100", 80);
Explanation is simple: accelerometer
is not a pointer, therefore you should not be using ->
to access its members and methods. accelerometer
is an instance of socket_h
, so to access its members and methods, you should use .
operator.
socket_h accelerometer("192.168.5.100"); // You don't have a constructor that takes a string as an argument.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.