Solved

C++ Header file/cpp file incompatibility?

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!
8 answers Last reply Best Answer
More about header file file incompatibility
  1. Look at the declaration in the header and modify your code accordingly.
  2. 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?
  3. Best answer
    Works for me.

    list.h:
    #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
    #include "list.h"
    
    using namespace std;
    
    void list::addItem(node* newNodeAddress)
    {
    	if (top == NULL)
    	{
    
    	}
    
    }
    
    //constructor
    list::list()
    {
    	passedAddress = NULL;
    	top = NULL;
    }


    P.S. use
    tags.
  4. Best answer selected by girisking.
  5. 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
  6. 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
    #ifndef NODE_H
    #define NODE_H
    
    ...
    
    #endif
    

    to ensure that the file is only included once.
  7. oh okay. good to know, thanks. I generally throw a #pragma once on top of my header files :)
  8. Ah yes! I missed the #pragma once. Although not standard most comilers support it.

    I guess I'm just old school.
Ask a new question

Read More

Programming Apps