-1

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.

Codename
  • 452
  • 3
  • 15
  • 2
    Doesn't look like valid C++. Assignment to instance variable in the middle of a class declaration??? – gnasher729 Dec 29 '14 at 07:07
  • 1
    Looks like you're approaching C++ by just typing in code and seeing what happens. This is the worst possible mistake you can do with this language; C++ for a few reasons simply cannot be learned by experimentation (no matter how smart you are), you need instead to read the manual first. Pick up a good book from http://stackoverflow.com/q/388242/320726 and read it cover to cover. Your future you will be really grateful if you do this. – 6502 Dec 29 '14 at 07:41
  • Why didn't you fix the problem from [your previous question](http://stackoverflow.com/questions/27683855/initialising-a-struct-which-is-member-of-class) first? – juanchopanza Dec 29 '14 at 08:49

1 Answers1

0

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()) { }
cdhowie
  • 158,093
  • 24
  • 286
  • 300