error - C++写的一个简单类模版 友元函数求最大最小值
巴扎黑
巴扎黑 2017-04-17 12:01:55
[C++讨论组]

如题 , 编译时总是说 [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;

}
`
巴扎黑
巴扎黑

全部回复(3)
高洛峰

你需要实例化模板函数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;
}
怪我咯
  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;
}

PHP中文网

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号