C++写的一个简单类模版 友元函数求最大最小值

发布于 2022-09-01 15:46:03 字数 1199 浏览 25 评论 0

如题 , 编译时总是说 [Error] ld returned 1 exit status (编译器dev c++)

`#include<iostream>
using namespace std;
template <typename t>
class CValue {
    t data[5];
    public:
        CValue() {
            cout<<"please input 5 numbers"<<endl;
            for(int i = 0; i < 5; i++)
                cin>>data[i];
}
        friend t Max(CValue <t> &a);
        friend t Min(CValue <t> &a);
}; 
template <typename t> t Max(CValue <t> &a) {
    t x = a.data[0];
    for(int i = 1; i < 5; i++)
        if(x < a.data[1]) x = a.data[1];
    return x;
}
template <typename t> t Min(CValue <t> &a) {
    t min = a.data[0];
    for(int i = 1; i < 5; i++)
        if(min > a.data[1]) min = a.data[1];
    return min;
}
int main() {
     cout<<"整数对象a,";
    CValue<int> a;
    cout<<"浮点数对象b,";
    CValue<float> b;
    cout<<"整数元素对象a的元素最大值为:"<<Max(a)<<endl;
    cout<<"整数元素对象a的元素最小值为:"<<Min(a)<<endl;
    cout<<"浮点数元素对象b的元素最大值为:"<<Max(b)<<endl;
    cout<<"浮点数元素对象b的元素最小值为:"<<Min(b)<<endl;

}
`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事还在继续 2022-09-08 15:46:03

你需要实例化模板函数friend t Max<>(CValue <t> &a)

c#include<iostream>
using namespace std;

template <typename T> class CValue;

template <typename T> T Max(CValue<T> &a) {
    T x = a.data[0];
    for(int i = 1; i < 5; i++)
        if(x < a.data[i]) x = a.data[i];
    return x;
}
template <typename T> T Min(CValue<T> &a) {
    T min = a.data[0];
    for(int i = 1; i < 5; i++)
        if(min > a.data[i]) min = a.data[i];
    return min;
}

template <typename T>
class CValue {
    T data[5];
public:
    CValue() {
        cout << "please input 5 numbers" << endl;
        for(int i = 0; i < 5; i++)
            cin>>data[i];
    }
    friend T Max<>(CValue<T> &a);
    friend T Min<>(CValue<T> &a);
};

int main() {
    cout << "整数对象a,";
    CValue<int> a;
    cout << "浮点数对象b,";
    CValue<float> b;
    cout << "整数元素对象a的元素最大值为:" << Max(a) << endl;
    cout << "整数元素对象a的元素最小值为:" << Min(a) << endl;
    cout << "浮点数元素对象b的元素最大值为:" << Max(b) << endl;
    cout << "浮点数元素对象b的元素最小值为:" << Min(b) << endl;
}
亢潮 2022-09-08 15:46:03
  1. 修改了一下,看报错的行数,你就知道自己错哪里了。
  2. 还有取最大小值if(x < a.data[1]) x = a.data[1];下标应该是i
  3. 多用花括号,哪怕只有一行语句,保证程序可读性。
#include<iostream>
using namespace std;
template <typename t>
class CValue {
    t data[5];
    public:
        CValue() {
            cout<<"please input 5 numbers"<<endl;
            for(int i = 0; i < 5; i++)
                cin>>data[i];
}
        friend t Max(CValue <t> &a);
        friend t Min(CValue <t> &a);
}; 
template <typename t> t Max(CValue <t> &a) {
    t x = a.data[0];
    for(int i = 1; i < 5; i++)
    {
        if(x < a.data[i]){
            x = a.data[i];
        }
    }
    return x;
}
template <typename t> t Min(CValue <t> &a) {
    t min = a.data[0];
    for(int i = 1; i < 5; i++){
        if(min > a.data[i]){
            min = a.data[i];
        }
    }      
    return min;
}
int main() {
    cout<<"整数对象a,";
    CValue<int> a;
    cout<<"浮点数对象b,";
    CValue<float> b;
    cout<<"整数元素对象a的元素最大值为:"<<Max(a)<<endl;
    cout<<"整数元素对象a的元素最小值为:"<<Min(a)<<endl;
    cout<<"浮点数元素对象b的元素最大值为:"<<Max(b)<<endl;
    cout<<"浮点数元素对象b的元素最小值为:"<<Min(b)<<endl;
    return 0;
}

妄断弥空 2022-09-08 15:46:03

这个错是因为你忘记在main函数里写返回了啊!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文