vc6.0代码在 “sss.h”中定义模板类:
template<class T>
class sss
{
public:
sss(int n); T Front();
T Back();
}
template<class T>
sss<T>::sss(int s=QDEFAULT)
{
……
}
template<class T>
T sss<T>::Front()
{
……
}
template<class T>
T sss<T>::Back()
{
……
}
在另外的"test.h","test.cpp"中定义一个类,并在类的成员函数中使用模板类使用//--------test.h代码:
#include "sss.h"
class CTest
{
public:
BOOL GetCmd();//用到模板类的函数
//-------定义结构体--------------
#ifndef _cmdstruct_
#define _cmdstruct_
typedef struct cmdstruct{
CString ip;
sss<CString> Cmd(100); ////此句出错error C2059: syntax error : 'constant'
};
#endif
cmdstruct cmdq[4];
}//--------test.cpp代码:BOOL CTes::GetCmd()
{
for(int pot=0;pot<4;pot++)
{
CString str=cmdq[pot].Cmd.Back(); //此句出错,错误:left
//must have class/struct/union type of '.Back'
}
return TRUE;
}问题:
1.在类头文件中定义模板类对象,发现使用sss<CString> Cmd;定义对象没有?为什么?构造函数是有参数的
2.为什么在类的实现文件中没法用cmdq[pot].Cmd访问模板类成员函数,该怎样使用该类模板才能正确访问成员函数
template<class T>
class sss
{
public:
sss(int n); T Front();
T Back();
}
template<class T>
sss<T>::sss(int s=QDEFAULT)
{
……
}
template<class T>
T sss<T>::Front()
{
……
}
template<class T>
T sss<T>::Back()
{
……
}
在另外的"test.h","test.cpp"中定义一个类,并在类的成员函数中使用模板类使用//--------test.h代码:
#include "sss.h"
class CTest
{
public:
BOOL GetCmd();//用到模板类的函数
//-------定义结构体--------------
#ifndef _cmdstruct_
#define _cmdstruct_
typedef struct cmdstruct{
CString ip;
sss<CString> Cmd(100); ////此句出错error C2059: syntax error : 'constant'
};
#endif
cmdstruct cmdq[4];
}//--------test.cpp代码:BOOL CTes::GetCmd()
{
for(int pot=0;pot<4;pot++)
{
CString str=cmdq[pot].Cmd.Back(); //此句出错,错误:left
//must have class/struct/union type of '.Back'
}
return TRUE;
}问题:
1.在类头文件中定义模板类对象,发现使用sss<CString> Cmd;定义对象没有?为什么?构造函数是有参数的
2.为什么在类的实现文件中没法用cmdq[pot].Cmd访问模板类成员函数,该怎样使用该类模板才能正确访问成员函数
其次
typedef struct cmdstruct{
CString ip;
sss <CString> Cmd(100); ////此句出错error C2059: syntax error : 'constant'
}; 在类A或者结构A中声明B的对象时,是不能带构造函数的参数的。sss.h
template <class T>
class sss
{
public:
sss(int n = 100); T Front();
T Back();
} ;template <class T>
sss <T> ::sss(int s)
{
}
template <class T>
T sss <T> ::Front()
{
T t;
return t;
}
template <class T>
T sss <T> ::Back()
{
T t;
return t;
}
test.h#include "sss.h"
class CTest
{
public:
bool GetCmd();//Óõ½Ä£°åÀàµÄº¯Êý
//-------¶¨Òå½á¹¹Ìå--------------
#ifndef _cmdstruct_
#define _cmdstruct_ struct cmdstruct{
int ip;
sss<int> Cmd; ////´Ë¾ä³ö´íerror C2059: syntax error : 'constant'
};
#endif
cmdstruct cmdq[4];
} ;test.cpp#include "test.h"bool CTest::GetCmd()
{
for(int pot=0;pot <4;pot++)
{ int str=cmdq[pot].Cmd.Back(); //´Ë¾ä³ö´í£¬´íÎó£ºleft
//must have class/struct/union type of '.Back' }
return 1;
} void main()
{
}
所以把你的CString换成int了
如果我在定义模板类对象时,想指定构造函数的输入参数,那该怎样定义?我不一定非要用它的默认值
如果我在定义模板类对象时,想指定构造函数的输入参数,那该怎样定义?我不一定非要用它的默认值
需要的地方
Cmd = new xxxx;记得delete
另外如果改为
#ifndef _cmdstruct_
#define _cmdstruct_ struct cmdstruct{
int ip;
sss<int> Cmd;
};
#endif
cmdstruct cmdq[4];
会出现另一种错误error C2512: 'cmdstruct' : no appropriate default constructor available
是不是sss<int> Cmd; 找不到合适的构造函数的原因?
你再仔细分析下吧
新帖地址;http://forum.csdn.net/PointForum/Forum/PostTopic.aspx?forumID=f9fb995e-c93d-456a-8097-0b27ccb25c4e