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[i][j] = 0; // initalizes board to 0
// cout << boardarray[i][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[i][j];
}
}
void Board::placeatom( 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;
} 10 2187
Alan Lee wrote: 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);
You're passing the Board by value, i.e. as copy. You can only use
pointers and references to Board if that class is only forward
declared. private: int rcoord; int ccoord; int oldrcoord; int oldccoord;
};
....
Alan Lee wrote: 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);
You're passing the Board by value, i.e. as copy. You can only use
pointers and references to Board if that class is only forward
declared. private: int rcoord; int ccoord; int oldrcoord; int oldccoord;
};
....
Hi!
"Alan Lee" <al*****@stanford.edu> writes: 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.
Because before declaring a variable of a class the compiler wants to
know what is in your class. Just put the definition of class Board right
after the definition of class Atom and you should be fine. Definitions
of Member functions that take Board or Atom type arguments should come
after these classes have been defined.
Bye,
Chris Dams
Hi!
"Alan Lee" <al*****@stanford.edu> writes: 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.
Because before declaring a variable of a class the compiler wants to
know what is in your class. Just put the definition of class Board right
after the definition of class Atom and you should be fine. Definitions
of Member functions that take Board or Atom type arguments should come
after these classes have been defined.
Bye,
Chris Dams
thanks for the reply,
So by what you are saying, if I pass the board by reference, ie (Board
&b), everything should work fine?
if not, How would I get around this problem? thanks
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c4*************@news.t-online.com... Alan Lee wrote:
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);
You're passing the Board by value, i.e. as copy. You can only use pointers and references to Board if that class is only forward declared.
private: int rcoord; int ccoord; int oldrcoord; int oldccoord;
};
...
thanks for the reply,
So by what you are saying, if I pass the board by reference, ie (Board
&b), everything should work fine?
if not, How would I get around this problem? thanks
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:c4*************@news.t-online.com... Alan Lee wrote:
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);
You're passing the Board by value, i.e. as copy. You can only use pointers and references to Board if that class is only forward declared.
private: int rcoord; int ccoord; int oldrcoord; int oldccoord;
};
...
That worked great!
thanks for your help. I guess the memberfunctions need to know whats passed
to them. Is this why people put things in header files?
anyways, I am very thankful for your advice.
"Chris Dams" <ch****@gamow.sci.kun.nl> wrote in message
news:c4**********@gamow.sci.kun.nl... Hi!
"Alan Lee" <al*****@stanford.edu> writes:
Hi i am writing a small simulation for a bunch of atoms jumping around on
asurface. I know i am a crappy programmer since i am not very familiar
withobject oriented languages. I am woindering if anyone can tell me why my forward class declarations not working.
Because before declaring a variable of a class the compiler wants to know what is in your class. Just put the definition of class Board right after the definition of class Atom and you should be fine. Definitions of Member functions that take Board or Atom type arguments should come after these classes have been defined.
Bye, Chris Dams
That worked great!
thanks for your help. I guess the memberfunctions need to know whats passed
to them. Is this why people put things in header files?
anyways, I am very thankful for your advice.
"Chris Dams" <ch****@gamow.sci.kun.nl> wrote in message
news:c4**********@gamow.sci.kun.nl... Hi!
"Alan Lee" <al*****@stanford.edu> writes:
Hi i am writing a small simulation for a bunch of atoms jumping around on
asurface. I know i am a crappy programmer since i am not very familiar
withobject oriented languages. I am woindering if anyone can tell me why my forward class declarations not working.
Because before declaring a variable of a class the compiler wants to know what is in your class. Just put the definition of class Board right after the definition of class Atom and you should be fine. Definitions of Member functions that take Board or Atom type arguments should come after these classes have been defined.
Bye, Chris Dams
Please don't top-post.
Alan Lee wrote: >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. Because before declaring a variable of a class the compiler wants to know what is in your class. Just put the definition of class Board right after the definition of class Atom and you should be fine. Definitions of Member functions that take Board or Atom type arguments should come after these classes have been defined.
That worked great! thanks for your help. I guess the memberfunctions need to know whats passed to them.
Yes. If the object is passed by value, a local copy needs to be made for
the function, and then the compiler needs to know e.g. how big the
object is and if it is actually allowed to be copied. It can't get that
information from a forward declaration.
Is this why people put things in header files?
More or less, yes. If you pass by pointer or reference (which you should
actually prefer if you don't need it to be copied), the compiler
doesn't need to know any details about the class - only that it exists.
But later, where the funciton is implemented, you probably want to call
member functions on the object and things like that. Then the compiler
needs to know the full class definition. So you have to #include its
header in the file where your function implementation is written down.
Please don't top-post.
Alan Lee wrote: >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. Because before declaring a variable of a class the compiler wants to know what is in your class. Just put the definition of class Board right after the definition of class Atom and you should be fine. Definitions of Member functions that take Board or Atom type arguments should come after these classes have been defined.
That worked great! thanks for your help. I guess the memberfunctions need to know whats passed to them.
Yes. If the object is passed by value, a local copy needs to be made for
the function, and then the compiler needs to know e.g. how big the
object is and if it is actually allowed to be copied. It can't get that
information from a forward declaration.
Is this why people put things in header files?
More or less, yes. If you pass by pointer or reference (which you should
actually prefer if you don't need it to be copied), the compiler
doesn't need to know any details about the class - only that it exists.
But later, where the funciton is implemented, you probably want to call
member functions on the object and things like that. Then the compiler
needs to know the full class definition. So you have to #include its
header in the file where your function implementation is written down. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Aaron Anodide |
last post by:
I ran into a wierd problem today where a case of circular includes caused my
build to completely break. All my classes are spit out by a class wizard
with "#pragma once" in the header. I...
|
by: mjm |
last post by:
Folks,
Please help me with the following problems:
********************************************
1. I have a class template
template<class Base> class Matrix : public Base { /* .... */ }
|
by: Alan Lee |
last post by:
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...
|
by: vishnu |
last post by:
what is the exact difference between including a class header file and
forward declaration.
and Is there a case , where in forward declaration is not possible and
including is .
|
by: Steve |
last post by:
Can anyone explain why C++ .NET compiler throws error for the following code
segment:
// Forward declaration of ListViewItemComparer class
public __gc class ListViewItemComparer ;
public __gc...
|
by: fox |
last post by:
How do I (portably) make a forward reference to a static (I mean
file-scope) variable?
I've been using "extern" for years, for example:
extern int x;
int foo(void)
{
return x++;
}
|
by: NickC |
last post by:
I am using .NET version 2.0 and implementing all my event handlers to use the
new generic type. The problem I am running into is most of my Event Arguments
are defined in a single header which...
|
by: Hunk |
last post by:
Hi
I have a question on usage of forward declarations of templates. While
searching through this group , people suggested using forward
declarations and typedefs for templates as
// in...
|
by: Jess |
last post by:
Hello,
Sometimes declarations are all what we need when we define/declare
classes (or functions?), but sometimes we need definitions. I learned
that if we define a class (B) that has an object...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |