ISO C ++禁止声明'myStruct'没有类型 [英] ISO C++ forbids declaration of 'myStruct' with no type

查看:375
本文介绍了ISO C ++禁止声明'myStruct'没有类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码DeviceClass.cpp:

Here is my code DeviceClass.cpp:

...
#include "myHeader.h"

class DeviceClass : public DeviceClassBase {
private:
myClass::myStruct Foo;

Foo.one = 1;
Foo.two = 2;

myClass myclass(Foo);
...
};

这是来自myHeader.h文件的myClass:

This is myClass from the myHeader.h 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 get this error:

: error: ISO C++ forbids declaration of 'myStruct' with no type
: error: expected ';' before '.' token
: error: 'myStruct' is not a type
: In member function 'virtual void DeviceClass::Init()':
: error: '((DeviceClass*)this)->DeviceClass::myclass' does not have class type

我会出错吗?

我只能编辑DeviceClass.cpp文件。

I can only edit the DeviceClass.cpp file.

推荐答案

语句在类级别无效,它们只在函数或方法内有效。也许你打算写一个默认的构造函数。这是有点复杂的事实, myClass 没有默认构造函数(你的显式声明的复制构造函数抑制隐式默认构造函数),所以你需要在初始化列表。您将需要一个 static 工厂函数来创建所需的 myStruct 参数。

Statements are not valid at the class level, they are only valid inside of functions or methods. Perhaps you meant to write a default constructor instead. This is somewhat complicated by the fact that myClass has no default constructor (your explicitly-declared copy constructor suppresses the implicit default constructor), so you need to construct it in the initializer list. You'll need a static factory function to create the required myStruct argument with the values you want.

class DeviceClass : public DeviceClassBase
{
public:
    DeviceClass();

private:
    static myClass::myStruct create_myStruct();

    myClass myclass;
};

myClass::myStruct DeviceClass::create_myStruct()
{
    myClass::myStruct Foo;

    Foo.one = 1;
    Foo.two = 2;

    return Foo;
}

DeviceClass::DeviceClass() : myclass(create_myStruct()) { }

这篇关于ISO C ++禁止声明'myStruct'没有类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆