2016-02-12 38 views
0

沒有語法錯誤,這不是Pixel_test.cpp和Pixel.cpp的完整代碼。從字符串流中讀取或寫入自定義對象時,測試失敗

我只有完整的頭文件。

失敗的測試是assert(actual == correct);

我在想,問題源於std::ostream&std::istream&中的Pixel.cpp文件。

我花了幾個小時,試圖明白爲什麼這個測試不起作用,所以我來這裏得到一些幫助。

Pixel_test.cpp:

stringstream in, out; 
    string correct; 
    Pixel pix; 
    pix.set(5,3,2); 
    correct = 5,3,2; 
    out << pix; 
    string actual = out.str(); 
    assert(actual == correct); 
    in << (6,4,6); 
    in >> pix; 
    assert(pix.getRed() == 6); 
    assert(pix.getBlue() == 6); 
    assert(pix.getGreen() == 4); 

Pixel.cpp:

std::ostream& operator<<(std::ostream& out, const Pixel& pix) 
{ 
    int r, g, b; 
    r = pix.getRed(); 
    g = pix.getGreen(); 
    b = pix.getBlue(); 
    out << r << g << b; 
    return(out); 
} 
std::istream& operator>>(std::istream& out, Pixel& pix) 
{ 
    int r, g, b; 
    pix.setRed(r); 
    pix.setGreen(g); 
    pix.setBlue(b); 
    out >> r >> g >> b; 
    return out; 
} 

Pixel.h:

#ifndef PIXEL_H 
#define PIXEL_H 

namespace imagelab 
{ 
    class Pixel 
    { 
    public: 
     Pixel(int r=0, int g=0, int b=0);  
     void set(int r, int g, int b); 
     void setRed(int r); 
     void setGreen(int g); 
     void setBlue(int b); 
     int getRed() const; 
     int getGreen() const; 
     int getBlue() const; 

    private: 
     int rval; // red 0-255 
     int gval; // green 0-255 
     int bval; // blue 0-255 
    }; 

    bool operator== (const Pixel& pix1, const Pixel& pix2); 
    bool operator!= (const Pixel& pix1, const Pixel& pix2); 
    std::ostream& operator<< (std::ostream& out, const Pixel& pix); 
    std::istream& operator>> (std::istream& in, Pixel& pix); 
} 

#endif 
+1

您是否向控制檯輸出了「實際」和「正確」(或通過調試器查看它們的值)以查看它們是什麼?除非'correct = 5,3,2;'在字符串文字周圍丟失了引號,否則將ASCII值爲2的字符賦值給字符串。 –

+0

什麼是變量'correct'的類型? –

回答

1

對於assert(actual == correct);工作,兩個string應該是完全一樣的,這不是你的情況。

所以,替換:

correct = 5,3,2; 

Pixel_test.cpp有:

correct = "5,3,2"; 

,並替換:

out << r << g << b; 

std::ostream& operator<<(std::ostream& out, Pixel& pix)有:

out << r << ',' << g << ',' << b; 

out << pix;呈現相同的輸出。

通過進行上述更改,您的assert(actual == correct);將停止失敗。

但是,在asserts年代後,可能會失敗,因爲,當你調用in>>pix;這個函數被調用:

std::istream& operator>>(std::istream& out, Pixel& pix) 
{ 
int r, g, b; 
pix.setRed(r); 
pix.setGreen(g); 
pix.setBlue(b); 
out >> r >> g >> b; 
return out; 
} 

我們可以清楚地看到,rgb沒有指定任何值,稱在他們各自的set方法。

因此,只有垃圾值獲取存儲在rvalbvalpixgval

這就是爲什麼assert S:

assert(pix.getRed() == 6); 
assert(pix.getBlue() == 6); 
assert(pix.getGreen() == 4); 

是註定要失敗。

編輯:

要解決這個問題,就需要讀取輸入,你剛投入流,入變量rgb

因此,改變你的std::istream& operator>>(std::istream& out, Pixel& pix)功能是這樣的:

std::istream& operator>>(std::istream& out, Pixel& pix) 
{ 
int r, g, b; 
out >> r >> g >> b; 
pix.setRed(r); 
pix.setGreen(g); 
pix.setBlue(b); 
return out; 
} 

而且取代:

in << (6,4,6); 

Pixel_test.cpp文件有:

in << "6 4 6"; 

因爲stringstream存儲數據爲string

+0

空隙像素:: setRed(INT R) \t { \t \t RVAL = R; \t} \t空隙像素:: setGreen(INT克) \t { \t \t GVAL =克; \t} \t空隙像素:: setBlue(INT B) \t { \t \t BVAL = B; \t} \t INT像素:: getRed()const的 \t { \t \t回報(rval中); \t} \t INT像素:: getGreen()const的 \t { \t \t回報(GVAL); \t} \t INT像素:: GetBlue進行()const的 \t { \t \t回報(BVAL); \t}這些是賦予r,g,b賦值的函數,但爲什麼最後三個斷言仍然不起作用 – terriyon

+0

@terriyon請始終添加用''括起來的代碼,以便在顯示時將其呈現爲代碼。這些函數是正確的,問題出在你使用的方式上:'in <<(6,4,6); in >> pix;'。我正在編輯我的答案以作解釋。請看看這個。 –

+0

@terriyon編輯完成。請看看帖子的結尾。 –

相關問題