473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Redefinition Error

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 Formula:public 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 Operation:public 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

Sep 18 '05 #1
3 8734
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 Formula:public 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 Operation:public 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
Sep 18 '05 #2
* 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 Formula:public 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?
Sep 18 '05 #3
al***@start.no (Alf P. Steinbach) wrote in
news:43****************@news.individual.net:
* 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 Formula:public 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.
Sep 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lomat | last post by:
Hello, While compiling a file, I get following error .... ================================= /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/type_traits.h:14 2: redefinition of...
1
by: A | last post by:
Hi, I created an array of user defined objects dynamically in a class Foo's constructor. class Foo{ Foo(){ Bar* b = new Bar(); }
1
by: squallions | last post by:
Hi I doing my c++ homework and stuck on error 'class' type redefinition. I have 5 classes. First class is base class. The next two classes are derived from the first class. The next two...
4
by: junaidnaseer | last post by:
Hi ! I am facing a problem that I have defined a function which when called in the same file generates an error as follows; " visual c error C2371 redefinition basic types see...
1
by: Alex | last post by:
Hello all, I have a very stupid problem that is driving me crazy...so plz if anyone ever saw this, I would like him to help me :) I have static MFC application in MSVC++ 6.0 (named Example)....
2
by: Mohammad Omer | last post by:
Hi, i am developing an application which uses WAB API's, for doing all this i am using vs2k5. I have wab.h header file included in my project to use WAB api's but after compilation one error...
5
by: aspineux | last post by:
I want to parse 'foo@bare' or '<foot@bar>' and get the email address foo@bar the regex is r'<\w+@\w+>|\w+@\w+' now, I want to give it a name
0
by: =?Utf-8?B?Sm9obkE=?= | last post by:
When adding a "Managed Assembly" i.e., a dll to an existing C++/CLI program i get the error: LDAPfunctions.h(183) : error C2011: 'LDAPfunctions::LdapSsl' : 'class' type redefinition This is...
9
by: pauldepstein | last post by:
On my visual c++ compiler, I compiled code which contained something like for( int i =0; i < 5; i++) { double x =5;} I expected it to give a compiler error because x is being redefined
7
by: Samant.Trupti | last post by:
Hi, I am getting this error. error C2371: 'LineCollection' : redefinition; different basic types I have 'LineCollection' defined in two header files in one project Like "typedef...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.