A
Alan Lee
Hi i am writing a small simulation for a bunch of atoms jumping around on a
surface. I know i am a crappy programmer since i am not very familiar with
object oriented languages. I am woindering if anyone can tell me why my
forward class declarations not working.
the member function findpaths can't seem to access the class Board
when I try to pass it a board object by value.
I put the Board class latrerbut also put in a forward declaration before
the ATOM class.
please someone tell me why the memberfunction in class atom still can't see
the board class. I made them friends of each other also. i am going crazy
trying to figure it out. thanks. here
is the relevant portions of the code:
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
using std::left;
#include <iomanip>
using std::setw;
using std::setprecision;
#include <cstdlib> // contains function protype for rand
#include <stdlib.h> // for pausing function
// class Atom; // forward declaration
#include <vector>
using std::vector;
const int Boardsize=4;
class Board;
class Atom {
friend class Board;
public:
Atom(int,int); // constructor
void findpaths(Board b);
private:
int rcoord;
int ccoord;
int oldrcoord;
int oldccoord;
};
Atom::Atom(int r,int c)
{
rcoord = r;
ccoord = c;
}
void Atom::findpaths(Board b)
{
int northr,southr,westr,eastr;
int northc,southc,westc,eastc;
//define coordinates of neighbors taking into account atoms on edge of
board
//north
if (rcoord==0){
northr=Boardsize-1;}
else {
northr=rcoord-1;
}
northc=ccoord;
//south
if (rcoord==Boardsize-1){
southr=0;}
else {
southr=rcoord + 1;
}
southc=ccoord;
//east
if (ccoord==Boardsize-1){
eastc=0;}
else {
eastc=ccoord+1;
}
eastr=rcoord;
//west
if (ccoord==0){
westc=Boardsize-1;}
else {
westc=ccoord-1;
}
westr=rcoord;
}
class Board {
friend class Atom;
public:
Board();
int getneighbors( int r, int c);
void drawboard(); // draws board
void placeatom(int,int); // places atom on board
void update(Atom a);
private:
int boardarray[Boardsize][Boardsize];
};
Board::Board()
{
for (int i= 0; i < Boardsize; i++)
for (int j=0; j < Boardsize; j++)
{ boardarray[j] = 0; // initalizes board to 0
// cout << boardarray[j];
}
}
void Board::update(Atom a)
{
// int row,column, oldrow, oldcolumn;
// oldrow=(a.oldycoord)-1;
// oldcolumn=(a.oldxcoord)-1;
// row=a.ycoord - 1;
// column= a.xcoord -1;
// note that this is the relationship
// between boardarray coordinates
// and cartesian
boardarray[a.oldrcoord][a.oldccoord] = 0; // set old position to 0
boardarray[a.rcoord][a.ccoord] = 1; // set new position to 1
cout << "old position" << a.oldrcoord << a.oldccoord << "\n";
cout << "new position" << a.rcoord<< a.ccoord<<"\n";
}
void Board::drawboard()
{
// boardarray[1][3] = 1;
for (int i= 0; i < Boardsize; i++)
{ cout << "\n" ;
for (int j=0; j < Boardsize; j++)
cout << setw(2) << boardarray[j];
}
}
void Board:laceatom( int x, int y )
{
boardarray[x][y] = 1; //again, this is board coordinates
//we have to convert from cartesian
}
int Board::getneighbors( int r, int c)
{
// works fine
int totalneighbors=0;
int northr,southr,westr,eastr;
int northc,southc,westc,eastc;
//define coordinates of neighbors taking into account atoms on edge of board
//if space where neighbor check is done occupied, then return 99 othewise
returns
//number of nearest neighbors
if (boardarray[r][c]==1){
totalneighbors=99;}
else{
//north
if (r==0){
northr=Boardsize-1;}
else {
northr=r-1;
}
northc=c;
//south
if (r==Boardsize-1){
southr=0;}
else {
southr=r + 1;
}
southc=c;
//east
if (c==Boardsize-1){
eastc=0;}
else {
eastc=c+1;
}
eastr=r;
//west
if (c==0){
westc=Boardsize-1;}
else {
westc=c-1;
}
westr=r;
// tallies total neighbors
if (boardarray[northr][northc]==1){ // if there is an atom north add 1 to
neighbor
totalneighbors++;
}
if (boardarray[southr][southc]==1){
totalneighbors++;
}
if (boardarray[eastr][eastc]==1){
totalneighbors++;
}
if (boardarray[westr][westc]==1){
totalneighbors++;
}
}
return totalneighbors;
}
surface. I know i am a crappy programmer since i am not very familiar with
object oriented languages. I am woindering if anyone can tell me why my
forward class declarations not working.
the member function findpaths can't seem to access the class Board
when I try to pass it a board object by value.
I put the Board class latrerbut also put in a forward declaration before
the ATOM class.
please someone tell me why the memberfunction in class atom still can't see
the board class. I made them friends of each other also. i am going crazy
trying to figure it out. thanks. here
is the relevant portions of the code:
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
using std::left;
#include <iomanip>
using std::setw;
using std::setprecision;
#include <cstdlib> // contains function protype for rand
#include <stdlib.h> // for pausing function
// class Atom; // forward declaration
#include <vector>
using std::vector;
const int Boardsize=4;
class Board;
class Atom {
friend class Board;
public:
Atom(int,int); // constructor
void findpaths(Board b);
private:
int rcoord;
int ccoord;
int oldrcoord;
int oldccoord;
};
Atom::Atom(int r,int c)
{
rcoord = r;
ccoord = c;
}
void Atom::findpaths(Board b)
{
int northr,southr,westr,eastr;
int northc,southc,westc,eastc;
//define coordinates of neighbors taking into account atoms on edge of
board
//north
if (rcoord==0){
northr=Boardsize-1;}
else {
northr=rcoord-1;
}
northc=ccoord;
//south
if (rcoord==Boardsize-1){
southr=0;}
else {
southr=rcoord + 1;
}
southc=ccoord;
//east
if (ccoord==Boardsize-1){
eastc=0;}
else {
eastc=ccoord+1;
}
eastr=rcoord;
//west
if (ccoord==0){
westc=Boardsize-1;}
else {
westc=ccoord-1;
}
westr=rcoord;
}
class Board {
friend class Atom;
public:
Board();
int getneighbors( int r, int c);
void drawboard(); // draws board
void placeatom(int,int); // places atom on board
void update(Atom a);
private:
int boardarray[Boardsize][Boardsize];
};
Board::Board()
{
for (int i= 0; i < Boardsize; i++)
for (int j=0; j < Boardsize; j++)
{ boardarray[j] = 0; // initalizes board to 0
// cout << boardarray[j];
}
}
void Board::update(Atom a)
{
// int row,column, oldrow, oldcolumn;
// oldrow=(a.oldycoord)-1;
// oldcolumn=(a.oldxcoord)-1;
// row=a.ycoord - 1;
// column= a.xcoord -1;
// note that this is the relationship
// between boardarray coordinates
// and cartesian
boardarray[a.oldrcoord][a.oldccoord] = 0; // set old position to 0
boardarray[a.rcoord][a.ccoord] = 1; // set new position to 1
cout << "old position" << a.oldrcoord << a.oldccoord << "\n";
cout << "new position" << a.rcoord<< a.ccoord<<"\n";
}
void Board::drawboard()
{
// boardarray[1][3] = 1;
for (int i= 0; i < Boardsize; i++)
{ cout << "\n" ;
for (int j=0; j < Boardsize; j++)
cout << setw(2) << boardarray[j];
}
}
void Board:laceatom( int x, int y )
{
boardarray[x][y] = 1; //again, this is board coordinates
//we have to convert from cartesian
}
int Board::getneighbors( int r, int c)
{
// works fine
int totalneighbors=0;
int northr,southr,westr,eastr;
int northc,southc,westc,eastc;
//define coordinates of neighbors taking into account atoms on edge of board
//if space where neighbor check is done occupied, then return 99 othewise
returns
//number of nearest neighbors
if (boardarray[r][c]==1){
totalneighbors=99;}
else{
//north
if (r==0){
northr=Boardsize-1;}
else {
northr=r-1;
}
northc=c;
//south
if (r==Boardsize-1){
southr=0;}
else {
southr=r + 1;
}
southc=c;
//east
if (c==Boardsize-1){
eastc=0;}
else {
eastc=c+1;
}
eastr=r;
//west
if (c==0){
westc=Boardsize-1;}
else {
westc=c-1;
}
westr=r;
// tallies total neighbors
if (boardarray[northr][northc]==1){ // if there is an atom north add 1 to
neighbor
totalneighbors++;
}
if (boardarray[southr][southc]==1){
totalneighbors++;
}
if (boardarray[eastr][eastc]==1){
totalneighbors++;
}
if (boardarray[westr][westc]==1){
totalneighbors++;
}
}
return totalneighbors;
}