2012-06-27 100 views
1

你好,我在我的代碼中調用了void initialize()函數有問題。我應該得到那個看起來像這樣調用void函數問題

//cccccccccccccccccccccccccc 
    cxxxxxxxxxxxxxxxxxxxxxxxxc 
    cxxxxxxxxxxxxxxxxxxxxxxxxc 
    cxxxxxxxxxxxxxxxxxxxxxxxxc 
    cxxxxxxxxxxxxxxxxxxxxxxxxc 
    cxxxxhhhhhhhxxxxxxxxxxxxxc 
    cxxxxhhhhhhhxxxxxxxxxxxxxc 
    cxxxxhhhhhhhxxxxxxxxxxxxxc 
    cxxxxhhhhhhhxxxxxxxxxxxxxc 
    cxxxxxxxxxxxxxxxxxxxxxxxxc 
    cccccccccccccccccccccccccc// 

,其中陣列在初始化(定義的數組)和變量來自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; 
} 

任何建議將非常讚賞。謝謝

+0

至少嘗試使用一些縮進的代碼並將其縮小到實際的問題。否則這太痛苦了。 – pmr

+0

是的,這裏有一大塊代碼,編輯它的可讀性似乎太高了!請簡要,格式良好的例子。 – Rook

回答

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;} 

順便說一句:這不是一個好習慣,把函數定義在頭(除非它是一個模板),而是把它們放在源文件中,只是在頭文件中聲明。
stream<<t.GridArray;
這是行不通的:

在你的代碼也在您的輸出流直接使用t.GridArray。 它不是一個函數,或者一個類型,ostream將理解。 使用已聲明的i和j變量,並逐步通過數組的每個元素來打印它們,與初始化數組時相同。

+0

謝謝Slashmais,老師給了我們頭部的一部分。我們應該初始化函數管道並創建數組,我們不能在源代碼中移動任何函數。順便說一句,我只是切換溫度和符號方面,仍然無法正常工作。我所得到的是函數地址= 002e8728 – lolo1116

+0

再次感謝你Slashmais,我做了你所說的,現在看起來好多了。唯一的問題是我沒有得到填充'H'的內部矩形。但我不斷尋找任何我可能會錯過的東西。 – lolo1116

+0

@ lolo1116:這就是學習的方式,玩代碼,做出改變,看看會發生什麼。你應該從現在開始做的一個非常好的習慣是當你有工作代碼時,在你改變它之前備份它。這樣,當您的更改觸發時,您可以將其複製回來,然後重試。 (並相信我是'何時'不是'如果';) – slashmais