[英]Templates, no matching function for call to error
我编写了一个可以正常工作但只有一行的代码。 它给我带来麻烦。 在上面说k = Money<double>().increment (k,m); // this should've printed 6.25
k = Money<double>().increment (k,m); // this should've printed 6.25
,但无法正常工作。 当您注释掉并运行代码时,一切都很好。 有什么问题,我该如何解决?
谢谢您的帮助。
控制台中的错误显示:
main.cpp:59:36:错误:'Money :: increment(Money&,Money&)'没有匹配的函数调用错误main.cpp:59:36:info:候选者是:
main.cpp:41:3:info:T Money :: increment(T,T)[T = double]
main.cpp:41:3:信息:参数1从'Money'到'double'的未知转换
而且...候选人也是空的。 正如我说的那样,一切都很好。
这是代码:
#include <iostream>
using namespace std;
template <class T>
class Money {
private:
T dollar, cent;
public:
Money(T a, T b){
dollar = a;
cent = b;
}
Money(){
dollar = 0;
cent = 1;
}
Money& operator +=(const Money& v){
dollar += v.dollar;
cent += v.cent;
return (*this);
}
Money operator +(const Money& v) const{
Money temp(*this);
temp += v;
return temp;
}
Money& operator =(const Money& v){
dollar = v.dollar;
cent = v.cent;
return (*this);
}
T increment(T value, T amount);
};
template <class T>
T Money<T>::increment(T value, T amount)
{
T result = 0;
result += value + amount;
cout << result << " $" << endl;
return result;
}
int main()
{
int a = 2;
double b = 3.45;
Money<double> k(3,75);
Money<double> m(2,50);
a = Money<double>().increment (a,5); // this prints 7
b = Money<double>().increment (b,4.5); // this prints 7.95
k = Money<double>().increment (k,m); // this should've printed 6.25
return 0;
}
T
的背景下Money<double>
是double
,但你不及格double
s到Money<double>::increment()
你逝去的Money<double>
情况下,并且有可用的隐式转换从Money<double>
double
。
有几种方法可以解决此问题。
Money<T>::operator T() const;
。 这将提供从Money<double>
到double
的隐式转换,您可以定义它的作用。 编译器将在您传递的两个Money<double>
实例上自动调用此运算符。 Money<T>::increment(Money const &, Money const &);
添加重载Money<T>::increment(Money const &, Money const &);
。 Money<T>::increment
本身就是模板函数。 从代码中尚不清楚应采用哪种方法,但是其中一种将解决此特定错误。
@cdhowie的答案已经解决了您帖子中的问题。
这个答案质疑函数increment
的语义。
您正在使用:
a = Money<double>().increment (a,5);
没什么不同
a += 5;
Money<double>()
对象根本没有任何作用。 它不会增加任何Money
对象。 这就是为什么使其成为该类的成员函数没有任何意义。 要增加 Money
对象,可以使用以下语法:
int a = 2;
double b = 3.45;
Money<double> k(3,75);
Money<double> m(2,50);
Money<double> n(4,20);
k += a; // Increment k.dollars by a
m += k; // Increment m by k
m += b; // Increment m.dollars by b
k.increment(n); // Increment k by n
尽管我不知道为什么可以在使用k += n
时使用k.increment(n)
。
这是您班级的修订版,其中包含有效的main
。
#include <iostream>
using namespace std;
template <class T>
class Money {
private:
T dollar, cent;
public:
Money(T a = {}, T b = {}) : dollar(a), cent(b) {}
Money& operator +=(const Money& v){
dollar += v.dollar;
cent += v.cent;
return (*this);
}
Money operator +(const Money& v) const{
Money temp(*this);
temp += v;
return temp;
}
Money& operator =(const Money& v){
dollar = v.dollar;
cent = v.cent;
return (*this);
}
friend std::ostream& operator<<(std::ostream& out, Money const& m)
{
return (out << "Dollors: " << m.dollar << ", Cents: " << m.cent);
}
};
int main()
{
int a = 2;
double b = 3.45;
Money<double> k(3,75);
Money<double> m(2,50);
Money<double> n(4,20);
k += a; // Increment k.dollars by a
m += k; // Increment m by k
m += b; // Increment m.dollars by b
k += n; // Increment k by n
std::cout << m << std::endl;
std::cout << k << std::endl;
return 0;
}
输出:
Dollors: 10.45, Cents: 125
Dollors: 9, Cents: 95
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.