0

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;
}
1
  • one suggestion, for long code, it's important to format it properly. I had quite a bit of trouble reading through your code... :-( Commented Oct 18, 2012 at 18:33

2 Answers 2

1

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...

Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting these errors now: ..error: ‘Node’ in ‘class List<Item>’ does not name a type error: need ‘typename’ before ‘List<Item>::Node’ because ‘List<Item>’ is a dependent scope
@user1673882, oops my bad. I forgot the keyword typename is needed because you need to inform the compiler that Item is a template name and will be resolved later.
Alright, that did the trick. Thanks. Also, you mentioned "formatting code properly." I'm really new to C++, could you explain what I did wrong?
@user1673882, I just meant that when you post code on SO, make sure to indent your code properly. :)
0

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;
}

1 Comment

I tried this, and now it throws this: need ‘typename’ before ‘List<Item>::Node’ because ‘List<Item>’ is a dependent scope

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.