模板的初级问题!在VC中不能运行。急救!多谢!

tata_1980 2003-10-23 11:07:02
用VC6.0建立了一个win32 console application,然后加入了一个c++source文件template.cpp,在这个文件里编了以下语句,结果编译无错无警告,运行时却说:
“cann't execute program”
哪位大虾知道是什么原因吗?
下面是我的例子(从“21天学通C++”摘来的),多谢了
---------------
#include<iostream>
using namespace std;
const int DefaultSize=10;

class Animal
{
public:
Animal(int);
Animal();
~Animal(){}
int GetWeight() const {return itsWeight;}
void Display() const {cout<<itsWeight;}
private:
int itsWeight;
};

Animal::Animal(int weight):itsWeight(weight){}

Animal::Anima():itsWeight(0){}


template <class T>
class Array
{
public:
//constructors
Array(int itsSize=DefaultSize);
Array(const Array &rhs);
~Array(){delete [] pType;}

//operators
Array & operator=(const Array&);
T& operator[](int offSet){return pType[offSet];}
const T& operator[](int offSet) const{return pType[offSet];}

//accessors
int GetSize() {return itsSize;}

private:
T *pType;
int itsSize;
};

//implement the Constructor
template <class T>
Array<T>::Array(int size):itsSize(size)
{
pType=new T[size];
for (int i=0;i<size;i++)
pType[i]=0;
}

//copy constructor
template <class T>
Array<T>::Array(const Array &rhs)
{
itsSize=rhs.GetSize();
pType=new T[itsSize];
for(int i=0;i<itsSize;i++)
pType[i]=rhs[i];
}

//operator=
template <class T>
Array<T> & Array<T>::operator = (const Array & rhs)
{
if(this==&rhs)
return *this;
delete [] pType;
itsSize=rhs.GetSize();
pType=new T[itsSize];
for(i=0;i<itsSize;i++)
pType[i]=rhs[i];
return *this;
}

//driver program
int main()
{
Array<int> theArray;
Array<Animal> theZoo;
Animal * pAnimal;

//fill the arrays
for(i=0;i<theArray.GetSize ();i++)
{
theArray[i]=i*2;
pAnimal=new Animal(i*3);
theZoo[i]=*pAnimal;
delete pAnimal;
}

for (j=0;j<theArray.GetSize ();j++)
{
cout<<"theArray["<<j<<"]:\t";
cout<<theArray[j]<<"\t\t";
cout<<"theZoo["<<j<<"]:\t";
theZoo[j].Display();
cout<<endl;
}
return 0;
}
...全文
86 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tata_1980 2003-10-23
  • 打赏
  • 举报
回复
楼上的,谢谢你,但你的说法也不对,因为去掉这一句还是同样的错误
tibetan 2003-10-23
  • 打赏
  • 举报
回复
///////////////////////////////////////////////////////////////////////////////
如果没记错,VC6 在没有 patch 的情况下,using namespace std; 和 <iostream> 以及模板之间有问题。用 using std::cout; using std::endl 这样试试?
////////////////////////////////////////////////////////////////////////////////
其实,在VC6下,不用什么using std::cout; using std::endl 一样的工作。

myclass<char*> t2("醉拳");这个地方有问题。你是将一个临时变量的地址给了变量,所以编译没有错但是执行有错。
另外,很多情都是这样,如果你的错误是:编译通过,执行不能通过,那你就看看你的指针,地址什么的有没有错。多数情况下是那个坏东西在作怪。
tata_1980 2003-10-23
  • 打赏
  • 举报
回复
请教如何给VC打补丁?
langzi8818 2003-10-23
  • 打赏
  • 举报
回复
没有错误,只要你把
#include <iostream.h>改成#include <iostream> using namespace std;
就可以了,我的也是VC6,只是打了个补丁SP5!:)
mechgoukiteng 2003-10-23
  • 打赏
  • 举报
回复
搞个vc7.1的,或是gcc3.2版本以上

否则学template很挫折de
tata_1980 2003-10-23
  • 打赏
  • 举报
回复
谢谢楼上,可下面的例子也是同样的错误:
#include <iostream.h>

template <class T>

class myclass{

T temp;

public:

myclass(T name){

temp=name;

}

T vomit(){

return temp;

}

};

void main()

{

myclass<int> t1(2);

std::cout<<t1.vomit()<<endl;

myclass<char*> t2("醉拳");

std::cout<<t2.vomit()<<endl;

myclass<float> t3(3.14159);

std::cout<<t3.vomit()<<endl;

}

Wolf0403 2003-10-23
  • 打赏
  • 举报
回复
如果没记错,VC6 在没有 patch 的情况下,using namespace std; 和 <iostream> 以及模板之间有问题。用 using std::cout; using std::endl 这样试试?
tata_1980 2003-10-23
  • 打赏
  • 举报
回复
up
tata_1980 2003-10-23
  • 打赏
  • 举报
回复
楼上的对,但是我的确不能运行,奇怪。
请问,你的VC6打过补丁吗,如果打过,如何升级呢
pscqll 2003-10-23
  • 打赏
  • 举报
回复
我编译以后运行正常,结果是不是:
theArray[0]: 0 theZoo[0]: 0
theArray[1]: 2 theZoo[1]: 3
theArray[2]: 4 theZoo[2]: 6
theArray[3]: 6 theZoo[3]: 9
theArray[4]: 8 theZoo[4]: 12
theArray[5]: 10 theZoo[5]: 15
theArray[6]: 12 theZoo[6]: 18
theArray[7]: 14 theZoo[7]: 21
theArray[8]: 16 theZoo[8]: 24
theArray[9]: 18 theZoo[9]: 27
Press any key to continue
tata_1980 2003-10-23
  • 打赏
  • 举报
回复
uuuuuuuuuuuuuuupppppppppppp
tata_1980 2003-10-23
  • 打赏
  • 举报
回复
up

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧