This used to compile, but I changed something I can't remember what and suddenly it doesn't compile. The only thing I remember adding is the ID's to the ClientConnection.
The following is what I think is relevant code:
#pragma once
#include "ServerObject.h"
// these following 3 are brought in from ServerObject.h
#include <iostream>
#include <stdio.h>
#include <vector>
/////////////
#include "ClientConnection.h"
class Server: public ServerObject
{
public:
/*stuff*/
//Gives this error:
// error C2065: 'ClientConnection' : undeclared identifier
std::vector<ClientConnection> m_Connections;
/*more stuff*/
};
The following is the ClientConnection code
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#pragma once
#include "Server.h"
#include "ServerObject.h"
class ClientConnection: public ServerObject
{
public:
ClientConnection(void);
virtual ~ClientConnection(void);
staticint ms_ID;
int m_ID;
};
Try removing "(void)" from your constructor. I can't remember off hand how to make this work with the way you have it, it has something to do with declaring a pointer data-type with your custom class though, the Win32 API likes to do stuff like this...
In C++, (void) and () have the exact same meaning. I'd remove it because it's ugly and redundant, but it's not what's causing the problem.
It seems to me the problem is caused by the inclusion of "Server.h" in ClientConnection.h. When the compiler reads the preprocessed data, it will first read the declaration of Server, which has a vector of ClientConnection objects, which hasn't been defined yet.
filipe: In C++, (void) and () have the exact same meaning...
Not to hijack the thread or challenge you in anyway I am aware of your abilities. But why then would that change with a typedef declaration? Or is a void pointer treated different from a void?
I meant the use of the void keyword in a parameter list to establish that a function takes no arguments.
In C, a function like int f() can take any number of arguments. The equivalent C++ would be int f(...). Because of that, in C, there's a way for you to explicitly say a function doesn't take any arguments: int f(void). The notation is accepted in C++ because of C compatibility, but is redundant because, in C++, int f() already means f takes no arguments.
It seems to me the problem is caused by the inclusion of "Server.h" in ClientConnection.h. When the compiler reads the preprocessed data, it will first read the declaration of Server, which has a vector of ClientConnection objects, which hasn't been defined yet.
I tried removing #include Server.h Just to see what happens, because right now ClientConnection doesn't use anything from the Server class, and I got these Linking errors
Error1 error LNK2001: unresolved external symbol "public: static int ClientConnection::ms_ID" (?ms_ID@ClientConnection@@2HA)
Error 2 fatal error LNK1120: 1 unresolved externals
I'm assuming that Error2 is caused by Error1, but what is causing Error1? I removed the static keyword from the ms_ID declaration and it compiled succesfully....
How do I get this to compile with static variable, and the Server.h include
In ClientConnection.cpp, add this line int ClientConnection::ms_ID = 0;
Static member variables need to be defined outside of the class that they are in, and the best place to do so is in the .cpp file. (You can change the 0 to whatever you want the initial value to be).
Whichever class gets compiled/linked first will include the other file, in which the class tries to use the first class, which is not fully compiled/linked yet. That's a problem.