请看下面的代码。
主文件
#include <iostream>
#include "Maximum.h"
using namespace std;
int main()
{
Maximum max;
int int1, int2, int3;
cout << "Input three integers: ";
cin >> int1 >> int2 >> int3;
cout << "The maximum value is: " << max.maximum(int1,int2,int3) << endl;
double double1, double2, double3;
cout << "Input three doubles: ";
cin >> double1 >> double2, double3;
cout << "The maximum value is: " << max.maximum(double1, double2, double3) << endl;
char char1, char2, char3;
cout << "Input three char values: ";
cin >> char1 >> char2 >> char3;
cout << "The maximum value is: " << max.maximum(char1,char2,char3) << endl;
}
最大值.h
template <class T>
class Maximum
{
public:
Maximum();
T maximum(T value1, T value2, T value3);
};
最大值.cpp
#include <iostream>
#include "Maximum.h"
Maximum::Maximum()
{
}
T Maximum::maximum(T value1, T value2, T value3)
{
T maximumValue = value1;
if(value2>maximumValue)
{
maximumValue = value2;
}
if(value3>maximumValue)
{
maximumValue = value3;
}
return maximumValue;
}
在 maximum.cpp 中,它给出“'T' 没有命名类型”错误。由于它是用户定义的,它可能无法识别它。如何解决这个问题呢?