#include <iostream>
#include <conio.h>
using namespace std;
template <class T>
T Max(T a, T b) {
return a > b ? a : b;
}
struct box {
double height;
double width;
double length;
double volume;
};
void setVolume(box &b) {
b.volume = b.height * b.width * b.length;
}
template <> double Max<box>(box b1, box b2) { //Ошибка
return b1.volume > b2.volume ? b1.volume : b2.volume;
}
int main() {
box b1 = { 5, 4, 2 };
box b2 = { 10, 10, 2 };
setVolume(b1);
setVolume(b2);
cout << Max(b1, b2) << endl;
_getch();
return 0;
}
错误 C2912 显式特化;"double Max(box,box)" 不是函数特化
模板很明确:接受两个
T,返回T。你 - 需要两个box,但返回的东西不是box,而是double......这不是您的模板专业化。
你可以这样做:
或者像这样: