傲笑风 2017-04-22 12:56 采纳率: 100%
浏览 1658
已采纳

请问c++中能在内模板外定义构造函数吗?如果可以请告诉一下

本人试过在类模板外定义构造函数,编译不能通过,而在模板类内定义构造函数则能编译成功。

如在模板类外定义不成功:
#include"iostream"
using namespace std;

template
class Compare
{
public:
Compare(numtype a, numtype b);
/*{
x = a; y = b;
}*/
numtype max();
numtype min();
private:
numtype x, y;
};

template
numtype Compare::Compare(numtype a, numpyte b)
{
x = a;
y = b;
}

template
numtype Compare::max()
{
return((x > y) ? x : y);
}

template
numtype Compare::min()
{
return((x < y) ? x : y);
}

int main()
{
Compare comp(5, 4);
cout << comp.max() << endl;
cout << comp.min() << endl;
system("pause");
return 0;
}

在类模板内定义则成功:
#include"iostream"
using namespace std;

template
class Compare
{
public:
Compare(numtype a, numtype b)
{
x = a; y = b;
}
numtype max();
numtype min();
private:
numtype x, y;
};

/*template
Compare::Compare(numtype a, numpyte b)
{
x = a;
y = b;
}*/

template
numtype Compare::max()
{
return((x > y) ? x : y);
}

template
numtype Compare::min()
{
return((x < y) ? x : y);
}

int main()
{
Compare comp(5, 4);
cout << comp.max() << endl;
cout << comp.min() << endl;
system("pause");
return 0;
}

是在模板类外不能定义构造函数还是我语法错了,知道的请告诉我一声,谢谢!

  • 写回答

1条回答 默认 最新

  • 关注
     template<typename T> class Stack
    {
        public:
            Stack<T>();
            ~Stack<T>();
            void Push(const T&);
            T Pop();
            void clear();
            T GetTopData() const;
        private:
            typedef struct nodeStack{
                T data;
                nodeStack *last;
            }*LinkStack;
    
            LinkStack _linkStack;
    };
    
    template<typename T> Stack<T>::Stack()
    {
        _linkStack=new nodeStack();
        _linkStack->last=NULL;
        _linkStack->data=(T)NULL;
    }
    template<typename T> Stack<T>::~Stack()
    {
        clear();
    }
    template<typename T> void Stack<T>::Push(const T&t) 
    {
        LinkStack ls=new nodeStack;
        ls->last=_linkStack;
        ls->data=t;
        _linkStack=ls;
    
    }
    template<typename T> T Stack<T>::Pop()
    {
        T t=NULL;
        if(_linkStack)
        {
            LinkStack ls;
            ls=_linkStack->last;
            if(!ls)//如果是底栈
             return (T)NULL;
            t=_linkStack->data;//输出当前出栈数据
            delete _linkStack; //删除当前栈      
            _linkStack=ls; //把前一个栈指针指向当前栈
        }
        return t;
    }
    template<typename T> void Stack<T>::clear()
    {
        while(_linkStack)
        {
            Pop();
        }
    }
    template<typename T> T Stack<T>::GetTopData() const
    {
        if(!_linkStack)
        return (T)NULL;
        return (T)_linkStack->data;
    
    }
    

    这是我以前写的一个简单栈,你的代码没贴全,我也不帮你找出,你可以参考一下,自行找出问题啦

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 9月2日

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看