Hey everybody.
I am currently facing a problem with pointer of template class, and i cannot see any easy solution.
Here is the code :
// Agent.h
class Space;
template<class T> class Agent
{
public:
Agent(Space* a_space,...); // there are other variables here but not important for the code
virtual ~Agent();
.... (other member functions)
private:
Space* m_linkToSpace;
};
// Agent. cpp
#include "Space.h"
// declaration of Agent's functions
template class Agent<int>;
template class Agent<double>;
Then I have derived template agents from template class Agent.
Then all agents are in a space (or in a derived class from space)
// Space.h
template<class T> class Agent; // I tried also with #include "Agent.h"
class Space
{
public:
Space();
virtual ~Space();
... (other functions using pointers on Agent<T>)
private:
std::map<int,Agent<T>*> m_agents; // to store every agents in the space and their Ids.
};
// Space.cpp
#include "Agent.h"
// Declaration of Space's functions
This code does not compile because of Agent<T>* (it works when Space has no pointer on Agent<T>). I have tried different things (add some typename before Agent<T> or template<class T> friend class Agent or template Agent<int> at the end...) but none worked.
Does anyone see how to make the code works ?? THANK YOU !!
lorenzo17
lorenzo17
View Public Profile
Send a private message to lorenzo17
Find More Posts by lorenzo17
« Test if number is integer or float? | Call for papers: SETP-10, USA, July 2010 »
Thread Tools
Show Printable Version
Email this Page
Search this Thread
Advanced Search
Rate This Thread
You have already rated this thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Vista cannot obtain an IP address from certain routers or from certain non-Microsoft DHCP Brian W Wireless Networking 7 01-31-2010 03:46 AM
server generates Delayed Write errors copying Very large Files Phil Lewis Windows 64bit 15 07-22-2008 04:28 PM
File & Printer Sharing Erratic msb Wireless Networking 3 04-04-2006 02:41 AM
no file sharing =?Utf-8?B?YmlnZ2llRw==?= Wireless Networking 5 04-03-2006 09:35 PM
Problem with web configuration utility =?Utf-8?B?VG9ueQ==?= Wireless Networking 9 02-25-2006 04:30 PM
Ugh, you are going to run into endless problems as a result of the circular reference. You're going to have to
rethink your design, otherwise you're going to have to explicitly instantiate Agent and/or Space on all the
types your program will use in their corresponding .cpp files.
This isn't as big of a mess as jsmith suggested. You just have to know about #include dependencies and forward declared dependencies.
You won't be able to have .cpp files here. Instead you can have normal header files (I'd go with "whatever.h") and implementation header files (I typically go with "whatever.hpp")
Here's how you could do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//agent.h
#ifndef AGENT_H_INCLUDED
#define AGENT_H_INCLUDED
template <typename T> class Space;
template <typename T> class Agent
{
// ... put your Agent class here
// do not put bodies for any functions which use 'Space' here
};
// then include the implementation file
#include "agent.hpp"
#endif // AGENT_H_INCLUDED
space.h would look similar, only you'd forward declare Agent instead of Space, then #include "space.hpp" at the end.
agent.hpp and space.hpp would look like normal .cpp files:
1 2 3 4 5 6 7 8
// agent.hpp
// you can put #include guards here if you want. Although this file should not be included
// by anything other than agent.h
#include "space.h"
#include "whatever other headers you need"
// give bodies to your functions here
space.hpp would look the same.
Of course you could put everything in the .h file and skip the .hpp files, but I find that a bit messier. Just instead of putting things in a hpp file and #including it, you can just put it directly after the class body in the header.