0

Good afternoon,

Issue #1

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");

}

Issue #2

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.

Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85
Mich
  • 3,188
  • 4
  • 37
  • 85

2 Answers2

3

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.

Issue #1

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.

Issue #2

Change to:

socket_h accelerometer;

i.e. 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.

Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85
  • Thank you for your help, I changed my class functions, constructor, and destructor to use the scope resolution operator. However, the same exact problems occur. I also changed ->setAddress(...) to .setAddress(...) and I still get the error: Error error C2228: left of '.setAddress' must have class/struct/union – Mich Apr 18 '13 at 20:08
  • I used the same format as in this example: http://www.cplusplus.com/forum/beginner/11123/ – Mich Apr 18 '13 at 20:13
  • Thanks, I tried it, now I'm getting an "LNK2022: Inconsistent layout information in duplicated types" error. The class is instantiated from within windows forms C++ CLR code. I'm reading here: http://stackoverflow.com/questions/11990095/lnk2022-metadata-operation-inconsistent-layout-information-in-duplicated-types that this doesn't work with CLR? Is it impossible to use classes with CLR? I tried including at the very top as suggested here: http://www.idigitalhouse.com/Blog/?p=163, without any success – Mich Apr 18 '13 at 20:26
  • Just follow the answer in the question you linked and it should work. The errors you asked help about in the original question are resolved now, so this question is answered. If you have more troubles, you'd have to create a separate question and state clearly what they are, as this post gets too boilerplate. – Alexander Shukaev Apr 18 '13 at 20:30
  • Thanks for all the help, unfortunately, I can't remove the /clr option. I will have to get rid of the code as a class, since I only really need 1 instance of it. But I will reference this information next time I need to implement a class structure. Thanks, – Mich Apr 18 '13 at 20:31
  • @user2056201: It seems you are new here, here is the [FAQ](http://stackoverflow.com/faq#howtoask) worth looking at for you. If my answer was helpful to you, consider accepting it. Thanks. – Alexander Shukaev Apr 19 '13 at 23:10
0

socket_h accelerometer("192.168.5.100"); // You don't have a constructor that takes a string as an argument.