By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

I need some help...

P: 5
Can someone offer some insight to help correct this piece of code?

Expand|Select|Wrap|Line Numbers
  1. TwoDShape *shapes[5];
  2.  
  3.     shapes[0] = &Triangle("right", 8.0, 12.0);
  4.     shapes[1] = &Rectangle(10);
  5.     shapes[2] = &Rectangle(10, 4);
  6.     shapes[3] = &Triangle(7.0);
  7.     shapes[4] = &TwoDShape(10, 20, "generic");
The compiler says "[Warning] taking address of temporary" for each of the shapes assignments. I think I can see the problem, I'm just a newbie (this is a code snippet from a c++ intro book), and I don't know to fix it.

Thanks.
Mar 15 '07 #1
Share this Question
Share on Google+
5 Replies


Ganon11
Expert 2.5K+
P: 3,652
Maybe you should split each line like:

Expand|Select|Wrap|Line Numbers
  1. shapes[0] = &Triangle(whatev);
to

Expand|Select|Wrap|Line Numbers
  1. Triangle *temp = new Triangle(whatev);
  2. shapes[0] = temp;
I think your code is creating the object, getting its address, and then setting that address to the array - but when the function exits, the temporary object that was created is deleted. You need to create the object using new so that it won't be deleted when the function exits.

You may also be able to write

Expand|Select|Wrap|Line Numbers
  1. shapes[0] = new Triangle(whatev);
but without testing, I couldn't be sure.
Mar 15 '07 #2

P: 5
This solution worked:

Expand|Select|Wrap|Line Numbers
  1. shapes[0] = new Triangle(whatev);
The book I'm using doesn't even cover the new operator until the last chapter (I checked the index), so I never would have guessed to use it.

Thank you. :)
Mar 15 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Odd that your book would include this code, but not realize the error being generated. Perhaps this is included in main() and not a function, in which case the warning never causes a problem? In any case, it's good that you figured it out.
Mar 15 '07 #4

P: 39
Do not forget, if you are going to use the "new" operator, it must be accompanied with the "delete" operator as well. Otherwise, memory leaks can occur.
Mar 15 '07 #5

P: 5
This code was in main(), and the program compiled, but the output was totally screwy. It was supposed to output this:
Expand|Select|Wrap|Line Numbers
  1. Object is triangle
  2. Area is 48
  3.  
  4. Object is rectangle
  5. Area is 100
  6.  
  7. Object is rectangle
  8. Area is 40
  9.  
  10. Object is triangle
  11. Area is 24.5
  12.  
  13. Object is generic
  14. Error: area() must be overridden.
  15. Area is 0
Using this loop (where area() is a virtual function from the base class that should be overridden by triangle or rectangle's area()):
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i < 5; i++) {
  2.             cout << "Object is " <<
  3.                  shapes[i]->getName() << "\n";
  4.  
  5.             cout << "Area is " << 
  6.                  shapes[i]->area() << "\n";
  7.  
  8.             cout << "\n";
  9.             }
But before I added the new operator, the output was this:
Expand|Select|Wrap|Line Numbers
  1. Object is generic
  2. Error: area() must be overridden.
  3. Area is 0
  4.  
  5. Object is generic
  6. Error: area() must be overridden.
  7. Area is 0
  8.  
  9. Object is generic
  10. Error: area() must be overridden.
  11. Area is 0
  12.  
  13. Object is generic
  14. Error: area() must be overridden.
  15. Area is 0
  16.  
  17. Object is generic
  18. Error: area() must be overridden.
  19. Area is 0
  20.  
  21.  
Mar 15 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.