Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Redefinition Error

Reply
Thread Tools

Redefinition Error

 
 
cody
Guest
Posts: n/a
 
      09-18-2005
Hello fols. I'm doin a school project and have this stupid problem. We're
on virtual functions and have to seperate out each class into a file. There
are 9 classes and so 9 .h and .c files. the file structure is something
like this.

control
|
formula
|| |
|| |
|| Operation
|variable |||
literal ||AND
|XOR
OR

So formula inherits from control and operation from formula and etc. Well
each of those classes would have an include for their parent and that's the
problem i'm having. I've compiled the 9 classes, but the main keeps giving
me that redefinition error. In main i had to include formula, variable,
literal, and, or, xor.h.

Formula.h
#include <iostream>
#include "Control.h"
#include<cstring>
using namespace std;
class Literal;

class Formulaublic Control
{
public:

virtual void print(ostream& os) const=0;
virtual Formula* differentiate(const string& var) const=0;
virtual bool evaluate(bool& val) const=0;
virtual Formula*subst() const=0;
virtual Formula*reduce(const string& var, const Literal* val) const=0;

Control.h
public:
void addRef();
void removeRef() ;

private:
int refCount;

protected:
Control(void);
virtual ~Control(void){;}

================================================== =============
Literal.h
#include <iostream>
#include <cstring>
#include "Formula.h"
using namespace std;

class Literal : public Formula
{
private:

bool value;

public:

Literal(bool v);
~Literal(void);
void print(ostream& os) const;
bool evaluate(bool& val) const;
Formula* reduce(void) const;
Formula* differentiate( const string& val ) const;
Formula* subst(const string& var, const Literal* val) const;

};
================================================== =====================

Operation.h
#include <iostream>
using namespace std;
#include "Formula.h"

class Operationublic Formula
{
public:

protected:
Operation( Formula* l=0, Formula* r=0);
~Operation(void);
Formula * left;
Formula * right;
};

================================================== =====================
And.h
#include <iostream>
#include "Operation.h"
using namespace std;

class And : public Operation
{
public:

And(Formula* l, Formula*r): Operation(l,r){}
void print(ostream& os) const;
bool evaluate(bool& val) const;
Formula* reduce(void) const;
Formula* differentiate( const string& var ) const;

};

Thanks in advance

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-18-2005
cody wrote:
> Hello fols. I'm doin a school project and have this stupid problem. We're
> on virtual functions and have to seperate out each class into a file. There
> are 9 classes and so 9 .h and .c files. the file structure is something
> like this.
>
> control
> |
> formula
> || |
> || |
> || Operation
> |variable |||
> literal ||AND
> |XOR
> OR
>
> So formula inherits from control and operation from formula and etc. Well
> each of those classes would have an include for their parent and that's the
> problem i'm having. I've compiled the 9 classes, but the main keeps giving
> me that redefinition error. In main i had to include formula, variable,
> literal, and, or, xor.h.
>
> Formula.h
> #include <iostream>
> #include "Control.h"
> #include<cstring>
> using namespace std;
> class Literal;
>
> class Formulaublic Control
> {
> public:
>
> virtual void print(ostream& os) const=0;
> virtual Formula* differentiate(const string& var) const=0;
> virtual bool evaluate(bool& val) const=0;
> virtual Formula*subst() const=0;
> virtual Formula*reduce(const string& var, const Literal* val) const=0;
>
> Control.h
> public:
> void addRef();
> void removeRef() ;
>
> private:
> int refCount;
>
> protected:
> Control(void);
> virtual ~Control(void){;}
>
> ================================================== =============
> Literal.h
> #include <iostream>
> #include <cstring>
> #include "Formula.h"
> using namespace std;
>
> class Literal : public Formula
> {
> private:
>
> bool value;
>
> public:
>
> Literal(bool v);
> ~Literal(void);
> void print(ostream& os) const;
> bool evaluate(bool& val) const;
> Formula* reduce(void) const;
> Formula* differentiate( const string& val ) const;
> Formula* subst(const string& var, const Literal* val) const;
>
> };
> ================================================== =====================
>
> Operation.h
> #include <iostream>
> using namespace std;
> #include "Formula.h"
>
> class Operationublic Formula
> {
> public:
>
> protected:
> Operation( Formula* l=0, Formula* r=0);
> ~Operation(void);
> Formula * left;
> Formula * right;
> };
>
> ================================================== =====================
> And.h
> #include <iostream>
> #include "Operation.h"
> using namespace std;
>
> class And : public Operation
> {
> public:
>
> And(Formula* l, Formula*r): Operation(l,r){}
> void print(ostream& os) const;
> bool evaluate(bool& val) const;
> Formula* reduce(void) const;
> Formula* differentiate( const string& var ) const;
>
> };
>
> Thanks in advance
>


You need to use the 'include guard' trick.

// And.h
#ifndef AND_H
#define AND_H

contents of And.h here

#endif

// Operation.h
#ifndef OPERATION_H
#define OPERATION_H

contents of Operation.h here

#endif

etc. etc.

This stops your header files from being compiled more than once in the
same compilation which I think is the problem you're describing above.
Every header file you write should use include guards.

This trick doesn't stop headers being compiled more than once in
different complations though, nothing could do that, which is what some
newbies seems to think.

john
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-18-2005
* cody:
> Hello fols.


Folks? Fools? What?


> We're on virtual functions and have to seperate out each class into a file.


That's not always a good idea.

Anyway, that's _physical packaging_ of the code.

The logical structure of the code is mostly independent of physical packaging.


> There are 9 classes and so 9 .h and .c files.


For C++ it's a good idea to choose a different file name suffix for
implementation file than for C, e.g., use .cpp instead of .c.


> the file structure is something like this.
>
> control
> |
> formula
> || |
> || |
> || Operation
> |variable |||
> literal ||AND
> |XOR
> OR
>
> So formula inherits from control and operation from formula and etc.


The top level doesn't make much sense: how is a formula a "control"?


> Well
> each of those classes would have an include for their parent and that's the
> problem i'm having. I've compiled the 9 classes, but the main keeps giving
> me that redefinition error.


That is one problem, let's call it problem (A).


> In main i had to include formula, variable, literal, and, or, xor.h.


That is another problem, let's call it problem (B).

You haven't described what problem (A) is exactly, so no answer is possible
there.

For problem (B), simply create a header file, like, [expression.h], that
includes the relevant other header files.



> Formula.h



At this point you should have an _include guard_, e.g. as follows (simplest
possible):

#ifndef FORMULA_H
#define FORMULA_H

and then at the very end of the file,

#endif

Just doing this _may_ seem to solve your problem (A).

But it wouldn't really be a solution of that problem until you identified
exactly what caused the problem, and could reproduce the problem.


> #include <iostream>


In general, only do i/o in classes _dedicated_ to i/o, and nowhere else.
Otherwise your classes will be not very much reusable (e.g., in a graphical
user interface). And, too complex to discuss here, i/o creates a lot of other
problems, directly and indirectly.


> #include "Control.h"
> #include<cstring>
> using namespace std;


Don't _ever_ put 'using namespace std;' in a header file.



> class Literal;
>
> class Formulaublic Control
> {
> public:
>
> virtual void print(ostream& os) const=0;


As mentioned, it's not a good idea to do i/o here. Instead consider a
conversion to string. Or something -- separation of concerns (consider how
this class could be used in a windowing application).


> virtual Formula* differentiate(const string& var) const=0;


That's a raw pointer I see there in the result type. Is it a pointer to an
array or to a single object? How long is it valid? Who is responsible for
deallocation? Better make it a std::auto_ptr or some such.


> virtual bool evaluate(bool& val) const=0;
> virtual Formula*subst() const=0;
> virtual Formula*reduce(const string& var, const Literal* val) const=0;


Ditto.

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
cody
Guest
Posts: n/a
 
      09-18-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (Alf P. Steinbach) wrote in
news:(E-Mail Removed):

> * cody:
>> Hello fols.

>
> Folks? Fools? What?
>

sorry lol ment to say folks. didnt spell check it enough ^.^
>
>> We're on virtual functions and have to seperate out each class into a
>> file.

>
> That's not always a good idea.
>
> Anyway, that's _physical packaging_ of the code.
>
> The logical structure of the code is mostly independent of physical
> packaging.
>
>
>> There are 9 classes and so 9 .h and .c files.

>
> For C++ it's a good idea to choose a different file name suffix for
> implementation file than for C, e.g., use .cpp instead of .c.
>
>
>> the file structure is something like this.
>>
>> control
>> |
>> formula
>> || |
>> || |
>> || Operation
>> |variable |||
>> literal ||AND
>> |XOR
>> OR
>>
>> So formula inherits from control and operation from formula and etc.

>
> The top level doesn't make much sense: how is a formula a "control"?


Control more or less keeps track of references to a formula. if a formula
has no references then it is deleted.
>
>
>> Well
>> each of those classes would have an include for their parent and
>> that's the problem i'm having. I've compiled the 9 classes, but the
>> main keeps giving me that redefinition error.

>
> That is one problem, let's call it problem (A).
>
>
>> In main i had to include formula, variable, literal, and, or, xor.h.

>
> That is another problem, let's call it problem (B).
>
> You haven't described what problem (A) is exactly, so no answer is
> possible there.
>
> For problem (B), simply create a header file, like, [expression.h],
> that includes the relevant other header files.
>
>
>
>> Formula.h

>
>
> At this point you should have an _include guard_, e.g. as follows
> (simplest possible):
>
> #ifndef FORMULA_H
> #define FORMULA_H
>
> and then at the very end of the file,
>
> #endif
>
> Just doing this _may_ seem to solve your problem (A).
>
> But it wouldn't really be a solution of that problem until you
> identified exactly what caused the problem, and could reproduce the
> problem.
>
>
>> #include <iostream>

>
> In general, only do i/o in classes _dedicated_ to i/o, and nowhere
> else. Otherwise your classes will be not very much reusable (e.g., in
> a graphical user interface). And, too complex to discuss here, i/o
> creates a lot of other problems, directly and indirectly.
>
>
>> #include "Control.h"
>> #include<cstring>
>> using namespace std;

>
> Don't _ever_ put 'using namespace std;' in a header file.
>
>
>
>> class Literal;
>>
>> class Formulaublic Control
>> {
>> public:
>>
>> virtual void print(ostream& os) const=0;

>
> As mentioned, it's not a good idea to do i/o here. Instead consider a
> conversion to string. Or something -- separation of concerns
> (consider how this class could be used in a windowing application).


i woulda used string or something if i could, but all the function
protypes were specified by the prof so I gotta work around em.
>
>
>> virtual Formula* differentiate(const string& var) const=0;

>
> That's a raw pointer I see there in the result type. Is it a pointer
> to an array or to a single object? How long is it valid? Who is
> responsible for deallocation? Better make it a std::auto_ptr or some
> such.
>
>
>> virtual bool evaluate(bool& val) const=0;
>> virtual Formula*subst() const=0;
>> virtual Formula*reduce(const string& var, const Literal* val)
>> const=0;

>
> Ditto.
>
> Hth.,
>
> - Alf
>


Thanks for the reply guys. i'll try the include guard again. I did it b4
and wheeww it threw out lotsa errors.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template "redefinition" linker error. (attempt #3) Robbie Hatley C++ 2 06-29-2006 12:09 AM
Template "redefinition" linker error. (attempt #2) Robbie Hatley C++ 1 06-28-2006 09:32 PM
Strange template "redefinition" linker error. Robbie Hatley C++ 0 06-28-2006 02:10 AM
visual C++ error C2371 redefinition basic types junaidnaseer C++ 4 05-09-2006 04:04 PM
Bar: 'class' type redefinition compile time error A C++ 1 10-22-2003 10:34 AM



Advertisments