非常简单的,c++模板类 对象不能访问里面的方法,为什么?
在vc下编译报错,为什么?
// Template1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<iostream>
using std::cout;
using std::endl;
template<class T>
class Max
{
private:
T item1,item2,item3;
public:
Max(){}
Max(T thefirst,T thesecond,T thethird);
T GetMaxItem();
void SetItem(T thefirst,T thesecond,T thethird);
};
template<class T>
Max<T>::Max(T thefirst,T thesecond,T thethird)
{
//return;
}
template<class T>
void Max<T>::SetItem(T thefirst,T thesesecond,T thethird)
{
item1=thefirst;
item2=thesesecond;
item3=thethird;
}
template<class T>
T Max<T>::GetMaxItem()
{
T maxitem;
maxitem=item1>item2?item1:item2;
maxitem=maxitem>item3?maxitem:item3;
return maxitem;
}
int main()
{
Max<int> nmyMax();
nmyMax.SetItem(4,5,6);
Max<double> dblmyMax(1.2,1.3,-1.4);
cout<<nmyMax.GetMaxItem()<<endl;
cout<<dblmyMax.GetMaxItem()<<endl;
return 0;
}
[解决办法]
Max<int> nmyMax();
改为Max<int> nmyMax;
否则c++会认为nmyMax是个函数。
[解决办法]
++
[解决办法]
你声明个函数,编译器怎么会认为你错, 但是你用函数调用肯定报错在nmyMax.SetItem(4,5,6);这行
