5

There is an array of objects and to add object to it i tries the following:

Shape ShapeList[30];

void addShape(Shape s)
{
  for(int i=0; i<30;i++)
  {
    if(ShapeList[i] != '\0')
        { i++;}
    else
        {
            ShapeList[i]=s;
            numShapes++;
            break;
        }
   }
}

numShapes is an integer variable, Shape is the class and ShapeList is the array of objects. But the compiler gives an error in this way that != operator is not allowed. So how can i implement this?

3
  • ShapeList is a class like Shape? If so you should be calling the instance of ShapeList, not the class name. Commented Feb 21, 2012 at 9:37
  • 1
    ShapeList is the array of objects Commented Feb 21, 2012 at 9:40
  • @user I seriously doubt that the answer you’ve accepted is actually going to help you. Commented Feb 21, 2012 at 11:45

5 Answers 5

4

I think you need to change your 'container' declaration:

Shape *ShapeList[30];

void addShape(Shape *s)
{
  for(int i=0; i<30;i++)
  {
    if(ShapeList[i])
        { i++;}
    else
        {
            ShapeList[i]=s;
            numShapes++;
            break;
        }
   }
}

and call addShape this way:

addShape(new Shape());
Sign up to request clarification or add additional context in comments.

4 Comments

Note that while this answer fixes the obvious problems in the code, it’s still not good code. In particular, it’s prone to memory leaks. C++ offers superior mechanisms for this.
@ konradrudolph can u suggest any other mechanism
@user It really depends on the specific scenario but if you don’t require polymorphism, then don’t use pointers at all; store the objects directly. Otherwise, one simple general-purpose solution is to use std::shared_ptr instead of raw pointers, and either a std::array or std::vector instead of the array.
I keep the answer most simple possible, because the theme of proper memory handling is worth much more questions/answers :)
2

ShapeList[i] returns an object of type Shape. In that case, you can overload operator != (char).

class Shape
{
//your implementation
//
public:
   bool operator != (char x) const
   {
     // comparison logic
   }
};

Also, I believe you have a mistake here:

if(ShapeList[i] != '\0')
    { i++;}

I assume you want to skip this case, but you already increment i in the for loop. You probably want:

if(ShapeList[i] != '\0')
    { continue;}

As others have pointed out, you should use a std::vector instead of a raw array. I initially assumed ShapeList was a wrapper over a std container.

5 Comments

you havent added the object to array
This answer is seriously confused. You might get the code to compile like this but it’s certainly not what the OP wants.
@KonradRudolph I was assuming he wanted to compare a Shape with a char.
@Luchian Obviously. But I doubt that that’s the case, otherwise the question would have been completely different. My guess is that chac’s answer is more or less right (but his minimal fix isn’t a good solution to the problem either).
@KonradRudolph might be, if that is indeed what the op wants to do. We can just assume.
1
Shape * ShapeList[30];   
  numShapes=0;   

     void addShape(Shape* s)
        {
            if( i>=30)
                return;
            ShapeList[numShapes++]=s;      // provided you need to insert at end
        }

You can't use \0 because it's an array, not a string.

storing the whole object as such is an overhead with memory. pointers are a better choice unless you have local variables going out of scope problems. and if STL and Vectors is not beyond your scope of the project you are on to try using it. in which you can use pushback() or pushfront()

6 Comments

how does this answer the question?
do you find any problem with this?
It doesn't answer the question.
he want to implement an array of objects. and this does solve it
@CodingMastero: If you want to add something to your answer, edit your answer. Don't make it a comment. Specifically, your suggestion about using the STL should be an edit, not a comment.
|
0

You did not specify how ShapeList is declared.

With the != operator you compare it to the character NUL, while 4 lines below you assign it a Shape object.

What i think you are trying to achieve, is: find an empty slot in the array of pointers to Shape, and store the Shape there.

But probably better is to use either a std::vector, or std::list, and push_back your shape.

Another thing you have to ask yourself: do i want to store copies of my Shape object, or pointers?

Comments

0

Unless you have a conversion operator in the Shape class to convert it to a character, or a not-equal comparison operator that takes a character as argument, you can not compare a Shape object to a char like you do.

You should be using a std::vector or std::array and use the at member function to see if entry exists or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.