初始化作为类成员的struct [英] Initialising a struct which is member of class
本文介绍了初始化作为类成员的struct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
...
#includemyheader.h
myClass :: myStruct Foo;
Foo.one = 1;
Foo.two = 2;
myClass myclass(Foo);
...
这是我的头文件类:
class myClass:baseClass {
public:
struct myStruct {
myStruct():
one (0),
two(0){}
int one;
int two;
}; $ b $b myClass(const myStruct& mystruct);
};
但这是无法编译,我想我正确访问元素一和二...
我得到这个错误:
:预期的构造函数,析构函数或类型转换。
我错了什么?
解决方案
Foo.one = 1;
这是一个语句,它需要进入一个函数或方法定义。语句不能单独出现在源文件的顶层。
尝试将此代码放在函数内部,例如入口点 main ()
:
int main(){
myClass :: myStruct Foo;
Foo.one = 1;
Foo.two = 2;
myClass myclass(Foo);
return 0;
}
Here is my code:
...
#include "myheader.h"
myClass::myStruct Foo;
Foo.one = 1;
Foo.two = 2;
myClass myclass(Foo);
...
This is my class from the header file:
class myClass : baseClass{
public:
struct myStruct {
myStruct():
one(0),
two(0){}
int one;
int two;
};
myClass(const myStruct &mystruct);
};
But this is failing to compile, I think I am accessing the elements one and two in the proper way... I get this error:
: expected constructor, destructor, or type conversion before '.' token .
Where a m I going wrong?
解决方案
Foo.one = 1;
This is a statement, and it needs to go inside of a function or method definition. Statements cannot appear by themselves at the top level of a source file.
Try putting this code inside of a function, for example the entry point main()
:
int main() {
myClass::myStruct Foo;
Foo.one = 1;
Foo.two = 2;
myClass myclass(Foo);
return 0;
}
这篇关于初始化作为类成员的struct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文