Sign in with
Sign up | Sign in
Your question
Solved

C++ Header file/cpp file incompatibility?

Last response: in Applications
Share
October 28, 2012 6:31:30 PM

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!
a b L Programming
October 28, 2012 7:08:12 PM

Look at the declaration in the header and modify your code accordingly.
m
0
l
October 28, 2012 7:39:20 PM

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?
m
0
l
Related resources

Best solution

a b L Programming
October 28, 2012 7:56:24 PM

Works for me.

list.h:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class node
  6. {
  7. public:
  8. int i;
  9. };
  10.  
  11. class list
  12. {
  13. public:
  14. int returnNodeValue(void);
  15. void addItem(node*);
  16. list();
  17.  
  18. private:
  19. node* top;
  20. node* passedAddress;
  21. };


list.cpp
  1. #include "list.h"
  2.  
  3. using namespace std;
  4.  
  5. void list::addItem(node* newNodeAddress)
  6. {
  7. if (top == NULL)
  8. {
  9.  
  10. }
  11.  
  12. }
  13.  
  14. //constructor
  15. list::list()
  16. {
  17. passedAddress = NULL;
  18. top = NULL;
  19. }


P.S. use [ code ] [/ code] tags.
Share
October 28, 2012 9:55:18 PM

Best answer selected by girisking.
m
0
l
October 28, 2012 9:55:39 PM

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
m
0
l
a b L Programming
October 29, 2012 6:35:35 AM

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
  1. #ifndef NODE_H
  2. #define NODE_H
  3.  
  4. ...
  5.  
  6. #endif

to ensure that the file is only included once.
m
0
l
October 29, 2012 1:59:24 PM

oh okay. good to know, thanks. I generally throw a #pragma once on top of my header files :) 
m
0
l
a b L Programming
October 30, 2012 6:46:43 AM

Ah yes! I missed the #pragma once. Although not standard most comilers support it.

I guess I'm just old school.
m
0
l
!