高手帮忙看看,为为什么这个数组模板类不行啊

nakey2008 2005-01-21 05:35:51
template<class T,class T1>
class myArray
{
public:
myArray(int size);
//~myArray();
T1 Sum(void);
T1 average_value(void);
void show_array(void);
add_value(T);

private:
T * data;
int size;
int index;
};
////////
template<class T,class T1>
myArray<T,T1>::myArray(int size)
{
size=size;
index=0;
data=new T[size];
if(data==NULL)
{
cerr<<"Insufficent memory!";
exit(1);
}
}

/*
template<class T,class T1>
myArray<T,T1>::~myArray()
{
delete[] data;
}*/


template<class T,class T1>
T1 myArray<T,T1>::Sum(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum;
}

template <class T,class T1>
T1 myArray<T,T1>::average_value(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum/index;
}

template<class T,class T1>
void myArray<T,T1>::show_array(void)
{
for(int i=0;i<index<i++)
{
cout<<data[index]<<" ";
cout<<endl;
}
}

template <class T,class T1>
myArray<T,T1>::add_value(T t)
{
if(index==size)
return -1;
data[index++]=t;
}
/////
#include "TemplateClass.h"
int main(int argc, char* argv[])
{
myArray<int,long> number(20);

for(int i=0;i<20;i++)
{
number.add_value(i);
}

number.show_myArray();
return 0;
}
...全文
175 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
nakey2008 2005-01-21
  • 打赏
  • 举报
回复
可能是这样,结贴了^_^
lw1a2 2005-01-21
  • 打赏
  • 举报
回复
我也刚开始学,不用VC的原因,是因为它对标准C++支持的不好
blueskyzsz 2005-01-21
  • 打赏
  • 举报
回复
带有模板类型的模板函数vc6只能放在头文件中定义,因为以前的编译器很难定位模板类里模板函数的位置
,所以只能在头文件中定义。如果不带的话没问题.
c++里面有一个新的关键字export 用这个关键字可以把模板函数放到cpp里面,
不过我不清楚那个编译器现在支持,vc6一定不支持的
这么写是可以地^_^

template<class T>
class MyClass
{
public:
MyClass() : m_argumT()
{
}
MyClass(T argumT)
{
m_argumT = argumT;
}

template<class OtherT>
void DoSomething(OtherT argumT1)
{
cout << "argumT: " << m_argumT << endl;
cout << "argumT1: " << argumT1 << endl;
}

protected:
T m_argumT;
};
nakey2008 2005-01-21
  • 打赏
  • 举报
回复
不懂你的意思,你能说清楚点么?
lw1a2 2005-01-21
  • 打赏
  • 举报
回复
你没包含实现的文件?
nakey2008 2005-01-21
  • 打赏
  • 举报
回复
我的文件定义如下:
1.模板类的头文件TemplateClass.h:
#ifndef TemplateClassH
#define TemplateClassH

template<class T,class T1>
class myArray
{
public:
myArray(int size);
~myArray();
T& operator[](int i);
T1 Sum(void);
T1 average_value(void);
show_array(void);
int add_value(T);

private:
T * data;
int size;
int index;
};

#endif
////////////////////////////////////
2。模板类的实现文件:TemplateClass.cpp
#include"stdafx.h"
#include "TemplateClass.h"

template<class T,class T1>
myArray<T,T1>::myArray(int size)
{
this->size=size;
index=0;
data=new T[size];
if(data==NULL)
{
cerr<<"Insufficent memory!";
exit(1);
}
}


template<class T,class T1>
myArray<T,T1>::~myArray()
{
delete[] data;
}

//重载下标操作符
template <class T,class T1>
T& myArray<T,T1>::operator[](int i)
{
return data[i];
}


template<class T,class T1>
T1 myArray<T,T1>::Sum(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum;
}

template <class T,class T1>
T1 myArray<T,T1>::average_value(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum/index;
}

template<class T,class T1>
myArray<T,T1>::show_array(void)
{
for(int i=0;i<index;i++)/////////////////////
{
cout<<data[i]<<" ";
cout<<endl;
}
}

template <class T,class T1>
int myArray<T,T1>::add_value(T t)
{
if(index==size)
return 1 ;/////////////////////////////////
data[index++]=t;
return 0;
}

3.主函数文件TestTemplate.cpp
#include "stdafx.h"
#include "TemplateClass.h"
#include"iostream"

using namespace std;


int main(int argc, char* argv[])
{
myArray<int,long> number(20);

for(int i=0;i<20;i++)
{
number.add_value(i);
}

//number.show_myArray();
number.show_array();
return 0;
}
很奇怪,怎么把头文件和实现文件分开就有问题了
lw1a2 2005-01-21
  • 打赏
  • 举报
回复
你用的什么编译器?

//t.h,类声明
#ifndef T_H
#define T_H
#endif

template<class T,class T1>
class myArray
{
public:
myArray(int size);
~myArray();
T& operator[](int i);
T1 Sum(void);
T1 average_value(void);
void show_array(void);
int add_value(T);


private:
T * data;
int size;
int index;
};



//t2.h,类实现
#ifndef T_H
#include "t.h"
#define T_H
#endif
template<class T,class T1>
myArray<T,T1>::myArray(int size)
{
myArray::size=size;
index=0;
data=new T[size];
if(!data)
{
cerr<<"Insufficent memory!";
exit(1);
}
}


template<class T,class T1>
myArray<T,T1>::~myArray()
{
delete[] data;
}

//重载下标操作符
template <class T,class T1>
T& myArray<T,T1>::operator[](int i)
{
return data[i];
}

template<class T,class T1>
T1 myArray<T,T1>::Sum(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum;
}

template <class T,class T1>
T1 myArray<T,T1>::average_value(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum/index;
}

template<class T,class T1>
void myArray<T,T1>::show_array(void)
{
for(int i=0;i<index;i++)
{
cout<<data[i]<<" ";
cout<<endl;
}
}

template <class T,class T1>
int myArray<T,T1>::add_value(T t)
{
if(index==size)
return -1;
data[index++]=t;
}



//t.cpp,主程序
#include "t.h"
#include "t2.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
myArray<int,long> number(20);

for(int i=0;i<20;i++)
{
number.add_value(i);
}

number.show_array();
system("pause");
return 0;
}

老田低代码 2005-01-21
  • 打赏
  • 举报
回复
我刚开始学习C++第一次实验了一个栈类,但是当我使用了
string strMSG;
cin>>strMSG;
以及:
cout<<strMSG;
的时候出现的错误大致是“string类中没有>>和<<操作符这样的提示”
我已经使用了:
#Include <iostream>
using std:string;
不知道为什么,但是我在其他的程序中又没有看见这样的问题。
nakey2008 2005-01-21
  • 打赏
  • 举报
回复
定义一个模板类的头文件,在定义一个实现文件,则会出现如下错误
TestTemplate.obj : error LNK2001: unresolved external symbol "public: __thiscall myArray<int,long>::~myArray<int,long>(void)" (??1?$myArray@HJ@@QAE@XZ)
TestTemplate.obj : error LNK2001: unresolved external symbol "public: void __thiscall myArray<int,long>::show_array(void)" (?show_array@?$myArray@HJ@@QAEXXZ)
TestTemplate.obj : error LNK2001: unresolved external symbol "public: int __thiscall myArray<int,long>::add_value(int)" (?add_value@?$myArray@HJ@@QAEHH@Z)
TestTemplate.obj : error LNK2001: unresolved external symbol "public: __thiscall myArray<int,long>::myArray<int,long>(int)" (??0?$myArray@HJ@@QAE@H@Z)

但如果都放在头文件里,则可以正常运行
nakey2008 2005-01-21
  • 打赏
  • 举报
回复
很奇怪啊,我把模板类的声明和实现分开来定义就不行,而放在一个文件里就可以了?
lw1a2 2005-01-21
  • 打赏
  • 举报
回复
1.size变量名的问题
2.没重载下标操作符
3.show_array(void)中应该是cout<<data[i]<<" ";
4.还有一些小错,我也改了
lw1a2 2005-01-21
  • 打赏
  • 举报
回复
错误很多。
以下在DevCPP中编译通过:

//TemplateClass.h
template<class T,class T1>
class myArray
{
public:
myArray(int size);
~myArray();
T& operator[](int i);
T1 Sum(void);
T1 average_value(void);
void show_array(void);
int add_value(T);


private:
T * data;
int size;
int index;
};
////////
template<class T,class T1>
myArray<T,T1>::myArray(int size)
{
myArray::size=size;
index=0;
data=new T[size];
if(!data)
{
cerr<<"Insufficent memory!";
exit(1);
}
}


template<class T,class T1>
myArray<T,T1>::~myArray()
{
delete[] data;
}

//重载下标操作符
template <class T,class T1>
T& myArray<T,T1>::operator[](int i)
{
return data[i];
}

template<class T,class T1>
T1 myArray<T,T1>::Sum(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum;
}

template <class T,class T1>
T1 myArray<T,T1>::average_value(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum/index;
}

template<class T,class T1>
void myArray<T,T1>::show_array(void)
{
for(int i=0;i<index;i++)
{
cout<<data[i]<<" ";
cout<<endl;
}
}

template <class T,class T1>
int myArray<T,T1>::add_value(T t)
{
if(index==size)
return -1;
data[index++]=t;
}




//TemplateClass.cpp
#include "TemplateClass.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
myArray<int,long> number(20);

for(int i=0;i<20;i++)
{
number.add_value(i);
}

number.show_array();
//system("pause");
return 0;
}
avalonBBS 2005-01-21
  • 打赏
  • 举报
回复
1.少头文件
2.定义处和引用处的名字不符,汗~`~~*_*
3。即使返回类型为void的成员函数,其void也不应省略
4.返回类型为void其return语句不应带表达式。
大致就这么多了,编译的能过了

/////////////////////////////////////////
#include <iostream>

using namespace std;

template<class T,class T1>
class myArray
{
public:
myArray(int size);
//~myArray();
T1 Sum(void);
T1 average_value(void);
void show_array(void);
void add_value(T);

private:
T * data;
int size;
int index;
};
////////
template<class T,class T1>
myArray<T,T1>::myArray(int size)
{
size=size;
index=0;
data=new T[size];
if(data==NULL)
{
cerr<<"Insufficent memory!";
exit(1);
}
}

/*
template<class T,class T1>
myArray<T,T1>::~myArray()
{
delete[] data;
}*/


template<class T,class T1>
T1 myArray<T,T1>::Sum(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum;
}

template <class T,class T1>
T1 myArray<T,T1>::average_value(void)
{
T1 sum=0;
for(int i=0;i<index;i++)
sum=sum+data[index];
return sum/index;
}

template<class T,class T1>
void myArray<T,T1>::show_array(void)
{
for(int i=0;i<index;i++)/////////////////////
{
cout<<data[index]<<" ";
cout<<endl;
}
}

template <class T,class T1>
void myArray<T,T1>::add_value(T t)
{
if(index==size)
return ;/////////////////////////////////
data[index++]=t;
}
/////





#include "TemplateClass.h"
int main(int argc, char* argv[])
{
myArray<int,long> number(20);

for(int i=0;i<20;i++)
{
number.add_value(i);
}

//number.show_myArray();
number.show_array();
return 0;
}






64,649

社区成员

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

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