shshuahw 2020-06-23 18:40 采纳率: 33.3%
浏览 135

关于模板类的定义和使用

下面这个程序问题在哪里呢,昨天看了两个多小时都没成功
//模板类的定义
#include
using namespace std;
template
class ARRAY
{
public:
template
ARRAY(int _size)
{
this->size=_size;
p=new T[this->size];

}
    ~ARRAY(void)
{
    delete this->p;
}
template <typename T>
    T* Append(const ARRAY & other)const
{
    int i;
    T *p1;
    p1=new T[this->size+other.size];
    for(i=0;i<this->size;i++)
        p1[i]=this->p[i];
    for(int j=0;j<other.size;i++,j++)
        p1[i]=other.p[j];
    return p1;
}
template <typename T>
    T* operator+(const ARRAY & other)const
{
    int i;
    T *p1;
    p1=new T[this->size+other.size];
    for(i=0;i<this->size;i++)
        p1[i]=this->p[i];
    for(int j=0;j<other.size;i++,j++)
        p1[i]=other.p[j];
    return p1;
}
template <typename T>
    void setarray(void)
{
    int i;
    for(i=0;i<this->size;i++)
        cin>>this->p[i];
}

private:
int size;
T * p;
};
#include
#include"Append.h"
using namespace std;
void Print(double *p,int size)
{
for(int i=0;i cout }
//以下为main.cpp
#include
#include"Append.h"
using namespace std;
void Print(double *p,int size)
{
for(int i=0;i cout }
int main()
{
double *p;
ARRAY a(5);
ARRAY b(7);
a.setarray(void);
b.setarray(void);
p=a+b;
Print(p,12);
return 0;
}

C:\Users\hp\Desktop\main.cpp(12) : error C2664: '__thiscall ARRAY::ARRAY(const class ARRAY &)' : cannot convert parameter 1 from 'const int' to 'const class ARRAY &'
Reason: cannot convert from 'const int' to 'const class ARRAY'
No constructor could take the source type, or constructor overload resolution was ambiguous
C:\Users\hp\Desktop\main.cpp(13) : error C2664: '__thiscall ARRAY::ARRAY(const class ARRAY &)' : cannot convert parameter 1 from 'const int' to 'const class ARRAY &'
Reason: cannot convert from 'const int' to 'const class ARRAY'
No constructor could take the source type, or constructor overload resolution was ambiguous
C:\Users\hp\Desktop\main.cpp(14) : error C2144: syntax error : missing ')' before type 'void'
C:\Users\hp\Desktop\main.cpp(14) : error C2783: 'void thiscall ARRAY::setarray(void)' : could not deduce template argument for 'T'
C:\Users\hp\Desktop\main.cpp(14) : error C2059: syntax error : ')'
C:\Users\hp\Desktop\main.cpp(15) : error C2144: syntax error : missing ')' before type 'void'
C:\Users\hp\Desktop\main.cpp(15) : error C2783: 'void __thiscall ARRAY::setarray(void)' : could not deduce template argument for 'T'
C:\Users\hp\Desktop\main.cpp(15) : error C2059: syntax error : ')'
C:\Users\hp\Desktop\main.cpp(16) : error C2784: 'class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument for '' from 'class ARRAY e>'
C:\Users\hp\Desktop\main.cpp(16) : error C2783: ' *
thiscall ARRAY::operator +(const class ARRAY &) const' : could not deduce template argument for 'T'
C:\Users\hp\Desktop\main.cpp(16) : error C2676: binary '+' : 'class ARRAY' does not define this operator or a conversion to a type acceptable to the predefined operator
执行 cl.exe 时出错.
13 14行那个构造函数到底错在哪了,我看了参数表列没问题啊

  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿