• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 685
  • Last Modified:

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!
0
Manny1
Asked:
Manny1
  • 2
  • 2
1 Solution
 
AlexFMCommented:
Every h-file should contain lines which prevent it's including second or more time to the same c/cpp file:

#ifndef <some unique string>
#define <some unique string>

... all other stuff ...


#endif

With MS VC compiler you can use

#pragma once

Change your code by such way:


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

#ifnfef ITEM_H
#define ITEM_H

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

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

#endif
--------------------------------------

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

#ifnfef NODE_H
#define 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;

#endif

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


/* Program linkedlist */

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

void main()
{
    Item data;
};

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


0
 
alb66Commented:
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__
0
 
Manny1Author Commented:
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!
0
 
Manny1Author Commented:
amazing speed :)
0
 
AlexFMCommented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now