Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > user-type operator definition and multiple classes / source files

Reply
Thread Tools

user-type operator definition and multiple classes / source files

 
 
Gerard Kramer
Guest
Posts: n/a
 
      06-17-2006
Hello,

There is a slight problem with operator overloading in a program I
attempt to start practising C++. It is a basic (not very original) game
of life simulator. It uses two classes: LifeGeneration and LifeHistory.
Declarations are given by:

// Contents of file LifeGeneration.h

#ifndef LIFEGENERATION_H
#define LIFEGENERATION_H

namespace life {

class LifeGeneration {

private:

int generation[ROW_DIM][COL_DIM];

unsigned mod(int, unsigned);
unsigned int countNn(int, int);
int alive(int, int);

public:

LifeGeneration();
void setCell(int, int, int);
void nextGeneration();
bool operator == (const LifeGeneration &);

};

}

#endif

and:

// Contents of file LifeHistory.h

#ifndef LIFEHISTORY_H
#define LIFEHISTORY_H

namespace life {

class LifeHistory {

private:

LifeGeneration history[MAX_GENERATIONS];
unsigned n; // Generation counter

public:

LifeHistory();
unsigned storeGeneration(LifeGeneration);
unsigned getLength();
unsigned isPeriodic();

};

}

#endif

There is a problem with the following implementation:

// Contents of file LifeHistory.cpp

#include "parameters.h" // Contains definitions of constants
#include "LifeGeneration.h"
#include "LifeHistory.h"

namespace life {

// (...)

unsigned LifeHistory::isPeriodic() {
// Tests whether evolution is periodic with period > 0.
// Returns zero for a-periodicity.
for (unsigned i = 0; i < n; i++)
if (history[i] == history)
return i;
return 0;
}

}

The test-for-equality operator used in the isPeriodic() function is
defined as:

// Contents of file LifeGeneration.cpp

#include "parameters.h"
#include "LifeGeneration.h"

namespace life {

// (...)

bool LifeGeneration:perator == (const LifeGeneration & g) {
bool eq;
for (int m = 0; m < ROW_DIM; m++)
for (int n = 0; n < COL_DIM; n++)
eq = generation[m][n] == g.generation[m][n];
return eq;
}

}

When I perform a compilation to obtain a module LifeHistory.o, I get
complaints:

$ g++ -c LifeHistory.cpp
LifeHistory.cpp: In member function `unsigned int
life::LifeHistory::isPeriodic()':
LifeHistory.cpp:31: error: no match for 'operator==' in '
this->life::LifeHistory::history[i] == this->life::LifeHistory::history'
LifeGeneration.h:28: error: candidates are: bool
life::LifeGeneration:perator==(const life::LifeGeneration&)

Resources I consulted are not very helpful because they contain little
discussion regarding the use of multiple source files. I must be missing
something very simple, but I can't figure out what it is. Could someone
give me a hint and/or constructive criticism? I'm grateful in advance.

Regards,
Gerard.
 
Reply With Quote
 
 
 
 
Markus Schoder
Guest
Posts: n/a
 
      06-17-2006
Gerard Kramer wrote:
> Hello,
>
> There is a slight problem with operator overloading in a program I
> attempt to start practising C++. It is a basic (not very original) game
> of life simulator. It uses two classes: LifeGeneration and LifeHistory.
> Declarations are given by:
>
> // Contents of file LifeGeneration.h
>
> #ifndef LIFEGENERATION_H
> #define LIFEGENERATION_H
>
> namespace life {
>
> class LifeGeneration {
>
> private:
>
> int generation[ROW_DIM][COL_DIM];
>
> unsigned mod(int, unsigned);
> unsigned int countNn(int, int);
> int alive(int, int);
>
> public:
>
> LifeGeneration();
> void setCell(int, int, int);
> void nextGeneration();
> bool operator == (const LifeGeneration &);
>
> };
>
> }
>
> #endif
>
> and:
>
> // Contents of file LifeHistory.h
>
> #ifndef LIFEHISTORY_H
> #define LIFEHISTORY_H
>
> namespace life {
>
> class LifeHistory {
>
> private:
>
> LifeGeneration history[MAX_GENERATIONS];
> unsigned n; // Generation counter
>
> public:
>
> LifeHistory();
> unsigned storeGeneration(LifeGeneration);
> unsigned getLength();
> unsigned isPeriodic();
>
> };
>
> }
>
> #endif
>
> There is a problem with the following implementation:
>
> // Contents of file LifeHistory.cpp
>
> #include "parameters.h" // Contains definitions of constants
> #include "LifeGeneration.h"
> #include "LifeHistory.h"
>
> namespace life {
>
> // (...)
>
> unsigned LifeHistory::isPeriodic() {
> // Tests whether evolution is periodic with period > 0.
> // Returns zero for a-periodicity.
> for (unsigned i = 0; i < n; i++)
> if (history[i] == history)


history is an array of LifeGeneration objects but you need a plain
LifeGeneration object. It should probably be something like

if (history[i] == history[n])

generally speaking the second operand should be your current generation.

> return i;


This can return 0 so either return say i+1 here or choose for instance
UINT_MAX to indicate a-periodicity.

> return 0;
> }
>
> }
>
> The test-for-equality operator used in the isPeriodic() function is
> defined as:
>
> // Contents of file LifeGeneration.cpp
>
> #include "parameters.h"
> #include "LifeGeneration.h"
>
> namespace life {
>
> // (...)
>
> bool LifeGeneration:perator == (const LifeGeneration & g) {
> bool eq;
> for (int m = 0; m < ROW_DIM; m++)
> for (int n = 0; n < COL_DIM; n++)
> eq = generation[m][n] == g.generation[m][n];
> return eq;
> }
> }


This function looks odd since eq captures just the result of the very last
comparison. You probably want

bool LifeGeneration:perator == (const LifeGeneration & g) {
for (int m = 0; m < ROW_DIM; m++)
for (int n = 0; n < COL_DIM; n++)
if (generation[m][n] != g.generation[m][n])
return false;
return true;
}

 
Reply With Quote
 
 
 
 
Gerard Kramer
Guest
Posts: n/a
 
      06-17-2006
Thank you for your to-the-point reply. I made the stupid mistake of
assuming that history is equivalent to history[0] while in fact it is
equivalent to &history[0].

The other remarks were also very right.

Regards,
G.

Markus Schoder wrote:
> Gerard Kramer wrote:
>> Hello,
>>
>> There is a slight problem with operator overloading in a program I
>> attempt to start practising C++. It is a basic (not very original) game
>> of life simulator. It uses two classes: LifeGeneration and LifeHistory.
>> Declarations are given by:
>>
>> // Contents of file LifeGeneration.h
>>
>> #ifndef LIFEGENERATION_H
>> #define LIFEGENERATION_H
>>
>> namespace life {
>>
>> class LifeGeneration {
>>
>> private:
>>
>> int generation[ROW_DIM][COL_DIM];
>>
>> unsigned mod(int, unsigned);
>> unsigned int countNn(int, int);
>> int alive(int, int);
>>
>> public:
>>
>> LifeGeneration();
>> void setCell(int, int, int);
>> void nextGeneration();
>> bool operator == (const LifeGeneration &);
>>
>> };
>>
>> }
>>
>> #endif
>>
>> and:
>>
>> // Contents of file LifeHistory.h
>>
>> #ifndef LIFEHISTORY_H
>> #define LIFEHISTORY_H
>>
>> namespace life {
>>
>> class LifeHistory {
>>
>> private:
>>
>> LifeGeneration history[MAX_GENERATIONS];
>> unsigned n; // Generation counter
>>
>> public:
>>
>> LifeHistory();
>> unsigned storeGeneration(LifeGeneration);
>> unsigned getLength();
>> unsigned isPeriodic();
>>
>> };
>>
>> }
>>
>> #endif
>>
>> There is a problem with the following implementation:
>>
>> // Contents of file LifeHistory.cpp
>>
>> #include "parameters.h" // Contains definitions of constants
>> #include "LifeGeneration.h"
>> #include "LifeHistory.h"
>>
>> namespace life {
>>
>> // (...)
>>
>> unsigned LifeHistory::isPeriodic() {
>> // Tests whether evolution is periodic with period > 0.
>> // Returns zero for a-periodicity.
>> for (unsigned i = 0; i < n; i++)
>> if (history[i] == history)

>
> history is an array of LifeGeneration objects but you need a plain
> LifeGeneration object. It should probably be something like
>
> if (history[i] == history[n])
>
> generally speaking the second operand should be your current generation.
>
>> return i;

>
> This can return 0 so either return say i+1 here or choose for instance
> UINT_MAX to indicate a-periodicity.
>
>> return 0;
>> }
>>
>> }
>>
>> The test-for-equality operator used in the isPeriodic() function is
>> defined as:
>>
>> // Contents of file LifeGeneration.cpp
>>
>> #include "parameters.h"
>> #include "LifeGeneration.h"
>>
>> namespace life {
>>
>> // (...)
>>
>> bool LifeGeneration:perator == (const LifeGeneration & g) {
>> bool eq;
>> for (int m = 0; m < ROW_DIM; m++)
>> for (int n = 0; n < COL_DIM; n++)
>> eq = generation[m][n] == g.generation[m][n];
>> return eq;
>> }
>> }

>
> This function looks odd since eq captures just the result of the very last
> comparison. You probably want
>
> bool LifeGeneration:perator == (const LifeGeneration & g) {
> for (int m = 0; m < ROW_DIM; m++)
> for (int n = 0; n < COL_DIM; n++)
> if (generation[m][n] != g.generation[m][n])
> return false;
> return true;
> }
>

 
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
How to avoid multiple definition of a variable by multiple inclusion of a header file lars.uffmann@rwth-aachen.de C++ 11 08-05-2006 10:24 PM
Separating a class definition into header and source files rajkirangrandhi@gmail.com C++ 4 03-18-2005 03:30 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
Beginner Help: Joining Multiple classes in multiple files? JHenstay C++ 3 01-11-2004 02:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57