?
Solved

error class type redefinition

Posted on 2003-03-10
5
Medium Priority
?
664 Views
Last Modified: 2012-05-04
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
Comment
Question by:Manny1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 300 total points
ID: 8102428
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
 
LVL 19

Expert Comment

by:alb66
ID: 8102445
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
 

Author Comment

by:Manny1
ID: 8102627
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
 

Author Comment

by:Manny1
ID: 8102635
amazing speed :)
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8102703
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question