#include <iostream>
using namespace std;
template <class T>
class Carray{
public:
Carry(int i = 2);//i是数组长度
bool show_array();
T *set_array(int i, int num);
T *enlarge(int oi,int want)//oi为原数组长度,want为需求量
private:
T *p;//任何类型数组的指针
int len;//数组长度
};
template<class T>
Carray<T>::Carry(int i){
p = new T[i]();
len = i;
}
template<class T>
T *Carray<T>::set_array(int len,int num){ /*num为要输入的数量*/
cout<<"请输入一系列相同数据类型的数"<<endl;
int j=0
for(;j < len;j++){
cin>>p[j];
}
if(j < num-1){
p = enlarge(len,num);
for(;j < num;j++)
cin>>p[j];
}
return p;
}
template<class T>
Carray<T>::enlarge(int oi,int want){
T *np = new T[want]();
for(int j = 0;j < oi;j++){
np[j] = p[j];
}
len = want;
return np;
}
template<class T>
bool Carray<T>::show_array(){
for(int j = 0;j < len;j++)
cout<<p[j]<<"\t";
cout<<endl;
return 1;
}
int main(){
Carray<int> a1();
Carray<char>a2(5)
return 1;
}
一共4个错误。
两个地方缺少分号
carray写成了carry
还有一个没有返回值
5不是char类型,改成 '5'
Carraya2(5)
改成
Carraya2(5)
// Q186587.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T>
class Carray{
public:
Carray(int i = 2);//i是数组长度
bool show_array();
T *set_array(int i, int num);
T *enlarge(int oi,int want);//oi为原数组长度,want为需求量
private:
T *p;//任何类型数组的指针
int len;//数组长度
};
template<class T>
Carray<T>::Carray(int i){
p = new T[i]();
len = i;
}
template<class T>
T *Carray<T>::set_array(int len,int num){ /*num为要输入的数量*/
cout<<"请输入一系列相同数据类型的数"<<endl;
int j=0
for(;j < len;j++){
cin>>p[j];
}
if(j < num-1){
p = enlarge(len,num);
for(;j < num;j++)
cin>>p[j];
}
return p;
}
template<class T>
T * Carray<T>::enlarge(int oi,int want){
T *np = new T[want]();
for(int j = 0;j < oi;j++){
np[j] = p[j];
}
len = want;
return np;
}
template<class T>
bool Carray<T>::show_array(){
for(int j = 0;j < len;j++)
cout<<p[j]<<"\t";
cout<<endl;
return 1;
}
int main(){
Carray<int> a1();
Carray<char>a2(5);
return 1;
}
char类型的数据都要用‘’和“”这个的
第一反应类型不对啊,char类型应该是‘5’!!
饿,大神就是大神,什么问题一眼就明了