Thread: Expected constructor, destructor, type conversion

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Expected constructor, destructor, type conversion

    I get these two errors when I compile
    Code:
    /home/sterling/CODE/UnorderedList/UnorderedList.cpp|22|error: expected constructor, destructor, or type conversion before ‘&’ token|
    /home/sterling/CODE/UnorderedList/UnorderedList.cpp|23|error: expected constructor, destructor, or type conversion before ‘&’ token|
    The part of the file that it's telling me about are these two functions -

    Code:
    //GETTER FUNCTIONS
    Node& UnorderedList::Node::getNext() {return next;}
    Node& UnorderedList::Node::getPrevious() {return previous;}
    I feel like it's telling me that it doesn't recognize Node, but I don't understand why. Node is a nested class in UnorderedList.h. That file is this -

    Code:
    #ifndef UNORDEREDLIST_H
    #define UNORDEREDLIST_H
    #include <exception>
    
    class UnorderedList {
    public:
    
        class EmptyListException: public std::exception {
            public:
                virtual const char* what() const throw();
        };  //END EMPTYLISTEXCEPTION
    
        class InvalidInputException: public std::exception {
            public:
                virtual const char* what() const throw();
        };  //END INVALIDINPUTEXCEPTION
    
        class Node {
            public:
                Node(int element);
                ~Node();
    
                Node& getNext();
                Node& getPrevious();
                int& getElement();
    
            private:
                int element;
                Node* next;
                Node* previous;
        };  //END NODE CLASS
    
        UnorderedList();
        ~UnorderedList();
    
        void add(int element);
        int remove(int element);
        void print();
        int size();
        bool isEmpty();
    
    private:
        Node* head;
        int count;
    };
    
    #endif
    I don't see why the exception classes compile fine but Node doesn't. I have tried changing the return from Node& to Node*, changing the return in .cpp to UnorderedList::Node&, and other random things. None have worked though. Can anyone tell me what's wrong? Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I notice that for the return type, you write Node instead of UnorderedList::Node.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Do you mean in the .h or .cpp? I tried having
    Code:
    UnorderedList::Node& UnorderedList::Node::getNext() {return next;}
    But that gave me this error
    Code:
    /home/sterling/CODE/UnorderedList/UnorderedList.cpp|22|error: invalid initialization of reference of type ‘UnorderedList::Node&’ from expression of type ‘UnorderedList::Node*’|
    I tried changing it to UnorderedList::Node in both files, but I got the same error.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, but this error is different. It now says that your implementation of the function is wrong, e.g., it should return *next;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Oh okay, thank you!

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Quick semi-related question,

    next and previous are pointers to a Node object. If I return the pointer (Node* instead of Node&), will it return the pointer to where I could change it? Or does it copy the pointer?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would return a copy of the pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Windows using Dev-C++
    By Renegade in forum C++ Programming
    Replies: 15
    Last Post: 07-07-2005, 08:29 PM