首页 > 开发 > C++ > 正文

c++ 程序编译错误

2017-09-11 21:19:30  来源: 网友分享

程序功能如下:

在主函数中接受键盘输入的三个数据(用空格分隔),并且将最大的一个值输出到屏幕上(输出信息的前后都没有空白,输出信息的后面也没有回车换行)
从键盘输入的信息的类型可能有:
英文单词(不少于3个字母),相邻两个单词间用空格分隔
英文字母
整数
浮点数
分数,分子与分母以/分隔
程序运行时,仅接受一种类型的3个数据输入。然后输出其中最大者,结束程序运行

本地调试正常编译的,上传到oj却死活编译不通过……
看编译错误提示改了下也没办法。

#include <iostream>#include <string>#include <iomanip>#include <sstream>#include <functional>#include <cmath>using std::cin;using std::cout;using std::ios;using std::string;using std::stringstream;using std::less;using std::greater;class fraction{public:    template<typename streamType>        friend streamType& operator>>(streamType& is, fraction& rhs);    template<typename streamType>        friend streamType& operator<<(streamType& os, fraction& rhs);    fraction() = default;    fraction(const fraction& rhs) = default;    ~fraction() = default;    fraction& operator=(const fraction& rhs)  = default;    void set(int a, int b)    {        this->a_ = a;        this->b_ = b;    }    bool operator<(const fraction& rhs) const { return fraction::compare(*this, rhs, less<int>()); };    bool operator>(const fraction& rhs) const { return fraction::compare(*this, rhs, greater<int>()); };    bool operator==(const fraction& rhs) const { return this->a_ == rhs.a_ && this->b_ == rhs.b_; };    bool operator!=(const fraction& rhs) const { return !(*this == rhs); };    template <typename compType>    static bool compare(const fraction& lhs, const fraction& rhs, compType comp = less<int>())    {        if (lhs.b_ == rhs.b_)        {            return comp(lhs.a_, rhs.a_);        }        int lcm = fraction::lcm(lhs.b_, rhs.b_);        int tmp1, tmp2;        tmp1 = lhs.a_ * (lcm / lhs.b_);        tmp2 = rhs.a_ * (lcm / rhs.b_);        return comp(tmp1, tmp2);    }private:    int a_, b_;    template <typename Integer>    static Integer gcd(Integer A, Integer B)    {        do        {            const Integer tmp(B);            B = A % B;            A = tmp;        } while (B != 0);        return A;    }    template <typename Integer>    static Integer lcm(const Integer & A, const Integer & B)    {        Integer ret = A;        ret /= gcd(A, B);        ret *= B;        return ret;    }};template <typename streamType>streamType& operator>>(streamType& is, fraction& rhs){    int a, b;    is >> a;    if (!is.fail() && is.get() == '/')    {        is >> b;        if (b<0)        {            b = -b;            a = -a;        }        rhs.set(a, b);    }    else {        is.setstate(ios::failbit);    }    return is;}template <typename streamType>streamType& operator<<(streamType& os, fraction& rhs){    return os << rhs.a_ << "/" << rhs.b_;}template<class T>T max(const T& val1, const T& val2, const T& val3){    const T * pmax = &val1;    if (val2 > *pmax)    {        pmax = &val2;    }    if (val3 > *pmax)    {        pmax = &val3;    }    return *pmax;}template<class T>bool tryType(const string& val1str, const string& val2str, const string& val3str) {    T val1, val2, val3;    stringstream ss;    ss << val1str << " " << val2str << " " << val3str;    ss >> val1 >> val2 >> val3;    if (!ss.fail())    {        cout << std::setiosflags(ios::fixed) << std::setprecision(5) << max<T>(val1, val2, val3);        return true;    }    return false;}int main(){    string val1, val2, val3;    getline(cin, val1, ' ');    getline(cin, val2, ' ');    getline(cin, val3, '\n');    tryType<int>(val1, val2, val3) ||        tryType<double>(val1, val2, val3) ||        tryType<fraction>(val1, val2, val3) ||        tryType<string>(val1, val2, val3);    return 0;}

编译错误

a.cpp: In instantiation of 'bool tryType(const string&, const string&, const string&) [with T = fraction; std::string = std::basic_string<char>]':
a.cpp:139:43: required from here
a.cpp:125:9: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
In file included from /usr/include/c++/4.7/iostream:40:0,
from a.cpp:1:
/usr/include/c++/4.7/ostream:600:5: error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = fraction]'

解决方案

感谢felix提供的解答

streamType& operator<<(streamType& os, const fraction& rhs)

添加const即可