Alex
Alex

Reputation: 11137

syntax errors in c++ linked list declaration

ok, so i've decided to start brushing off the dust from my c++ knowledge, and started by doing some simple examples with linked lists, but i get some errors. my Node class is TestClass and my linked list is List; The problems, from what i see are syntax related.

Node Header:

#pragma once
class TestClass
{
public:
    int x, y;
    TestClass *next;
    TestClass *prev;
    TestClass();
    TestClass(int,int);
    ~TestClass();
};

Node Base class

#include "stdafx.h"
#include "TestClass.h"

TestClass::TestClass()
{
}

TestClass::TestClass(int _x, int _y)
{
    x = _x;
    y = _y;
 }

TestClass::~TestClass()
{
}

Header List class

#pragma once
class List
{
public:

    TestClass *first;
    TestClass *last;

    List();
    ~List();
    void AddNew(TestClass);
    void PrintList();
};

List base class

#include "stdafx.h"
#include "List.h"
#include "TestClass.h"
#include <iostream>


using namespace std;

List::List()
{
    first = last = NULL;
}


List::~List()
{

}

void List::AddNew(TestClass node)
{
    if (!first && !last)
    {
        *first = *last = node;
        //first = last = &node;
    }
    else
    {
        TestClass *temp;
        temp = last;
        last = &node;
        temp->next = last;

    }
}

void List::PrintList()
{
    TestClass *p = first;

    while (p != NULL)
    {
        cout << p->x << " ";
        p = p->next;
    }

}

I get around 16 errors, like:

Can you please give a helping hand?

Upvotes: 0

Views: 468

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

Header List class shall include header Node Header because it referes declarations from it.

Upvotes: 1

Jonas B&#246;tel
Jonas B&#246;tel

Reputation: 4482

#include "TestClass.h in List.h before using the type.

Upvotes: 1

Related Questions