C++ Header file/cpp file incompatibility?

girisking

Honorable
Jun 20, 2012
6
0
10,510
Hey folks,

So I'm running into trouble with this HW code I'm trying to write (making a linked list). It's my idea to pass a pointer to my list object and then have it carry out some processes which I have yet to write. For some reason however, in the list class.cpp, the compiler is giving me an error saying the method is incompatible with the declaration in the header.

Other things: I have a class named node. (list will interact with nodes eventually...)
Also, don't worry about extraneous includes unless that's the issue.

My list class header:
___________________________________________
#pragma once
#include <iostream>
#include "Node Class.h"

using namespace std;

class list
{
public:
int returnNodeValue(void);
void addItem(node*);
list();

private:
node* top;
node* passedAddress;
};
___________________
My list class.cpp:
#include "Node Class.h"
#include "List Class.h"

using namespace std;

void list::addItem(node* newNodeAddress)
{
if (top == NULL)
{

}

}

//constructor
list::list()
{
passedAddress = NULL;
top = NULL;
}
____________________________________

The error is underlined at addItem in "void list::addItem(node* newNodeAddress)"

I really have no idea why this doesn't work. I am able to replace "int" with node, and then it works.

Thanks for any help!
 
Solution
Works for me.

list.h:
Code:
#include <iostream>

using namespace std;

class node
{
public:
	int i;
};

class list
{
public:
	int returnNodeValue(void);
	void addItem(node*);
	list();

private:
	node* top;
	node* passedAddress;
};

list.cpp
Code:
#include "list.h"

using namespace std;

void list::addItem(node* newNodeAddress)
{
	if (top == NULL)
	{

	}

}

//constructor
list::list()
{
	passedAddress = NULL;
	top = NULL;
}

P.S. use [ code ] [/ code] tags.

girisking

Honorable
Jun 20, 2012
6
0
10,510
you don't say...

the strange thing is having my headerfile prototype like:
void addItem(node*);

and the .cpp call of the method like:
void list::addItem(node* newNodeAddress)

doesn't work. HOWEVER:
void addItem(int*); and void list::addItem(int* newNodeAddress)

DOES.

I guess i'm not sure why int should work and my class, node, which is included in the class list.h file doesn't.

thoughts?
 

Sunius

Distinguished
Dec 19, 2010
390
0
19,060
Works for me.

list.h:
Code:
#include <iostream>

using namespace std;

class node
{
public:
	int i;
};

class list
{
public:
	int returnNodeValue(void);
	void addItem(node*);
	list();

private:
	node* top;
	node* passedAddress;
};

list.cpp
Code:
#include "list.h"

using namespace std;

void list::addItem(node* newNodeAddress)
{
	if (top == NULL)
	{

	}

}

//constructor
list::list()
{
	passedAddress = NULL;
	top = NULL;
}

P.S. use [ code ] [/ code] tags.
 
Solution

girisking

Honorable
Jun 20, 2012
6
0
10,510
oh i didn't know those code tags existed, thanks!

hmm, so. I had my nodeClass in another header and .cpp file. I combined the two and it seems to work now. That's so strange.

Thanks for your help
 

Ijack

Distinguished
Just one point to note. In your code you were including the "node" header file twice; once in the "list" header file and then again directly in the "list" source file. It's difficult to say what effect this would have without seeing the header, but it is very bad practice. It is normal to put the contents of the header within a block like
Code:
#ifndef NODE_H
#define NODE_H

...

#endif
to ensure that the file is only included once.