C++模板类的拷贝构造函数初始化对象出错,求指导

麻烦大家一下,教教我怎么准确使用模板类,这个程序是我自己编出来学模板类使用的。
请大家注意一下我程序里面类A后面的大括号,请不要把里面的确定类型(float)等改成T类型,也不要把T类型改成确知类型(不然我是不会给分的,sorry了)。我编这个程是学习模板类使用的,不是写程序来看运行结果的。我想知道对于这样一个类的内部定义,怎样使用他来进行对象初始化,成员函数定义等。
我感觉我错的地方不光是定义对象上,如果教会了我怎样用这种类模板定义对象,我采纳并追加10分,如果帮我纠正了所有错误并注释清楚了,我追加20分。

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

template<class T>///*
class A
{
T para1;
T para2;
public:
static int shot_a;
static int shot_b;

A(float p1,float p2){
para1=p1/100;
para2=p2/150;
};

void score(T c);
float getpara1(){
float x=float(rand()%100)/10+para1-5;
return x;
};
float getpara2(){
float x=float(rand()%100)/10+para2-5;
return x;
};
}; ///*不要改变这段程序里面的类型(T和确知的类型)
template<class T>
int A<T>::shot_a=0;
template<class T>
int A<T>::shot_b=0;

int ram(float p,unsigned n){
int m=0;
int w=int(p*10000);
for (int i=0;i<n;i++)
if (rand()%10000<w) m++;
return m;
}

template<class T> //错误
A<T> a1=A(100,98); //错误
A<T> a2=A(65,55); //错误

template<class T>
void A<T>::score(T c){
int pa=(para1-c.para2/8)/40;
int pb=(c.para1-para2/8)/40;
shot_a=ram(pa,92);
shot_b=ram(pb,92);
}

void main(){
srand(time(0));
a1.score(a2);
cout<<"比赛结果"<<A<int>::shot_a<<"比"<<A<int>::shot_a<<endl;
}
关键是,用拷贝函数定义构造函数那里不好搞,其他地方就算出错也好纠正。
你用a(常量,常量)的话,系统就提示不能为常量的错误,你用A<int> a=A<int>(常量,常量)的话系统又说无法将int类型传过去。我都不知道用这个构造函数定义并初始化能不能做到,说不定C++没这个语法。用C++搞某些关于类的代码真麻烦。
到处找资料,都找不到用不带模板类型的拷贝构造函数定义并初始化对象的例子。

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
template<class T>/// 这个是一个模板类,
class A
{
T para1;
T para2;
public:
static int shot_a;
static int shot_b;
A(float p1,float p2)
{
para1=p1/100;
para2=p2/150;
};
A(){};// 默认的构造函数

void score(T c);
float getpara1()
{
float x=float(rand()%100)/10+para1-5;
return x;
};
float getpara2()
{
float x=float(rand()%100)/10+para2-5;
return x;
};
}; ///*不要改变这段程序里面的类型(T和确知的类型)
template<class T>
int A<T>::shot_a=0;
template<class T>
int A<T>::shot_b=0;

int ram(float p,unsigned n)
{
int m=0;
int w=int(p*10000); //*10000容易引发错误,最好是定义为double型
for (int i=0;i<n;i++)
if (rand()%10000<w) m++;
return m;
}

template<class T> // 在外部定义成员
void A<T>::score(T c) //其实你的这里也定义错误了,因为你看,如果你的T的实例时int的话,那么你的c 是一个int型,但是你看c.para,说明c必须是一个结构体,共用体,等
{
int pa=(para1-c.para2/8)/40;
int pb=(c.para1-para2/8)/40;
shot_a=ram(pa,92);
shot_b=ram(pb,92);
}

// template<class T>// 你这里要实现是定义一个对象名的模板? 好像是没法使用吧,因为你写入一个常理不合理,但是在main中你又必须初始化这个,所以貌似不可以对象模板
// A<T> a1(float,float); //此时要与类模板的参数相对应,不能写入常量,类模板的实例 <类模板名> <<数据类型表》》 <<对象名列标>> 入Stack<int> ba; int是实例化的,而ba是实例化int是的对象
// template<class T> //**********
// A<T> a2(float,float); // 主要是没有意义的,因为就是定义了但是在下面也是要重新定义的,因为模板不允许有常量形参

void main()
{
srand(time(0));
//但是这里你错了,因为你没有定义类模板的实例 <类模板名> <<数据类型表》》 <<对象名列标>>
A<int> list;// 这里的list调用了默认的构造函数,你没定义,
// 其实就是这么说吧,被T修饰的地方,如果你定义了A(int)那么所有被T修饰的地方都被int代换,,,,,明白不?这个是实质,可能说的不对,但是有助你理解
A<int> a1(2,3);
A<int> a2(3,3);

cout<<"比赛结果"<<list.shot_a<<"比"<<list.shot_a<<endl;
// 验证你的

cout<<"比赛结果"<<a1.shot_a<<"比"<<a1.shot_a<<endl;
cout<<"比赛结果"<<a2.shot_a<<"比"<<a2.shot_a<<endl;
}

//不知道我的解释你懂不,,嘻嘻,我也在学,,这一部分,恩,你大概是钻了牛角尖了,自己看看书,看清了概念就会明白的,
//
下面是我的一个实验的程序,,你看一下,

// 这个是一个栈类,能够实现对int float ,double ,的进入栈测试,,再实现char 型,和string型的操作
#include <iostream>
#include "string"
using namespace std;
template <class T>
class Stack
{
int size;// 栈的大小
int top;//定义头标志
T* tack;// 定义一个栈的结构,动态分配空间的,
public:
Stack(int=10);// 初始化成10个空间
bool push(const T& );
bool pop(T& );
bool isEmpty()const {return top==-1;}// 这里是判断的是top==-1而不是top=-1不是赋值的过程所以错误了,
bool isFull()const {return top==size-1;}// 所以在修改错误的时很必须好好的看明白
~Stack(){delete[] tack;}
// 定义析构函数的情况
};
// 下面是在类外定义的情况
// template <class T>
// <返回类型> 类模板名 <类型名表>::成员函数( 形式参数表)

template <class T>
Stack<T>::Stack(int s)
{
size=s>0?s:10;
top=-1;
tack=new T[size];
}
template <class T>
bool Stack<T>::push(const T& pushvalue)
{
if(!isFull())
{
tack[++top]=pushvalue; // 因为这里是0开始的,,,所以先要++
return true; // bool类型,返回值为TRUE或是failed,为了用了看是否满足下一步,但是这里这个值也是可以手动赋的,所以比我一起的方好用
}
return false;
}
template <class T>
bool Stack<T>::pop(T &popvalue)
{
if(!isEmpty())
{
popvalue=tack[top--];
return true;
}
return false;
};

void main()
{
Stack <double> doublestack(5);
double f=1.0;
cout<<"开始为,双精度类型入栈"<<endl;
while(doublestack.push(f))
{
f++;
}
cout<<"栈满了"<<f<<" 不能入栈"<<endl;
cout<<"双精度开始出栈"<<endl;
while(doublestack.pop(f))
{
cout<<f<<" ";
}

Stack<int> intstack;
int a=22;
cout<<endl<<"整形开始入栈"<<endl;
while(intstack.push(a))
{ a+=2;
}
cout<<"出栈"<<endl;
while(intstack.pop(a))
{ cout<<a<<" ";
}
cout<<endl<<"字符型"<<endl;
Stack<char> charstack;
char ch='a';
while(charstack.push(ch))
{ ch+=2;
}
cout<<"字符出栈"<<endl;
while(charstack.pop(ch))
{ cout<<ch<<" ";
}
cout<<endl<<"字串型"<<endl;
Stack<string> ststack;
string st="wosh";
while(ststack.push(st))
{ st;
}
cout<<"出栈"<<endl;
while(ststack.pop(st))
{ cout<<st<<" ";
}

cout<<"\n 元素全部出栈"<<endl;

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-08-05
template<class T> //错误
A<T> a1=A(100,98); //错误
A<T> a2=A(65,55); //错误
这个地方错了。你应该是想声名两个A类型的对象吧?声明的时候必须用已知的类型。格式应该是A<int> a1(100,98) ,template<class T>删掉。
C++的泛型编程只针对类和函数,把数据类型作为一个参数。你声明一个类对象,就是把一个类实例化,如果数据类型都未知,那计算机怎么产生实例呢?
相似回答