I'm implementing an NN, and I've made a class for the connection between nodes and a class for nodes. In the class for nodes, there is a vector of connections and in the class for connections, there is a node* for the sender and receiver.
Node.hpp is the first to get processed by the compiler, b/c it is included in directly in main. Connection is included only by Node.hpp.
Originally Posted by
GCC
error: invalid use of incomplete type ‘struct Node’
error: forward declaration of ‘struct Node’
Code:
#ifndef NODE_HPP_INCLUDED
#define NODE_HPP_INCLUDED
#include <vector>
#include "Connection.hpp"
class Node
{
public:
void Recieve()
{
for(unsigned int i = 0;i < input.size();i++)
if((input[i])->Empty() == true)
return;
// TODO: Process input ...
}
private:
std::vector<class Input*> input;
std::vector<Connection*> output;
};
#endif // NODE_HPP_INCLUDED
Code:
#ifndef CONNECTION_INCLUDED
#define CONNECTION_INCLUDED
#include <cstdlib>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include "Node.hpp"
typedef bool Message;
class Connection
{
public:
Connection(class Node *sender, class Node *reciever) : reciever(reciever), sender(sender) {}
protected:
class Node *reciever, *sender;
};
class Input : public Connection
{
public:
Input(class Node *sender, class Node *reciever) : Connection(sender, reciever) {}
bool Empty()
{
return this->queue.empty();
}
virtual Message Recieve()
{
Message t = this->queue.front();
this->queue.pop();
return t;
}
protected:
std::queue<Message> queue;
};
class Output : public Input
{
public:
Output(class Node *sender, class Node *reciever) : Input(sender, reciever) {srand(time(NULL));}
Message Recieve()
{
int uid = _alloc_uid();
Message t = this->queue.front();
this->queue.pop();
this->sent.insert(this->sent.begin(), std::pair<int, Message>(uid, t));
return t;
}
void Send(Message m)
{
this->queue.push(m);
this->reciever->Recieve();
}
protected:
std::map<int, Message> sent;
private:
static std::set<int> _uid_list;
static int _alloc_uid()
{
int t;
while(_uid_list.count((t = rand())) != 0);
_uid_list.insert(_uid_list.end(), t);
return t;
}
static void _free_uid(int uid)
{
_uid_list.erase(uid);
}
};
#endif // CONNECTION_INCLUDED