【问题标题】:How do return and display multiple values using class templates?如何使用类模板返回和显示多个值?
【发布时间】:2020-08-31 22:23:13
【问题描述】:

不能使用向量

我正在为学校错误编写程序,我无法修复最后一个错误。我尝试在要求用户输入 3 个值之前和之后显示这些值。他们没有正确显示。我有一个类模板,可用于创建一个包含 3 个成员变量的类。名为ThreeValues 的类接受3 个相同数据类型的项目(3 个整数、3 个双精度、3 个字符和3 个浮点数)。我知道我在 .h 文件中写错了函数getAll。我发布了 .h 文件,getAll 函数是最后一个函数(在底部)。这是我在 .h 文件中的内容:

template <class elemType>
class ThreeValues
{
public:
    ThreeValues(elemType val1, elemType val2, elemType val3); //sets values to 0
    ThreeValues() {};
    void setFirst(elemType a); 
    elemType getFirst() const;
    void setSecond(elemType b);
    elemType getSecond() const;
    void setThird(elemType c);
    elemType getThird() const;
    void getAll(elemType &a, elemType &b, elemType &c); //supposed to return all 3 values but I can't get it to work
    elemType getMin(const elemType a, const elemType b, const elemType c);
    elemType calcSum(elemType a, elemType b, elemType c) const;
    void display(ThreeValues <elemType> &myVals);

private:
    elemType val1;
    elemType val2;
    elemType val3;
};

#endif

template <class elemType>
ThreeValues<elemType>::ThreeValues(elemType val1, elemType val2, elemType val3)
{
    val1 = 0;
    val2 = 0;
    val3 = 0;
}

template <class elemType>
void ThreeValues<elemType>::setFirst(elemType a)
{
    val1 = a;
}

template <class elemType>
elemType ThreeValues<elemType>::getFirst() const
{
    return val1;
}

template <class elemType>
void ThreeValues<elemType>::setSecond(elemType b)
{
    val2 = b;
}

template <class elemType>
elemType ThreeValues<elemType>::getSecond() const
{
    return val2;
}

template <class elemType>
void ThreeValues<elemType>::setThird(elemType c)
{
    val3 = c;
}

template <class elemType>
elemType ThreeValues<elemType>::getThird() const
{
    return val3;
}

template <class elemType>
elemType ThreeValues<elemType>::getMin(const elemType a, const elemType b, const elemType c)
{
    elemType min;

    if (a < b && a < b)
        min = a;
    else if (b < a && b < c)
        min = b;
    else
        min = c;
    return min;
}

template <class elemType>
elemType ThreeValues<elemType>::calcSum(elemType a, elemType b, elemType c) const
{
    elemType sum;

    sum = a + b + c;
    return sum;
}

template <class elemType>
void ThreeValues<elemType>::getAll(elemType &a, elemType &b, elemType &c) 
{
    val1 = a;  //this function won't display the values
    val2 = b;
    val3 = c;
}

现在老师说要把display函数放到main.cpp文件中。在主文件中,我实例化了一个 ThreeValues 来保存 3 个 int 类型的值,然后是 double,然后是 char,然后是 float。在我实例化每个之后,我调用显示函数。在display 函数中 I 1) 显示所有带有getAll 成员函数的初始化值。 2)使用setter函数设置三个值作为用户输入。 3) 再次使用getAll 显示修改后的值。 4) 致电calcSum 和getMin。除了getAll 函数外,我的代码中的所有内容似乎都运行良好。我也可能说错了,但我没有尝试过任何工作。这是我在main 中的内容:

template <class elemType>
void display(ThreeValues <elemType> &myVals);

int main()
{   
    ThreeValues<int> intValue1;
    intValue1.display(intValue1);

    ThreeValues<double> doubleValue2;
    doubleValue2.display(doubleValue2);


    ThreeValues<char> charValue3;
    charValue3.display(charValue3);

    ThreeValues<float> floatValue4;
    floatValue4.display(floatValue4);
    
    return 0;
}

template <class elemType>
void ThreeValues<elemType>::display(ThreeValues<elemType> &myVals)
{
    elemType val1,
        val2,
        val3;

    myVals.getAll(val1, val2, val3);  
    cout << "Initially, the three values are initialized as: " << val1 << " " << val2 << " " << val3 << endl;  
    cout << "Enter 3 " << typeid(val1).name() << ": ";
    cin >> val1 >> val2 >> val3;
    myVals.setFirst(val1);
    myVals.setSecond(val2);
    myVals.setThird(val3);
    cout << "After modification, the three values are: " << val1 << " " << val2 << " " << val3 << endl;
    cout << "The sum is: " << myVals.calcSum(val1, val2, val3) << endl;
    cout << "The minimun value is: " << myVals.getMin(val1, val2, val3) << endl;
    cout << "_________________________________________________________________\n";
//I'm supposed to use getAll to display the values before and after they are modified but since the function is type void I don't know how to call the function so it displays the right values
//it also still displays gibberish instead of the values entered by the user

}

输出如下:

Initially, the three values are initialized as: -858993460 -858993460 -858993460
Enter 3 int: 5 8 1
After modification, the three values are: 5 8 1
The sum is: 14
The minimun value is: 5
_________________________________________________________________
Initially, the three values are initialized as: -9.25596e+61 -9.25596e+61 -9.25596e+61
Enter 3 double: 5.6 8.4 3.4
After modification, the three values are: 5.6 8.4 3.4
The sum is: 17.4
The minimun value is: 5.6
_________________________________________________________________
Initially, the three values are initialized as: ╠ ╠ ╠
Enter 3 char: a b d
After modification, the three values are: a b d
The sum is: '
The minimun value is: a
_________________________________________________________________
Initially, the three values are initialized as: -1.07374e+08 -1.07374e+08 -1.07374e+08
Enter 3 float: 123456 456.789 789.123
After modification, the three values are: 123456 456.789 789.123
The sum is: 124702
The minimun value is: 456.789
_________________________________________________________________

对不起,我知道这很多。如果有什么我可以做的,请告诉我。我不是最擅长编程的。

【问题讨论】:

  • 这个getAll 方法你有多少余地?按照定义,它只能返回一个值。如果你被允许修改返回类型,你有选择,但目前你没有理智的选择。
  • @user4581301 修改返回类型应该是
  • std::tuple&lt;elemType, elemType, elemType&gt; getAll() const;
  • @Eljay 我希望我可以,但我们不允许在此类中使用 std:: 或向量。他们只是想让事情变得尽可能困难
  • getAll 只需要返回void 而不是elemType;它已经通过引用参数“返回”了所有值。

标签: c++ class templates constructor


【解决方案1】:

据我了解,您必须返回多个值。所以我看到了几个解决方案:

#include <iostream>
#include <tuple>

template<typename T>
struct ThreeElements
{
    T a,b,c;

    struct return_structure{ T r1, r2, r3; };

    std::tuple<T, T, T> getAll() const
    {
        return {a, b, c};
    }

    return_structure getAll(bool) const
    {
        return { a, b, c };
    }

    void getAll(T* r1, T* r2, T* r3) const
    {
        *r1 = a;
        *r2 = b;
        *r3 = c;
    }

};

template<typename T>
void display_3_numbers(const T& _a, const T& _b, const T& _c)
{
    std::cout << "a = " << _a << std::endl;
    std::cout << "b = " << _b << std::endl;
    std::cout << "c = " << _c << std::endl;
    std::cout << " ### ### ### ### ###" << std::endl;
}

int main()
{
    ThreeElements<int> te{ 10, 20, 30};

    // #1 use std::tuple (C++17)
    auto [ a1, a2, a3 ] = te.getAll();

    // #2 use struct for return
    const auto ret_struct = te.getAll(true);
    
    // #3 use pointers (or references)
    int b1, b2, b3;
    te.getAll(&b1, &b2, &b3);

    display_3_numbers(a1, a2, a3); // # 1
    display_3_numbers(ret_struct.r1, ret_struct.r2, ret_struct.r3); // # 2
    display_3_numbers(b1, b2, b3); // # 3
}

命令:c++ -o main main.cpp -std=c++17 &amp;&amp; ./main

输出:

a = 10
b = 20
c = 30
 ### ### ### ### ###
a = 10
b = 20
c = 30
 ### ### ### ### ###
a = 10
b = 20
c = 30
 ### ### ### ### ###

【讨论】:

    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多