c++ - 在从字符串流读取和写入自定义对象时测试失败

标签 c++ operator-overloading ostream istream

没有语法错误,这不是 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);

像素.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;
}

像素.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

最佳答案

对于 assert(actual == correct);工作,两者string s 应该完全相同,这在您的情况下是不正确的。

所以,替换:

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之后的 s 可能会失败,因为当您调用 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;
}

而且我们可以清楚地看到 r gb在调用它们各自的 set 之前未分配任何值方法。

因此,只有垃圾值被存储在rvalbvalgvalpix .

这就是为什么 assert小号:

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 .

关于c++ - 在从字符串流读取和写入自定义对象时测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35354924/

相关文章:

c++ - 使用重载运算符时获取临时错误的地址

c++ - 执行用C++编写的.exe问题(使用mingw编译器)

kotlin - Kotlin 中究竟何时需要 operator 关键字?

c++ - 错误 : expected ‘;’ before ‘generationString’

c++ - C++ 如何确定重载运算符的参数?

c++ - 访问来自不同类的对象

c++ - `shuffle_order_engine` 是做什么用的?

java - 那么...Java支持运算符重载吗?

c++ - 重载的类型转换不起作用

c++ - 如何在 C++ 中编写 ofstream vector ,它接收所有不同的输出流,如 cout、字符串流和 ofstream