473,324 Members | 1,678 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,324 software developers and data experts.

user-type operator definition and multiple classes / source files

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::operator == (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::operator==(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.
Jun 17 '06 #1
2 1935
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::operator == (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::operator == (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;
}

Jun 17 '06 #2
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::operator == (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::operator == (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;
}

Jun 17 '06 #3

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

Similar topics

2
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
2
by: Jesper Stocholm | last post by:
I have implemented role-based security within my ASP.Net application. However, it seems the role is not passed to the authentication ticket I create. I want to use it to display/hide some...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
3
by: Jiho Han | last post by:
Should ASPNET user belong to the local Users group? I may have made some changes that affected my workstation setup and I am experiencing some unexpected behaviors. For example, I have my IIS...
1
by: Robert Howells | last post by:
Perhaps I'm just too new at this to pull it off, or perhaps it's just bad architecture. I'd appreciate some feedback on the the wisdom (or lack thereof) in attempting the following: I'm not new...
17
by: Alphonse Giambrone | last post by:
I am building a web app for users to add/edit data. They may add/edit several records during a session. When they are done (not necessarily immediately, could be 10 or more minutes later), I need...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.