2016-02-12 36 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编辑完成。请看看帖子的结尾。 –

相关问题