[英]How to have a Struct with template with a class
With this code (just a class of test): 使用此代码(只是一类测试):
typedef unsigned short UInt16;
template<class T>
class CClass
{
public:
SValue* getNewSValue(void);
private:
typedef struct {
T *mValue;
T *next;
T *previous;
UInt16 index;
} SValue;
};
template<typename T>
SValue* CClass<T>::getNewSValue(void)
{
return new SValue;
}
I have the following errors: 我有以下错误:
error C2143: syntax error : missing ';'
错误C2143:语法错误:缺少';' before '*'
在'*'之前
error C4430: missing type specifier - int assumed.
错误C4430:缺少类型说明符 - 假定为int。 Note: C++ does not support default-int
注意:C ++不支持default-int
Is it possible to use a Struct within a class? 是否可以在类中使用Struct? If I declare the struct out of the class the template doesn't see the template
T
. 如果我从类中声明结构,模板就看不到模板
T
$9.2/2- The key is the below quote from the C++ Standard03 $ 9.2 / 2-关键是C ++ Standard03的以下引用
`A class is considered a completely-defined object type (3.9) (or complete type) at the closing } of the class-specifier. `在类说明符的结束时,类被认为是完全定义的对象类型(3.9)(或完整类型)。 Within the class member-specification, the class is regarded as complete within function bodies, default arguments and constructor ctor-initializers (including such things in nested classes).
在类成员规范中,该类在函数体,默认参数和构造函数ctor-initializers(包括嵌套类中的这类事物)中被视为完整。 Otherwise it is regarded as incomplete within its own class member-specification.
否则,它在其自己的类成员规范中被视为不完整。
Don't know what is UINT16
, but the following should work 不知道什么是
UINT16
,但以下应该有效
template<class T>
class CClass
{
private:
typedef struct {
T *mValue;
T *next;
T *previous;
short int index; // Replacing with int for illustration only
} SValue;
public:
SValue* getNewSValue(void);
private:
};
EDIT 3: The ***
came there trying to make the change BOLD (which I should have deleted anyways) 编辑3:
***
来到那里试图改变BOLD(我应该删除它)
template<class T> typename CClass<T>::SValue* CClass<T>::getNewSValue(void)
{
return new SValue;
}
int main(){
CClass<int> s;
s.getNewSValue();
}
Since the member function definition is in global scope, you need to qualify its return type with CClass::
to refer to the name within class scope. 由于成员函数定义在全局范围内,因此需要使用
CClass::
限定其返回类型以引用类范围内的名称。 Also, the typename
keyword is needed when referring to typenames nested within templates. 另外,当引用嵌套在模板中的类型名时,需要
typename
关键字。
template<typename T>
typename CClass<T>::SValue* CClass<T>::getNewSValue(void)
Also, the nested struct is unnamed. 此外,嵌套结构是未命名的。 Note that in C++, classes and structs have proper names, and a typedef name is not a class name.
请注意,在C ++中,类和结构具有正确的名称,并且typedef名称不是类名。 You will be much better off avoiding the typedef.
避免使用typedef你会好得多。
struct SValue {
T *mValue;
T *next;
T *previous;
UInt16 index;
};
It looks like your struct
is not defined when you declare the return type of getNewSValue
. 当您声明
getNewSValue
的返回类型时,看起来您的struct
getNewSValue
。
With that you don't have that error: 有了它你没有那个错误:
template<class T>
class CClass
{
public:
SValue* getNewSValue(void);
private:
typedef struct {
T *mValue;
T *next;
T *previous;
UInt16 index;
} SValue;
};
template<typename T>
SValue* CClass<T>::getNewSValue(void)
{
return new SValue;
}
And you also have to qualify the SValue
return type, because it is nested in CClass
when you use it to define getNewSValue
. 你也有资格的
SValue
返回类型,因为它是嵌套在CClass
当你用它来定义getNewSValue
。
Declare SValue
before getNewSValue()
: 在
getNewSValue()
之前声明SValue
:
template<class T>
class CClass
{
private:
typedef struct {
T *mValue;
T *next;
T *previous;
UInt16 index;
} SValue;
public:
SValue* getNewSValue(void);
};
Also, make sure UInt16
is defined. 另外,确保定义了
UInt16
。
how about trying this ? 试试这个怎么样? it worked on VS2008
它适用于VS2008
template<class T>
class CClass
{
private:
typedef struct {
T *mValue;
T *next;
T *previous;
__int16 index;
} SValue;
public:
SValue* getNewSValue(void)
{
return new SValue;
}
};
The SValue-Type is inside the CClass-namespace. SValue-Type位于CClass-namespace中。 You need to fully qualify the typename outside the class.
您需要完全限定类外的类型名称。
template<typename T>
CClass<T>::SValue* CClass<T>::getNewSValue(void)
Also, you should make SValue public, if it is returned by a public method. 此外,如果公共方法返回SValue,则应将其公之于众。
For the record, none of these will compile (on GCC 4.2 at least) as soon as a call to getNewSValue()
is called. 对于记录,一旦调用
getNewSValue()
调用,这些都不会编译(至少在GCC 4.2上)。 Since it's a public method that returns a pointer to a private type, you'll get a compile error stating that SValue is private
. 由于它是一个返回指向私有类型的指针的公共方法,因此您将收到一个编译错误,指出
SValue is private
。
In order for this to work, you'll either need to make SValue public, or change the type of the return value of getNewSValue()
to something like void*
, since it seems you're trying to make SValue an opaque type. 为了实现这一点,你需要公开SValue,或者将
getNewSValue()
的返回值类型更改为void*
,因为看起来你正试图让SValue成为一个不透明的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.