gpt4 book ai didi

c++ - 我收到错误 : call of overloaded ‘max(Cents&, Cents&)’ is ambiguous

转载 作者:搜寻专家 更新时间:2023-10-31 02:03:18 45 4
gpt4 key购买 nike

<分区>

1.. 我正在测试以下使用模板的代码
源码来源于此learncpp教程
https://www.learncpp.com/cpp-tutorial/132-function-template-instances/

#include <iostream>
#include <cstring>
#include <typeinfo>
using namespace std;

// ======================================================================
template <typename T>
const T& max(const T& x,const T& y)
{
return (x>y)?x:y;
}

// ======================================================================
class Cents
{
private:
int m_cents;
public:
Cents(int cents):m_cents(cents)
{
}

friend bool operator>(const Cents &c1,const Cents &c2)
{
return (c1.m_cents>c2.m_cents);
}

int get_val()
{
m_cents;
}
};

// ======================================================================
int main()
{
Cents nickle(5);
Cents dime(10);

Cents bigger=max(nickle,dime);
// std::cout<<"bigger: "<<bigger.get_val()<<std::endl;
// bigger: 1471225424

return 0;
}

我得到这个错误

error: call of overloaded ‘max(Cents&, Cents&)’ is ambiguous
Cents bigger=max(nickle,dime);

代码有什么问题?

2.. 如何打印结果?
例如,我试过 std::cout<<"bigger: "<<bigger<<std::endl;
但是我遇到了以下错误,它说没有重载运算符 << for bigger(Cent type object)

error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
std::cout<<"bigger: "<<bigger<<std::endl;
error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘Cents’)
std::cout<<"bigger: "<<bigger<<std::endl;

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com