Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

class - ISO C++ forbids declaration of 'myStruct' with no type

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);
...
};

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

Where a m I going wrong?

I can only edit the DeviceClass.cpp file.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()) { }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...