What to do when 2 classes are interdependant?

This is a discussion on What to do when 2 classes are interdependant? within the C++ Programming forums, part of the General Programming Boards category; I'm implementing an NN, and I've made a class for the connection between nodes and a class for nodes. In ...

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    What to do when 2 classes are interdependant?

    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.

    Quote 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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > #include "Node.hpp"
    Remove this from the connection.hpp file, and replace it with

    class Node;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I still get the same error messages. Notice that in Output::Send(), I call a method in Node.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    To fix your problem, you have to move the problematic code into a .cpp file. In order for your code to work correctly as is now, you need to have it all in the same header otherwise it will not work.

    Headers are for declarations, cpp files are for definitions

  5. #5
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I have a habit of doing my headers before implementing the definitions behind them. Sometimes I get to prototyping and then BOOM, everything blows up in my face. Thanks, I've got all sorted out into separate files and it compiles fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple classes question
    By TriKri in forum C++ Programming
    Replies: 20
    Last Post: 06-11-2010, 04:03 PM
  2. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21