需要帮助进行前瞻性声明 [英] NEED HELP with forward declarations

查看:82
本文介绍了需要帮助进行前瞻性声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一堆原子在

表面上跳来跳去做一个小模拟。我知道我是一个蹩脚的程序员因为我不太熟悉

面向对象的语言。如果有人能告诉我为什么我的

前瞻性声明无法正常工作,我很谦虚。


成员函数findpaths似乎无法访问类Board

当我尝试按价值传递一个棋盘对象时。


我把Board class latrer也放在了前面的声明中

ATOM课程。


请有人告诉我为什么类原子中的成员函数仍然看不到

板类。我也成了彼此的朋友。我疯了

试图解决这个问题。谢谢。这里

是代码的相关部分:


#include< iostream>

使用std :: cout;

使用std :: endl;

使用std :: fixed;

使用std :: left;


#include< iomanip>


使用std :: setw;

使用std :: setprecision;


#include< cstdlib> //包含rand的函数原型

#include< stdlib.h> //用于暂停功能

//类Atom; //前向声明

#include< vector>

使用std :: vector;


const int Boardsize = 4;


班级董事会;


班级原子{

朋友班董事会;


public:

Atom(int,int); //构造函数

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;

//定义考虑原子边缘的邻居坐标

board

// north

if(rcoord == 0){

northr = Boardsize-1;}

else {

northr = rcoord-1;

}

northc = ccoord;

//南方

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;

}


班级董事会{

朋友班Atom; <公开:

Board();

int getneighbors(int r,int c);

void drawboard(); //绘制板

void placeatom(int,int); //将原子放在船上


无效更新(原子a);

私人:


int boardarray [ Boardsize] [Boardsize];

};

Board :: Board()

{

for(int i = 0; i< Boardsize; i ++)

for(int j = 0; j< Boardsize; j ++)

{boardarray [i] [j] = 0; //将董事会初始化为0

// cout<< boardarray [i] [j];


}

}


void Board :: update(Atom a )

{

// int row,column,oldrow,oldcolumn;


// oldrow =(a.oldycoord) -1;

// oldcolumn =(a.oldxcoord)-1;

//行= a.ycoord - 1;

/ / column = a.xcoord -1;


//请注意这是关系

//在boardarray坐标之间

//和笛卡儿


boardarray [a.oldrcoord] [a.oldccoord] = 0; //将旧位置设为0

boardarray [a.rcoord] [a.ccoord] = 1; //将新位置设为1

cout<< 旧位置 << a.oldrcoord<< a.oldccoord<< " \ n";

cout<< 新职位 << a.rcoord<< a.ccoord<<" \ n";

}


void Board :: drawboard()

{


// boardarray [1] [3] = 1;

for(int i = 0; i< Boardsize; i ++)

{cout<< " \\\
" ;

for(int j = 0; j< Boardsize; j ++)

cout<< setw(2)<< boardarray [i] [j];

}


}

void Board :: placeatom(int x,int y)

{

boardarray [x] [y] = 1; //再次,这是董事会坐标

//我们必须从笛卡儿转换

}


int Board :: getneighbors (int r,int c)

{

//工作正常

int totalneighbors = 0;

int northr,southr,westr,eastr;

int northc,southc,westc,eastc;


//定义考虑到邻居的坐标董事会边缘的原子

//如果邻居检查的空间被占用,则返回99其他

返回

//最近的数量邻居

if(boardarray [r] [c] == 1){

totalneighbors = 99;}

else {

//北

if(r == 0){

northr = Boardsize-1;}

else {

northr = r-1;

}

northc = c;

//南

if(r == Boardsize-1){

southr = 0;}

else {

southr = r + 1; < br $>
}

southc = c;

//东

if(c == Boardsize-1){

ea stc = 0;}

else {

eastc = c + 1;

}

eastr = r;

// west

if(c == 0){

westc = Boardsize-1;}

否则{

westc = c-1;

}

westr = r;

//计算总邻居


if(boardarray [northr] [northc] == 1){//如果有一个原子北加1到

邻居

totalneighbors ++;

}

if(boardarray [southr] [southc] == 1){

totalneighbors ++;

}

if(boardarray [eastr] [eastc] == 1){

totalneighbors ++;

}

if(boardarray [westr] [westc] == 1){

totalneighbors ++;

}

}


返回全部邻居;


}


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;

}


推荐答案

Alan Lee写道:
Alan Lee wrote:
我正在为一堆原子在表面上跳跃的原子写一个小模拟。我知道我是一个蹩脚的程序员,因为我不熟悉面向对象的语言。如果有人可以告诉我为什么我的前瞻性声明无法正常工作,我很谦虚。

成员函数findpaths似乎无法访问班级董事会
当我尝试按价值传递一个董事会对象。

我把董事会班级latrer但也在ATOM课程之前提出了一份前瞻声明。

请有人告诉我为什么类原子中的成员函数仍然无法看到董事会成员。我也成了彼此的朋友。我要去疯狂
试图搞清楚。谢谢。这里
是代码的相关部分:

#include< iostream>
使用std :: cout;
使用std :: endl;
使用std :: fixed;
使用std :: left;

#include< iomanip>

使用std :: setw;
使用std :: setprecision;

#include< cstdlib> //包含rand的函数原型
#include< stdlib.h> //用于暂停功能
//类Atom; //前向声明
#include< vector>
使用std :: vector;


类Board; <类Atom {
朋友类板;

公开:
Atom(int,int); //构造函数
void findpaths(Board b);


您按价值通过董事会,即作为副本。你只能使用

指针和对Board的引用,如果该类只是转发

声明。

私人:
int rcoord;
int ccoord;
int oldrcoord;
int oldccoord;

};
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写道:
Alan Lee wrote:
我正在为一堆原子跳来跳去写一个小模拟
在表面上。我知道我是一个蹩脚的程序员,因为我不熟悉面向对象的语言。如果有人可以告诉我为什么我的前瞻性声明无法正常工作,我很谦虚。

成员函数findpaths似乎无法访问班级董事会
当我尝试按价值传递一个董事会对象。

我把董事会班级latrer但也在ATOM课程之前提出了一份前瞻声明。

请有人告诉我为什么类原子中的成员函数仍然无法看到董事会成员。我也成了彼此的朋友。我要去疯狂
试图搞清楚。谢谢。这里
是代码的相关部分:

#include< iostream>
使用std :: cout;
使用std :: endl;
使用std :: fixed;
使用std :: left;

#include< iomanip>

使用std :: setw;
使用std :: setprecision;

#include< cstdlib> //包含rand的函数原型
#include< stdlib.h> //用于暂停功能
//类Atom; //前向声明
#include< vector>
使用std :: vector;


类Board; <类Atom {
朋友类板;

公开:
Atom(int,int); //构造函数
void findpaths(Board b);


您按价值通过董事会,即作为副本。你只能使用

指针和对Board的引用,如果该类只是转发

声明。

私人:
int rcoord;
int ccoord;
int oldrcoord;
int oldccoord;

};
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" <人***** @ stanford.edu>写道:
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.




因为在声明类的变量之前编译器想要

知道你班上有什么。在Atom类定义之后,只需将类Board的定义放在

之后就可以了。定义
带有Board或Atom类型参数的
成员函数应该在定义了这些类之后来到




再见,

Chris Dams



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


这篇关于需要帮助进行前瞻性声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆