C++运算符>>重载问题: change object only once

标签 c++ stream operator-overloading constructor-overloading

我有以下 Vector3D 代码,出于某种原因,cin 只能更改对象值一次。你知道为什么吗? 我想这与“常量”定义有关,但我不确定。 添加代码(部分)和测试

// code:
class Vector3D
{
private:
    double _x, _y, _z;
public:
    Vector3D() : _x(0), _y(0), _z(0) {};
    Vector3D(double x, double y, double z) : _x(x), _y(y), _z(z) {};
    Vector3D(const double parm[DIMENSION]) : _x(parm[0]), _y(parm[1]), 
             _z(parm[2]) {};

    const Vector3D operator+(const Vector3D &other) const; 
    Vector3D &operator+=(const Vector3D &other);
    const double &operator[](int idx) const;
    double &operator[](int idx);
    friend ostream &operator<<(ostream &out, const Vector3D &c);
    friend istream &operator>>(istream &in, Vector3D &c);
};

const Vector3D Vector3D::operator+(const Vector3D &other) const {
    return Vector3D(*this) += other; 
}

Vector3D &Vector3D::operator+=(const Vector3D &other) {
    (*this)._x += other._x;
    (*this)._y += other._y;
    (*this)._z += other._z;
    return *this; 
}

const double &Vector3D::operator[](int idx) const {
    assert(idx >= 0 && idx < DIMENSION);
    switch (idx)
    {
        case 0:
            return _x;
        case 1:
            return _y;
        case 2:
            return _z;
        default:
            return _x; // not reachable
    }
}

double &Vector3D::operator[](int idx)
{
    assert(idx >= 0 && idx < DIMENSION);
    switch (idx)
    {
        case 0:
            return _x;
        case 1:
            return _y;
        case 2:
            return _z;
        default:
            return _x; // not reachable
    }
}

ostream &operator<<(ostream &out, const Vector3D &c)
{
    out << c._x << " " << c._y << " " << c._z;
    return out;
}

istream &operator>>(istream &in, Vector3D &c)
{
    in >> c._x >> c._y >> c._z;
    return in;
}



// test: 
void readFromStreamTest(int &tests) {
    std::stringstream os;
    os << 8.0 << " " << 3.0 << " " << 4.0;
    Vector3D a;
    os >> a;
    os << a; // "8 3 4"  as should be
    os.str(std::string());
    os << 2.1 << " " << 3.1 << " " << 4.1;
    os >> a;
    os.str(std::string());
    os << a;
    if (os.str()!=("2.1 3.1 4.1"))
    {
        std::cerr << "failed. should be 2.1 3.1 4.1 but was " << a;
    }
}

测试结果:“失败。应该是 2.1 3.1 4.1 但是是 8 3 4”(旧值!!)

最佳答案

在提取包含抛出 os >> 之后,您不能像那样使用 stringstream你不能再通过 os.str(value) 修改它也不os <<如果你不打电话 os.clear()之前,例如

void readFromStreamTest(int &tests) {
  std::stringstream os;
  os << 8.0 << " " << 3.0 << " " << 4.0;
  Vector3D a;
  os >> a;
  os.clear();
  os << a; // "8 3 4"  as should be
  os.str(std::string());
  os << 2.1 << " " << 3.1 << " " << 4.1;
  os >> a;
  os.str(std::string());
  os.clear();
  os << a;
  if (os.str()!=("2.1 3.1 4.1"))
  {
    std::cerr << "failed. should be 2.1 3.1 4.1 but was " << a;
  }
}

附言我修改了您的原始代码以使其可编译

关于C++运算符>>重载问题: change object only once,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53802195/

相关文章:

c++ - 在 C++ 中进行快速除法的好方法?

c++ - 如何在 Rcpp 中加快 xts 数据到 Datetime Vector 的转换?

c++ - 使用运算符重载添加两个数组对象会导致段错误

c++ - 在 C++ 中创建自定义可比树状数据结构

java - Android:从 url 查看视频流

c++ - 运算符重载问题

c++ - C++ 中的什么数据类型等同于 vb6 中的 const CURRENCY?

c++ - 对比 openCV 拉伸(stretch)图像

c - Padding - 加密算法

delphi - 如何在Delphi中播放资源中的mp3内容?