C2065 vector generic

Here's my deal...

// error C2065: 'ClientConnection' : undeclared identifier
std::vector<ClientConnection> m_Connections;

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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);

	static int ms_ID;
	int m_ID;
};


Please help!
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...
Last edited on
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?

EXAMPLE: Win32 API
Last edited on
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.
Last edited on
Interesting, once again C, and so by extension the Win32 API, bits me in the pants with its liberal attitude. Thank you for that info.
Last edited on

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
Can I ask why it has to be static? In my experiance this usually leads to some trouble later on.
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).

Your problem with the #include "Server.h" is something called a Circular Dependency.
http://en.wikipedia.org/wiki/Circular_dependency

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.

To avoid this, either rethink how you organize your classes so this is not necessary, or use a Forward Declaration.
http://en.wikipedia.org/wiki/Forward_declaration

Since you have a circular dependency, you will not be able to use a complete object type in one of the classes. You will need to use a pointer, and promise the linker that the class will be defined later.
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11
Thank you Branflakes!
I had a small hunch that it had something to do with circular dependency (now I remember what it is called....).

I added the line class ClientConnection; just before the server class declaration and it has compiled successfully.
My static ID works now too.

Much appreciated, you've saved me a world of headaches!
Topic archived. No new replies allowed.