0

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:

  • undeclared identifiers first, last in List.cpp (base class)
  • syntax error : missing ';' before '*' -> List.h declaration of TestClass *last;

Can you please give a helping hand?

3
  • On which lines do errors occur? Commented Nov 10, 2013 at 19:11
  • 1
    List header should #include "TestClass.h" Commented Nov 10, 2013 at 19:14
  • @ChrisLaplante undeclared identifiers on every "first"/"last" mention in List.cpp and the syntax errors when on declaration of *last and *first in List.h Commented Nov 10, 2013 at 19:14

2 Answers 2

1

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

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

1 Comment

@BadescuAlexandru That is because you are calling the construcor wrong. Also a linker error is "better" than a compiler error in the sense that at least the syntax was correct. Please don't drive your development by compiler errors though!
1

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

Comments

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.