2012-06-27 3 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

슬래시 마이어에게 감사드립니다. 선생님이 헤더의 일부를 주셨습니다. 우리는 함수 파이프를 초기화하고 배열을 작성해야합니다. 소스 코드에서 함수를 이동할 수 없습니다. BTW 난 그냥 온도 및 기호 측면을 전환하고 여전히 작동하지 않습니다. 내가 얻은 것은 기능의 주소 = 002e8728 – lolo1116

+0

Slashmais에게 다시 한번 감사드립니다. 나는 당신이 말한 것을했습니다. 그리고 지금은 더 좋아 보이고 있습니다. 문제는 'H'로 채워진 내부 직사각형을 얻지 못한다는 것입니다. 그러나 아픈 것은 내가 놓칠지도 모른 어떤 것을 계속 찾고 있습니다. – lolo1116

+0

@ lolo1116 : 그게 배우고, 코드를 가지고 놀며, 변화를주고, 어떤 일이 일어나는지를 보여줍니다. 지금부터 시작해야 할 아주 좋은 습관은 작업 코드를 가지고있을 때 코드를 변경하기 전에 백업하는 것입니다. 그렇게하면 변경 사항이 사라질 때 다시 복사 한 다음 다시 시도 할 수 있습니다. (그리고 나를 믿어 '때'아닌 '경우';) – slashmais

관련 문제