Link to home
Start Free TrialLog in
Avatar of Manny1
Manny1

asked on

error class type redefinition

I hope there's room for beginners here..

If you have time, please take a look at my 3 files (2 headers, 1 cpp)

////Item.h//////


/**Item header*/
class Item{
private:
     int a;
     char b;
public:
     Item(){};

     Item(int a, char b){
          a=0;
//          b="w";
     };
     ~Item() {};
};

--------------------------------------

///////node.h//////

// Node Header

#include "Item.h"
class node
{
     Item item;
     node* next;
     node(Item x, node* t)
     {
//          item = x;
//          next = t;
     };


};
typedef node* link;

--------------------------------------
/////list.cpp//////


/* Program linkedlist */

#include <stdlib.h>
#include "Item.h"
#include "node.h"

void main()
{
     Item data;
};

--------------------------------



compiling this in VB6 gives me 1 error and really I don't get it..
the produced error :

--------------------Configuration: Trees - Win32 Release--------------------
Compiling...
list.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\Trees\Item.h(4) : error C2011: 'Item' : 'class' type redefinition
Error executing cl.exe.

list.obj - 1 error(s), 0 warning(s)


I've tried struct also, same difference..I think maybe it has something to do with the construction of my class, but comparing with examples I found I don't see a difference..would anyone be able to clarify this for me?
the point is in the end to produce trees but I wanted to start with linked lists first to see if I understand that already..

Big thanks!
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You include "item.h" twice!
Try to use #ifndef direvtive in your "item.h" like this:

#ifndef __ITEM_H__
#define __ITEM_H__

/**Item header*/
class Item{
...
};

#endif // __ITEM_H__
Avatar of Manny1
Manny1

ASKER

I got your point...
Is it necessary to use the #ifndef there in case I know I will always use the same headerfiles anyway? I mean what is the advantage of using the #ifndef-directive (thanks for that I had never heard of it before..), above simply deleting a #include "item.h"-line in the node-header for instance, are they not being compiled together then anyhow?
Thanks again though!
Avatar of Manny1

ASKER

amazing speed :)
You should always use #ifndef in h-files. In complicated projects there are always situations when the same h-file is included number of times.
If you create class using Visual Studio (right-click on root tree item in Class View and select New Class), Visual Studio generates these lines automatically.