0
class List{
        public:
        List();
        void add(int x);
        void remove();
        void display();
        void findingEvens(Node* n, Node* &h);
        private:
        struct Node{
                Node* next;
                int data;

        };
        Node* head;

    };

I have the above code in my header class, in the member function

void findingEvens(Node* n, Node* &h);

The problem is in the main class it gives an error for the following code besides I've already include the list.h,

    Node *result = 0;
    cout << findingEvens(l, result);
    l.display();

As an error it says that

error: ‘Node’ was not declared in this scope

But to declare it in this scope I've already include the list.h class. Am I wrong?

3 Answers3

3

However the main problem is in the prototype of the function findingEvents it gives an error for the Node* however it's already defined

You use the type before it seen by the compiler in the same file. So the compiler cannot understand what is Node
Move the function declaration after the structure is declared.

 public:
        List();
        void add(int x);
        void remove();
        void display();
        private:
        struct Node{
                Node* next;
                int data;

        };
        Node* head;
        void findingEvens(List::Node* n, List::Node* &h);

Node is a nested structure. You need to tell the compiler where to find it by using its full qualification:

List::Node *result = 0;
^^^^^^^^^^^^

You need to read about Nested classes.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
0

Try moving the prototype of findingEvens to be after the definition of struct Node. You should remove the private: for now.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
0

This answer is not really useful anymore, but here the fixed code:

class List{
public:
    List();
    void add(int x);
    void remove();
    void display();

    struct Node {
        struct Node* next;
        int data;
    };

    void findingEvens(Node* n, Node* &h);
private:
    Node* head;
};
benjarobin
  • 4,410
  • 27
  • 21
  • Well I learn one thing today : http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c – benjarobin Jan 01 '13 at 15:32