UrhoKarila
UrhoKarila

Reputation: 354

How do I return a pointer to an Object's Struct with a method?

I'm trying to write a method in C++ for a custom list class, called List. It's a linked list made up of Nodes, which are the various items. For the method, I want it to return a pointer to a Node, but get an error saying that " 'Node' does not name a type." Note that the sort() method is still in progress, and I'm waiting on setMyNext() for that.

template<class Item>
class List {
public:
List();
List(const List& original);
virtual ~List();
void sort();

List& operator=(const List& original);
bool operator==(const List& original)const;
bool operator!=(const List& l2) const;

private:
void print()const;
unsigned mySize;
struct Node{
    Node();
    Node(Item item, Node * next);
    ~Node();
    void print()const;
    Node * setMyNext(Node * newNext);
    Item myItem;
    Node * myNext;
};

Node * myFirst;
Node * myLast;

friend class ListTester;
};



template<class Item>
List<Item>::List() {
myFirst=NULL;
myLast=NULL;
mySize=0;
}

 template<class Item>
List<Item>::List(const List& original){
myFirst=myLast=NULL;
mySize=0;
if(original.getSize()>0){
    Node * oPtr = original.myFirst;
    while(oPtr!=NULL){
        append(oPtr->myItem);
        oPtr=oPtr->myNext;
    }
}
}

template<class Item>
List<Item>::Node::Node(){
myItem=0;
myNext=NULL;
}


template<class Item>
List<Item>::Node::Node(Item item, Node * next){
myItem=item;
myNext= next;
}

template<class Item>
List<Item>::~List() {
//  cout<<"Deleting List..."<<endl;
delete myFirst;
myFirst=myLast=NULL;
mySize=0;
}

template<class Item>
void List<Item>::sort(){
//get my first 2 items
if(mySize<2)
    return;
Node * compareEarly=myFirst;
Node * compareLate=myFirst->myNext;
//compare
if(compareEarly->myItem > compareLate->myItem){
//If 2<1, set 0's next pointer to 2, set 2's next to 1, set 1's next to 3
    cout<<"big"<<endl;

}
    //This needs a set previous pointer and set next item's pointer

    //increment
}

template<class Item>
Node * List<Item>::Node::setMyNext(Node * newNext){
myNext=newNext;
}

Upvotes: 0

Views: 465

Answers (2)

Joel Barba
Joel Barba

Reputation: 73

Your problem is that the compiler doesn't know what the definition of a 'Node *' is becuse the function definition is in the global namespace. You need to specify What namespace it is part of for the compiler to recognize it.

template<class Item>
List<Item>::Node * List<Item>::Node::setMyNext(Node * newNext){
myNext=newNext;
}

Upvotes: 0

Jimmy Lu
Jimmy Lu

Reputation: 5100

Because Node does not name a type.

The node you are trying to refer to here is really the node in your template class List. However, since you are defining your functions outside of the class declaration. The function's return type is in global (or ur current namespace) scope. Thus, in order for the compiler to find the correct type, you need to supply Node's full name.

In short:

Node * List<Item>::Node::setMyNext(Node * newNext){

This line is wrong. It should be this instead.

typename List<Item>::Node* List<Item>::Node::setMyNext(Node * newNext){

Same applies to your other functions...

Upvotes: 1

Related Questions