Some kind of class truble

This is a discussion on Some kind of class truble within the C++ Programming forums, part of the General Programming Boards category; I get an error when running this code: Code: class Material { public: Material(int _r, int _g, int _b); ~Material(); ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Some kind of class truble

    I get an error when running this code:

    Code:
    class Material
    {
          public:
                 Material(int _r, int _g, int _b);
                 ~Material();
                 
                 void setColor(int _r, int _g, int _b);
                 int getR();
                 int getG();
                 int getB();
                 
                 // Operator overloading
                 Material& operator =(const Material& m)
                 {
                     r = m.r;
                     g = m.g;
                     b = m.b;
                     return (*this);
                 }
                 
          private:
                  int r,g,b;
                
          
    };
    
    class Sphere
    {
          public:
                 Sphere();
                 ~Sphere();
                 
                 void setRadius(float r);
                 float getRadius();
                 
                 void setPosition(Vector3D pos);
                 Vector3D getPosition(); 
                 
                 void setMaterial(Material m);
                 Material getMaterial();
                 
                 void setLight(int l);
                 bool isLight();
          private:      
                  Vector3D position;
                  Material color;
                  float radius;
                  bool light;
    };
    I have no idea what is wrong, but the errors it spits out are:
    Code:
    In constructor `Sphere::Sphere()': 
    no matching function for call to `Material::Material()' 
    candidates are: Material::Material(const Material&) 
    Material::Material(int, int, int)
    And when I press the text, it links to this part of my code:
    Code:
    // Spherer class defenition
    // ------------------------------------------------------------
    Sphere::Sphere()
    {
          position.set(0,0,0);
          radius = 1;
    }
    Thanks for your time

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    color is a Material, and you have to call the constructor because there's not a default constructor when you overload the constructor and don't add a default. Something like this I guess:
    Code:
    Sphere::Sphere(): color(0,0,0)
    {
          position.set(0,0,0);
          radius = 1;
    }

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thank you, that solved it.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    An other quick question, how can I overload Sphere, so that I can do Sphere[]?

  5. #5
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    I think you need a default constructor, a copy constructor, and a destructor. All you're missing is a copy constructor.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> how can I overload Sphere, so that I can do Sphere[]?
    Do you mean you want to make an array of Spheres (you can do that already) or do you want to overload operator[] so that you can get data from the Sphere? If you want to overload operator[], then write that function, but it doesn't seem to make much sense for this type of object.

    >> I think you need a default constructor, a copy constructor, and a destructor. All you're missing is a copy constructor.
    Actually, in the two classes shown I think all three of those are not necessary. The default ones generated by the compiler should be sufficient.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    can someone explain the this in the class ? i havent yet understood what it does

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    You should probably start a new thread with that question so that you don't derail this one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21