Incomplete Type

前端 未结 4 1408
孤街浪徒
孤街浪徒 2020-12-18 09:09

I\'m getting a incomplete type error for the \'next\' and \'previous\' variables. I\'m not sure what I am doing wrong because I am very rusty on writing classes in C++. An

相关标签:
4条回答
  • 2020-12-18 09:44

    You need to specify the return type for all of your .cpp functions. ex:

        //returns previous element in structure
    LinearNode LinearNode::getPrevious()
    {
        return previous;
    }//ends getPrevious function
    
    0 讨论(0)
  • 2020-12-18 09:56

    Your type has a recursive definition, which is prohibited.

    class LinearNode
    {
    private: 
        LinearNode next;
        LinearNode previous;
    };
    

    The data members next and previous are instances (not references or pointers) of the LinearNode class, which is not completely defined yet.

    You probably want this:

    class LinearNode
    {
    private: 
        LinearNode * next;
        LinearNode * previous;
    };
    
    0 讨论(0)
  • 2020-12-18 10:04

    Your members of type LinearNode are instances, which they aren't allowed to be in that context. Remember, in C++, if you declare "Foo f", that's not a reference to a heap object as in C#, but an instance of Foo right there on the stack or in your class layout.

    Even worse, those two LinearNode instances would be instantiated when their containing instance is instantiated. And each of them has two child instances which would be likewise instantiated, and each of those has two... And so on, and so on, until you run out of memory. Naturally you're not allowed to do that, because it makes no sense.

    So those have to be pointers or references. The reason is that the size of those two instances has to be known for the compiler to determine the size of the LinearNode class, but that size must be known before their size can be known.

    0 讨论(0)
  • 2020-12-18 10:06

    it would appear that your trying to impliment a "linked list"

    The structure of a linked is that next & previous "points" (*) and is not actually another node as Andre states.

    http://www.cplusplus.com/doc/tutorial/pointers/ may help clarify.

    class LinearNode
    {
    //...snip
    //returns the next node in the set.
    LinearNode* getNext();
    //returns the previous node in the set
    LinearNode* getPrevious();
    
    //...snip
    private: 
        LinearNode * next;
        LinearNode * previous;
    };
    

    So in the implementation file:

    //returns the next element in the structure
    LinearNode* LinearNode::getNext()
    {
        return next; // now a poiner
    }//ends getNext function
    
    //returns previous element in structure
    LinearNode*  LinearNode::getPrevious()
    {
        return previous; // now a poiner
    }//ends getPrevious function
    
    //sets the next variable for the node
    void LinearNode::setNext(LinearNode* node) //pointer in, void out
    {
        next = node
    }//ends the setNext function
    
    //sets previous for the node
    void LinearNode::setPrevious(LinearNode* node)//pointer in, void out
    {
        previous = node;
    }//ends the setPrevious function
    

    so main looks like:

    int main()
    {
        LinearNode node1, node2, move;
        node1.setElement(1);
        node2.setElement(2);
    
        node2.setNext(&node1); // give the address of node to the next pointer (&)
        node1.setPrevious(&node2); // give the address of node to the next pointer (&)
    
        move = node2;
    
        while(move.getNext() != NULL)
            cout << move.getElement() << endl;
    
    }
    

    I don't know what the while loop's trying to do though! if - perhaps?

    Also as Nicklamort states all your class functions need to have a return type, except in the case of void.

    0 讨论(0)
提交回复
热议问题