【问题标题】:When I initialize a class, and cout it, it just shows the default constructor [closed]当我初始化一个类并计算它时,它只显示默认构造函数[关闭]
【发布时间】:2021-12-30 03:13:14
【问题描述】:

我在练习代码 oop 时遇到了一个问题。 我的 'class vdv' 是一名运动员,有一个默认构造函数,其中包含“姓名”、“运动”、0 年龄、0 身高和 0 体重。 我也为它编写了运算符重载 cout。 但是当 i cin >> my obj 然后 cout 它时,它只显示默认值“”名称,“”运动,0 年龄,0 高度和 0重量。

这是我的代码

#include <string>
using namespace std;
#include "VanDongVien.h"

//------------------------------------------

#pragma once
class vdv {
private:
    int age;
    string name, sport;
    double weight, height;
public:
    vdv() {
        name = sport = "No name";
        age = 0;
        weight = height = 0;
    }
    vdv(string _name, string _sport, int _age, double _height, double _weight) :
        name(_name), sport(_sport), age(_age), height(_height), weight(_weight) {};
    ~vdv() {
        name = sport = " ";
        age = 0;
        height = weight = 0;
    }
    friend istream& operator>>(istream &is, vdv obj);
    friend ostream& operator<<(ostream &os, vdv u);
    bool operator>(vdv obj);
};

//------------------------------------------

istream &operator>>(istream &is, vdv obj) {

    cout << "Nhap ho va ten: "; fflush(stdin); getline(is, obj.name);
    cout << "Nhap mon thi dau: "; fflush(stdin); getline(is, obj.sport);
    cout << "Nhap tuoi: "; cin >> obj.age;
    cout << "Nhap chieu cao: "; cin >> obj.height;
    cout << "Nhap can nang: "; cin >> obj.weight;
    return is;
}
ostream &operator<< (ostream & os, vdv obj) {
    cout << "Ho va ten: " << obj.name << endl;
    cout << "Mon thi dau: " << obj.sport << endl;
    cout << "Tuoi: " << obj.age << endl;
    cout << "Chieu cao: " << obj.height << endl;
    cout << "Can nang: " << obj.weight << endl;`enter code here`
    return os;
}
bool vdv::operator>(vdv obj) {
    if (height > obj.height) return true;
    else if (height < obj.height) return false;
    else if (weight >= obj.weight) return true;
    else return false;
}
void swap(vdv &o1, vdv &o2) {
    vdv temp = o1;
    o1 = o2;
    o2 = temp;
}
void bubblesort(vdv *a, int n) {
    for (int i = 0; i < n - 1; i++)
        for (int j = 1; j < n; j++) {
            if (a[i] > a[j]) swap(a[i], a[j]);
        }
}

//------------------------------------------

int main() {
    vdv o1;
    cin >> o1;
    cout << o1;
    system("pause");
}

vdv(Van dong vien 是运动员)。

感谢您阅读我的代码并提供帮助。

【问题讨论】:

  • 您正在通过值传递对象,因此在operator&gt;&gt; 中完成的工作是在原始对象的副本上完成的。您想改为传递参考。 stackoverflow.com/a/4421719/920069
  • 不仅适用于operator &gt;&gt;,而且您在大多数函数中通过值传递这些对象,而这些对象应该通过引用传递。喜欢这里:bool vdv::operator&gt;(vdv obj)obj 应该通过 const 引用传递。顺便说一句,~vdv() 不是必需的——它可以声明为=default; 或完全删除。
  • 这里有 许多 项在概念上是错误的。特别是使用operator&gt;&gt;输入时,不能合理地提示输入;编写operator&gt;&gt;全部要点 是输入可能来自任何流,而不仅仅是标准输入——因此可能没有人需要提示。同样,您编写operator&gt;&gt; 的方式忽略了它应该读取的流,并且总是尝试从std::cin 读取。这违背了目的。对于 operator&lt;&lt; 使用 std::cout 也是如此。
  • 欢迎来到 Stack Overflow。正确修复此代码所需的帮助超出了 Stack Overflow 问题的范围。您应该尝试使用真正的 讨论论坛,例如 Reddit 或 Quora;或与您的导师交谈。
  • 对不起:(我只是编码新手

标签: c++


【解决方案1】:

重载operator&lt;&lt;operator&gt;&gt; 时,您应该将第二个参数作为reference 传递,如下所示。我已经在我进行修改的地方添加了 cmets。

class vdv {
private:
    int age;
    std::string name, sport;
    double weight, height;
public:
    vdv() {
        name = sport = "No name";
        age = 0;
        weight = height = 0;
    }
    vdv(std::string _name, std::string _sport, int _age, double _height, double _weight) :
        name(_name), sport(_sport), age(_age), height(_height), weight(_weight) {};
    ~vdv() {
        name = sport = " ";
        age = 0;
        height = weight = 0;
    }
    friend std::istream& operator>>(std::istream &is, vdv &obj);//passed second object by reference
    friend std::ostream& operator<<(std::ostream &os, const vdv &u);//passed second object as a referece to const
    bool operator>(vdv obj);
};


std::istream &operator>>(std::istream &is, vdv &obj) {//added & on second argument

    is >> obj.name >> obj.sport >> obj.age >> obj.height >> obj.weight;
    
    //no need for cout here 
    
    //check if input succeded 
    if(is)
    {
        ;//do something here if you need to 
    }
    //otherwise leave the objec in its default state
    else 
    {
        obj = vdv();
    }
    
    
    return is;
}
std::ostream &operator<< (std::ostream & os, const vdv &obj) {//passed 2nd argument as reference to const
    os << obj.name << obj.sport << obj.age << obj.height << obj.weight;
    
    //no need for cout here
    
    return os;
}
bool vdv::operator>(vdv obj) {
    if (height > obj.height) return true;
    else if (height < obj.height) return false;
    else if (weight >= obj.weight) return true;
    else return false;
}

程序的输出可见here

我所做的一些修改包括:

  1. 第二个对象通过引用传递给operator&lt;&lt;operator&gt;&gt;
  2. 添加了检查operator&gt;&gt; 内的输入是否成功。
  3. operator&lt;&lt;operator&gt;&gt; 中删除了不必要的cout 语句。
  4. 删除了推荐做法的 using namespace std;

【讨论】:

    【解决方案2】:

    我认为您需要像这样在运算符中传递 vdv 作为参考>>:

    istream &operator>>(istream &is, vdv& obj) {
    
        cout << "Nhap ho va ten: "; fflush(stdin); getline(is, obj.name);
        cout << "Nhap mon thi dau: "; fflush(stdin); getline(is, obj.sport);
        cout << "Nhap tuoi: "; cin >> obj.age;
        cout << "Nhap chieu cao: "; cin >> obj.height;
        cout << "Nhap can nang: "; cin >> obj.weight;
        return is;
    }
    

    当您编写 cin &gt;&gt; o1 时,它将表现为 operator&gt;&gt;(cin, o1) 但您通过值传递 o1 因此在 operator&gt;&gt; 函数中,它会创建另一个副本,该副本与您尝试读取的副本无关。

    【讨论】:

    • 我修好了,但还是不行 :( 和你的一模一样
    • 它仍然可以:Ho va 10:没有名字 Mon thi dau:没有名字
    猜你喜欢
    • 2020-05-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 2011-01-25
    相关资源
    最近更新 更多