Array of objects

This is a discussion on Array of objects within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to create an array of objects but I keep getting errors. Help me please. The class ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    75

    Array of objects

    Hi,

    I am trying to create an array of objects but I keep getting errors.
    Help me please.
    The class is correct (no errors there).

    Code:
    #include<iostream>
     
    class Dominos
    {
    private:
       float x;
       float z;
       float angle;
    
    
    public:
       Dominos(float y,float z,float angle);
       float getX();
       float getZ();
       float getA();
       
       
    };
     
    Dominos::Dominos(float x,float z,float angle)
    {
      this->x = x;
      this->z = z;
      this->angle = angle;
    }
     
    float Dominos::getX()
    {
       return x;
    }
     
    float Dominos::getZ()
    {
       return z;
    } 
    float Dominos::getA()
    {
       return angle;
    } 
    
    int main()
    {
       Dominos dom = new Dominos[100];
       dom[0] = new Dominos(1,2,3);
       printf("X %f\n",dom[0].getX());
        printf("Z %f\n",dom[0].getZ());
    	 printf("A %f\n",dom[0].getA());
    
    
       return 0;
    }
    thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,788
    Moved to C++ Programming forum.

    Quote Originally Posted by jordanguyoflove
    I am trying to create an array of objects but I keep getting errors.
    You should post the error messages. If not, next time I'll just tell you that I have a solution, but I will neglect to tell you what it is.

    Consider this line:
    Code:
    Dominos dom = new Dominos[100];
    It has a syntax error and should be:
    Code:
    Dominos* dom = new Dominos[100];
    But then there would be another problem: the above code invokes the default constructor for Dominos, but none is available. You also need to define the default constructor for Dominos since you declared another constructor for it.

    Of course, once you follow my suggestion, this line becomes problematic since dom[0] is a Dominos object, not a pointer:
    Code:
    dom[0] = new Dominos(1,2,3);
    Oh, and then when you are done with dom, you should destroy the array with delete[]. Then again, it would be even better to #include <vector> and use a std::vector<Dominos>.

    Quote Originally Posted by jordanguyoflove
    The class is correct (no errors there).
    However, the class is not const correct. The getX, getZ and getA member functions should be declared const.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 08:24 AM

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