Thread: Issue creating objects with inheritance and initializater list

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    15

    Issue creating objects with inheritance and initializater list

    Hello. I want to combine a constructor that simultaneously uses an initializer list together with inheritance. My problem is that I am receiving these error messages:

    |4|error: no match for call to ‘(Vec4f) (float, float, float, float)’ |5|error: no match for call to ‘(Vec4f) (float, float, float, float) |6|error: no match for call to ‘(Vec4f) (float, float, float, float) |7|error: no match for call to ‘(Vec4f) (float, float, float, float)|
    Bellow a sample code that reproduces the above error:

    Code:
    class Vec3f {
       public:
          inline Vec3f () : x (0), y (0), z (0) {}
          inline Vec3f (float xx, float yy, float zz) : x (xx), y (yy), z (zz)  { }
       private:
          float x,y,z;
    };
    
    class Vec4f : Vec3f
    {
       public:
          inline Vec4f (): Vec3f(), w(0) {}
          inline Vec4f (float a,float b,float c,float d): Vec3f (a,b,c), w(d) {}
       private:
          float w;
    };
    
    class Matrix4x4 {
       public:
          void setIdentity ();
       private:
          Vec4f row1, row2, row3, row4;
    };
    
    void Matrix4x4::setIdentity () {
       row1 (1.0f,0.0f,0.0f,0.0f);
       row2 (0.0f,1.0f,0.0f,0.0f);
       row3 (0.0f,0.0f,1.0f,0.0f);
       row4 (0.0f,0.0f,0.0f,1.0f);
    }
    
    
    main () {
       Matrix4x4 a;
       a.setIdentity ();
    }
    So, any suggestions. Thanks for the input.

    P.S: I first opened a thread about it here: Initializer list with inheritance - C++ Forum. Until the present moment there was no solution, but if it shows up there, I will update here with the solution.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    One of two way perhaps.
    Code:
    class Vec3f {
       public:
          inline Vec3f () : x (0), y (0), z (0) {}
          inline Vec3f (float xx, float yy, float zz) : x (xx), y (yy), z (zz)  { }
          inline void Set3f (float xx, float yy, float zz) {
            x = (xx);
            y = (yy);
            z = (zz);
          }
       private:
          float x,y,z;
    };
    
    class Vec4f : Vec3f
    {
       public:
          inline Vec4f (): Vec3f(), w(0) {}
          inline Vec4f (float a,float b,float c,float d): Vec3f (a,b,c), w(d) {}
          inline void Set4f (float a,float b,float c,float d) {
            Set3f(a,b,c);
            w = d;
          }
       private:
          float w;
    };
    
    class Matrix4x4 {
       public:
          void setIdentity ();
          Matrix4x4() :
            row1 (1.0f,0.0f,0.0f,0.0f),
            row2 (0.0f,1.0f,0.0f,0.0f),
            row3 (0.0f,0.0f,1.0f,0.0f),
            row4 (0.0f,0.0f,0.0f,1.0f)
          {
          }
       private:
          Vec4f row1, row2, row3, row4;
    };
    
    void Matrix4x4::setIdentity () {
       row1.Set4f(1.0f,0.0f,0.0f,0.0f);
       row2.Set4f(0.0f,1.0f,0.0f,0.0f);
       row3.Set4f(0.0f,0.0f,1.0f,0.0f);
       row4.Set4f(0.0f,0.0f,0.0f,1.0f);
    }
    
    
    int main () {
       Matrix4x4 a;
       a.setIdentity ();
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue creating objects using composition and inheritance
    By ubmattpangolin in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2016, 06:24 PM
  2. Creating Linked List using inheritance or composition?
    By ubmattpangolin in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2015, 12:22 PM
  3. objects and classes in inheritance
    By student111 in forum C++ Programming
    Replies: 3
    Last Post: 06-12-2012, 07:10 AM
  4. Inheritance of objects and linked list
    By rakeshkool27 in forum C++ Programming
    Replies: 7
    Last Post: 11-11-2010, 12:39 PM
  5. Creating Objects On The Heap And Adding Them To A List
    By SomeBeginner in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2002, 04:01 PM

Tags for this Thread