Apr 29, 2012 at 3:19pm UTC
First, your class needs a three-argument constructor:
1 2 3 4 5
class a {
float x,y,z;
public :
a(float a, float b, float c) : x(a), y(b), z(c) {}
};
then you can use either
b.emplace_back(0, 0, 0);
or
b.push_back(a(0, 0, 0));
online demo:
http://ideone.com/5Ngbx
Last edited on Apr 29, 2012 at 3:23pm UTC
Apr 29, 2012 at 3:25pm UTC
When making x, y, z public (as they should be), you can write the following, even without creating a constructor:
b.push_back({0,0,0});
Apr 29, 2012 at 3:36pm UTC
It's not working. I wrote:
b.push_back({0,0,0});
But some primary-expression is expected before "{" token.
Apr 29, 2012 at 3:43pm UTC
This type of initialization and emplace_back are part of C++11. You need a recent compiler and you need to add the compiler switch -std=c++0x (GCC, ICC, clang).
Apr 29, 2012 at 3:46pm UTC
What Cubbi said about constructor and push_back should work even if you have an older compiler.
Apr 29, 2012 at 3:52pm UTC
Is it necessary that the floats are private then?
Last edited on Apr 29, 2012 at 3:56pm UTC
Apr 29, 2012 at 3:57pm UTC
No, it doesn't matter (to the compiler, that is).
Last edited on Apr 29, 2012 at 3:57pm UTC
Apr 29, 2012 at 4:11pm UTC
This is weird. It's not working but thank you anyway. Guess I'll have to solve the problem in some other way.
Apr 29, 2012 at 4:16pm UTC
Or you could just describe your problem in a more informative way than "it doesn't work" (without any information of what "it" even is).
Apr 29, 2012 at 4:36pm UTC
did you do like what cubbi said?