调用空函数问题
Calling void function problems
您好,我在代码中调用 void initialize() 函数时遇到问题。我应该得到一个看起来像这样的数组
//cccccccccccccccccccccccccc
cxxxxxxxxxxxxxxxxxxxxxxxxc
cxxxxxxxxxxxxxxxxxxxxxxxxc
cxxxxxxxxxxxxxxxxxxxxxxxxc
cxxxxxxxxxxxxxxxxxxxxxxxxc
cxxxxhhhhhhhxxxxxxxxxxxxxc
cxxxxhhhhhhhxxxxxxxxxxxxxc
cxxxxhhhhhhhxxxxxxxxxxxxxc
cxxxxhhhhhhhxxxxxxxxxxxxxc
cxxxxxxxxxxxxxxxxxxxxxxxxc
cccccccccccccccccccccccccc//
其中数组在 Initialize() 中定义,变量来自 FIN 、SteamPipe 和 GridPT 类。我的代码在数组之前给了我第一组答案,但是当它到达数组时,我只得到符号"?",这是它在类 GridPT 中指定的。我认为我调用 void 函数的方式是错误的还是传递变量有问题?我已经尝试取出 const 但仍然是一样的。这是我的代码.h文件
#include<iostream>
#include<istream>
#include<cmath>
#include<cstdlib>
using namespace std;
const int maxFinHeight = 100;
const int maxFinWidth = 100;
class Steampipe
{
private:
int height, width;
double steamTemp;
public:
Steampipe (int H = 0, int W = 0, double sT = 0.0)
{
height = H;
width = W;
steamTemp = sT;
}
// Access function definitions
int getWidth()const{return width;};
int getHeight()const{return height;};
double getSteamTemp()const{return steamTemp;};
void setWidth(int dummysteamwidth) {width = dummysteamwidth;};
void setHeight(int dummysteamheight){height = dummysteamheight;};
void setSteamTemp(double dummysteamtemp){steamTemp = dummysteamtemp;};
// Access function definitions
friend istream& operator>>(istream& , Steampipe& ); // YOU MUST DEFINE
friend ostream& operator<<(ostream& , const Steampipe& ); // YOU MUST DEFINE
};
class GridPt
{
private:
double temperature;
char symbol;
public:
GridPt(double t = 0.0, char s = '?')
{
temperature = t;
symbol = s;
}
// Access function definitions
double getTemperature()const {return temperature;};
char getsymbol() const {return symbol;};
void setTemperature (double T=0) {T=temperature;}
void setsymbol (char S='?'){S=symbol;}
};
class Fin
{
private:
int width, height; //width and height of fin
int pipeLocationX, pipeLocationY; //grid location of lower left corner of steampipe
double boundaryTemp; //temperature of fin surroundings
Steampipe Pipe; //steampipe object - COMPOSITION
GridPt GridArray[maxFinHeight][maxFinWidth]; // array of GridPts - COMPOSITION
public:
int getwidth() {return width;}
int getheight() {return height;}
int getpipeLocationX(){return pipeLocationX;}
int getpipeLocationY(){return pipeLocationX;}
double get_boundarytemp(){return boundaryTemp;}
void setwidth (int w){w=width;}
void setheight (int h){h=height;}
friend istream& operator>>(istream& , Fin& ); // YOU MUST DEFINE
friend ostream& operator<<(ostream& , const Fin& ); // YOU MUST DEFINE
void initialize(); // YOU MUST DEFINE
};
void Fin::initialize()
{
int j(0) ,i(0);
for ( i = 0; i < height; i++)
{
for ( j = 0; j < width; j++)
{
if ( j == pipeLocationX && i == pipeLocationY
&& i < Pipe.getHeight() + pipeLocationY
&& j < Pipe.getWidth() + pipeLocationX)
{
GridArray [j][i].setTemperature(Pipe.getSteamTemp());
GridArray [j][i].setsymbol('H');
}
else if (j == (width -1) || i == (height -1) || j == 0 || i == 0)
{
GridArray [j][i].setTemperature(boundaryTemp);
GridArray [j][i].setsymbol('C');
}
else
{
GridArray [j][i].setTemperature((Pipe.getSteamTemp() + boundaryTemp)/2);
GridArray [j][i].setsymbol('X');
}
}
}
}
istream &operator >> (istream & in, Fin& f)
{
int ws, hs;
double ts;
char comma;
cout << "Enter the height of the fin (integer) >>> ";
in >> f.height;
cout << "Enter the width of the fin (integer) >>> " ;
in >> f.width;
cout << "Enter the height of the streampipe (integer) >>> ";
in >>hs;
f.Pipe.setHeight(hs);
cout << "Enter the width of the steampipe (integer) >>> ";
in >> ws;
f.Pipe.setWidth(ws);
cout << "Enter coordinates of lower left corner of steampipe (X,Y) >>> ";
in >> f.pipeLocationX >> comma >> f.pipeLocationY;
cout << "Enter the steam temperature (floating point) >>> ";
in >> ts;
f.Pipe.setSteamTemp(ts);
cout << "Enter the temperature around the fin (floating point) >>> ";
in >> f.boundaryTemp;
return in;
}
ostream &operator << (ostream &stream, const Fin& t)
{
int i = 0;
int j = 0;
stream << "The width of the fin is " << t.width << endl;
stream << "The height of the fin is " << t.height << endl;
stream << "The outside temperature is " << t.boundaryTemp << endl;
stream << "The lower left corner of the steam pipe is at "
<< t.pipeLocationX << "," << t.pipeLocationY << endl;
stream << "The steampipe width is " << t.Pipe.getWidth() << endl;
stream << "the steampipe height is " << t.Pipe.getHeight() << endl;
stream << "The temperature of the steam is " << t.Pipe.getSteamTemp() << endl;
for ( i = 0; i < t.height; i++)
{
for ( j = 0; j < t.width; j++)
{
if (j == t.pipeLocationX && i == t.pipeLocationY && i < t.Pipe.getHeight() + t.pipeLocationY && j < t.Pipe.getWidth() + t.pipeLocationX)
{
t.GridArray [j][i].getTemperature();
stream<<t.GridArray [j][i].getsymbol();
}
else if (j == (t.width -1) || i == (t.height -1) || j == 0 || i == 0)
{
t.GridArray [j][i].getTemperature();
stream<<t.GridArray [j][i].getsymbol();
}
else
{
t.GridArray [j][i].getTemperature();
stream<<t.GridArray [j][i].getsymbol();
}
}stream<<endl;
}
int coorx(0),coory(0);char comma;
stream << "Enter the coordinates of the gridpoint of interest (X,Y) >>> "
<< endl;
cin>>coorx>>comma>>coory;
stream<<"The temperature at location ("<<coorx<<","<<coory<<") is "
<< t.GridArray << endl;
return stream;
}
我的驱动程序文件 cpp 是:
#include "pipe.h"
int main()
{
Fin one;
cin >> one;
one.initialize();
cout << one;
return 0;
}
任何建议将不胜感激。谢谢
在您的代码中,以下内容是错误的:
void setTemperature (double T=0) {T=temperature;}
void setsymbol (char S='?'){S=symbol;}
它必须是:
void setTemperature (double T=0) {temperature = T;}
void setsymbol (char S='?') { symbol = S;}
顺便说一句:将函数定义放在标头中不是一个好习惯(除非它是模板),而是将它们放在源文件中,标头中只有声明。
在代码中,您还可以直接在输出流中使用t.GridArray
:
stream<<t.GridArray;
这是行不通的。它不是一个函数,也不是一个流会理解的类型。使用已声明的 i 和 j 变量并单步执行数组的每个元素来打印它们,与初始化数组时相同。
相关文章:
- Visual Studio中的函数声明和函数定义问题
- C++quit()函数中可能存在作用域问题
- 类似于strcat()的函数出现问题
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 类C++中的函数问题(LNK2019和LNK1120错误)
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 关于简单C++函数(is_palindrome)的逻辑的问题
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- constexpr构造函数需要常量成员函数时出现问题
- 添加存储在向量中的大整数的函数出现问题
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 关于 c++ 函数中指针赋值的简单问题
- 如何在标头中声明(或定义)函数的问题
- 有关 c++ 构造函数的问题
- 模板类转换问题 - 无法推断调用的函数
- 从 C++ 中的函数返回数组地址问题
- 关于复制构造函数的一个棘手问题
- 移动赋值运算符;尝试引用已删除的函数.我该如何解决这个问题?
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题