0

Lets say I have a struct of POD type.

// A.hpp
//struct is of POD type
struct s {
    int x;
    double y;
};

Then I have this struct become private member of the class A

// A.hpp
class A{
    s my_struct;
    int size;
public:
    A(int, double, int);

};

Now I want to initialize the class using constructor and I also want to initialize the my_sruct.

What I have so far is

// A.cpp
A::A(int x_, double y_, int size_):my_struct(x_, y_), size(size_){}

// main.cpp
A a( 4, 6.6, 7); // this is how I try to instantiate the object 

But I get error saying

error: no matching function for call to 's::s(int&, double&)'

I could solve it by placing a constructor in the struct s. But I thought for POD we did not need to do that. Am I missing something here?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
pokche
  • 1,141
  • 12
  • 36
  • 2
    What about `:my_struct({x_, y_})` instead of `:my_struct(x_, y_)` ? – DimChtz Aug 15 '16 at 22:42
  • @DimChtz yup that did it, so its the same way we do zero initialization of the pod struct but this time with value in it .. right? – pokche Aug 15 '16 at 22:45
  • If I'm not mistaken, can't you initialize Structs by s.x = 2, and s.y = 3.4 for example? They are public access, even in the class, and you should be able to do the initialization inside the constructor braces? I don't know how to exactly do it the way you're doing it, but I usually would just initialize like that. Perhaps maybe the syntax is the same and I just am not sure how to do so. – Yoshi_64 Aug 15 '16 at 22:49
  • I think this is a same question: http://stackoverflow.com/questions/4203010/how-to-initialize-member-struct-in-initializer-list-of-c-class –  Aug 15 '16 at 22:50
  • @BorisT Yeah I have seen that .. but it does the zero initialization .. and I wanted value initialization – pokche Aug 15 '16 at 22:51
  • @Yoshi_64 I am not sure we could directly do s.x .. plus I want struct to be private not public. – pokche Aug 15 '16 at 22:53
  • @pokche The struct can be private still in the class, but the accessor for structs are still public unless declared private. Like classes are default to private unless declared public. So you should be able to have class A { private: struct MyStruct { } } And should be able to do MyStruct.x = value if you want. – Yoshi_64 Aug 15 '16 at 22:54
  • @Yoshi_64 Hmm .. I will try this .. I actually wanted to use initialization list in the first place .. – pokche Aug 15 '16 at 23:04
  • @pokche Understandable. If DimChtz method works for you too, then that work, go for it. I tried, but I couldn't get it to compile on my end. Using VS 2010 Service Pack 1 on it. – Yoshi_64 Aug 15 '16 at 23:11
  • If it will help, "my_struct({x_,y_})" works for g++ whith -std=c++11 –  Aug 15 '16 at 23:15
  • @Yoshi_64 Yeah I had to palce --std=c++11 flag for g++ to compile it – pokche Aug 15 '16 at 23:17
  • Ah, makes sense. I just found out too from some reading that this was in C++11 so makes sense why I can't do it with my current VS setup. – Yoshi_64 Aug 15 '16 at 23:19

1 Answers1

2

Change this:

A::A(int x_, double y_, int size_):my_struct(x_, y_), size(size_){}

to this:

A::A(int x_, double y_, int size_):my_struct({x_, y_}), size(size_){}

of even (without the parentheses as QPaystaxes said):

A::A(int x_, double y_, int size_):my_struct{x_, y_}, size(size_){}

Minimal example:

struct s {
    int x;
    double y;
};

class A{
    s my_struct;
    int size;
public:
    A(int, double, int);

};

A::A(int x_, double y_, int size_):my_struct({x_, y_}), size(size_){}

int main() {
    A a( 4, 6.6, 7);
    return 0;
}

Compilation:

C02QT2UBFVH6-lm:~ gsamaras$ g++ -std=c++11 main.cpp
C02QT2UBFVH6-lm:~ gsamaras$

Otherwise, you may follow this answer.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305