Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Initializer list complains about number of arguments.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
colt
n00b
n00b


Joined: 17 Apr 2018
Posts: 17

PostPosted: Tue Feb 13, 2024 1:25 pm    Post subject: Initializer list complains about number of arguments. Reply with quote

Hi. I have a structure that I initialize through this code:

Code:
Sphere sphere2 {{300,240,-300},40000,1};


which cause the following error messages:

Code:
raytracer.cpp: In function ‘int main()’:
raytracer.cpp:256:39: error: no matching function for call to ‘Sphere::Sphere(<brace-enclosed initializer list>)’
  Sphere sphere {{300,240,-100},10000,1};
                                       ^
raytracer.cpp:256:39: note: candidates are:
raytracer.cpp:217:8: note: constexpr Sphere::Sphere(const Sphere&)
 struct Sphere {
        ^
raytracer.cpp:217:8: note:   candidate expects 1 argument, 3 provided
raytracer.cpp:217:8: note: constexpr Sphere::Sphere(Sphere&&)
raytracer.cpp:217:8: note:   candidate expects 1 argument, 3 provided
raytracer.cpp:257:40: error: no matching function for call to ‘Sphere::Sphere(<brace-enclosed initializer list>)’
  Sphere sphere2 {{300,240,-300},40000,1};
                                        ^
raytracer.cpp:257:40: note: candidates are:
raytracer.cpp:217:8: note: constexpr Sphere::Sphere(const Sphere&)
 struct Sphere {
        ^
raytracer.cpp:217:8: note:   candidate expects 1 argument, 3 provided
raytracer.cpp:217:8: note: constexpr Sphere::Sphere(Sphere&&)
raytracer.cpp:217:8: note:   candidate expects 1 argument, 3 provided


So I want to understand what's happening, like why it's expecting only one argument, and also why these error messages just vanishes when I comment out the "private" modifier for the variables to be initialized.
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1949
Location: South America

PostPosted: Tue Feb 13, 2024 1:55 pm    Post subject: Reply with quote

How is class Sphere defined in that program? I'm assuming from context that it is a C++ program.
_________________
NeddySeagoon wrote:
I'm not a witch, I'm a retired electronics engineer :)
Ionen wrote:
As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though :)
Back to top
View user's profile Send private message
logrusx
Advocate
Advocate


Joined: 22 Feb 2018
Posts: 3152

PostPosted: Tue Feb 13, 2024 3:07 pm    Post subject: Re: Initializer list complains about number of arguments. Reply with quote

colt wrote:
Hi. I have a structure that I initialize through this code:

Code:
Sphere sphere2 {{300,240,-300},40000,1};




That looks like a recursive definition, something like struct Sphere {Sphere, int, int} but when you pass the structure for the first argument, you pass three ints while it asks for a sphere and two ints. My C is very rusty, but I think the only way that could work is if the first argument in the inner parentheses is 0 or NULL. Or a valid Sphere initializer, but then at some depth you need to pass 0 or NULL.

Best Regards,
Georgi
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1949
Location: South America

PostPosted: Tue Feb 13, 2024 3:11 pm    Post subject: Reply with quote

Based on the comment about the private member(s), the OP is probably trying to use aggregate initialization (i. e. C structure intialization-style syntax) for a C++ class that isn't an aggregate, but without the class definition, we are just guessing.
_________________
NeddySeagoon wrote:
I'm not a witch, I'm a retired electronics engineer :)
Ionen wrote:
As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though :)


Last edited by GDH-gentoo on Tue Feb 13, 2024 5:04 pm; edited 1 time in total
Back to top
View user's profile Send private message
logrusx
Advocate
Advocate


Joined: 22 Feb 2018
Posts: 3152

PostPosted: Tue Feb 13, 2024 3:36 pm    Post subject: Reply with quote

It looks like a C inline structure initialization to me, but again, my C is a good 20 y.o. if not more... And my C++ was worse than my C was at that time, so yes - a wild guesswork from my side.

Best Regards,
Georgi
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23671

PostPosted: Tue Feb 13, 2024 4:12 pm    Post subject: Reply with quote

colt wrote:
So I want to understand what's happening, like why it's expecting only one argument,
It considered all the standard constructors (copy, move), which by definition take only one argument. Which constructor did you expect to use?
colt wrote:
and also why these error messages just vanishes when I comment out the "private" modifier for the variables to be initialized.
Is the structure eligible to be an aggregate, when its members are public?

That double-level brace can work, depending on the definition of the structure. This works in C++20 mode:
Code:
#include <array>
struct Sphere
{
   std::array<int, 3> a;
   int b;
   int c;
};

void x()
{
   Sphere sphere2 {{300,240,-300},40000,1};
   (void)sphere2;
}
The inner braced expression becomes the initializer for a.

OP: if you need help, please follow the stackoverflow advice: provide a minimal program, and the compiler invocation to use it, that will exhibit your problem.
Back to top
View user's profile Send private message
sam_
Developer
Developer


Joined: 14 Aug 2020
Posts: 2400

PostPosted: Tue Feb 13, 2024 7:07 pm    Post subject: Reply with quote

In C, you can and should use designated initialisers. You don't (surprisingly!) have that option in C++ until C++20.
Back to top
View user's profile Send private message
colt
n00b
n00b


Joined: 17 Apr 2018
Posts: 17

PostPosted: Wed Feb 14, 2024 9:59 pm    Post subject: Reply with quote

Here is the class (struct) definition that shall answer all the doubts, I think

Code:
struct Sphere {
   public:
      bool intersect (Vec3f origin, Vec3f dir, float t) {};
      int getColor () {return color;}
      //Vec3f getCenter () {return center;}
   private:
      Vec3f center;
      float radius_squared;
      int color;
};'
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1949
Location: South America

PostPosted: Wed Feb 14, 2024 11:05 pm    Post subject: Reply with quote

I see. This kind of initialization:

colt wrote:
Code:
Sphere sphere2 {{300,240,-300},40000,1};

i. e., providing a list of initializers that correspond to class members in declaration order, is one form of what is called aggregate initialization. Which, in C++, can be used only for classes that are aggregates. There's a list of conditions that make a class an aggregate, but having private members makes a class not an aggregate. Therefore, your code fails [1]. If you want to initialize an object of such a class in the way that you thought that would work, you need to write a constructor, such as:

Code:
struct Sphere {
   private:
      Vec3f center;
      float radius_squared;
      int color;
   public:
      bool intersect (Vec3f origin, Vec3f dir, float t) {
         /* I suppose there's actually code here, otherwise
         calling this has undefined behaviour */
      }
      int getColor () {return color;}
      //Vec3f getCenter () {return center;}
      Sphere (float x, float y, float z, float r2, int c):
         center{x, y, z}, radius_squared(r2), color(c) {}
};

And, with this definition, then do:
Code:
Sphere sphere2(300,240,-300,40000,1);

That works if an object of type Vec3f can in turn be initialized with an initializer list containing 3 float elements, which may or may not be possible in your program, depending on the definition of class Vec3f.

[1] It is possible to initialize an object of a class that is not an aggregate with a braced initializer list, but that calls a constructor instead, which is what the compiler tried to generate code for and failed, because there was no suitable constructor.
_________________
NeddySeagoon wrote:
I'm not a witch, I'm a retired electronics engineer :)
Ionen wrote:
As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum