Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

C++ pointers to types with no default constructor


  • Please log in to reply

#1
W-Unit

W-Unit

    Member

  • Member
  • PipPipPip
  • 170 posts
Hey guys,

So let's say I have defined the following data types:
struct Point {
  int x;
  int y;

  Point(int ex, int ey) {
	x = ex;
	y = ey;
  }
}

struct Rectangle {
  Point p;
  int w;
  int h;

  Rectangle(Point ep, int ew, int eh) {
	p = ep;
	w = ew;
	h = eh;
  }
}

Now I want to create a bunch of Rectangles using a for loop, and place them in an array, like so.
Rectangle * rect = new Rectangle[10];
for (int x=0; x<10; x++) {
  Rectangle newRect(Point(0,x*10),10,20);
  *rect[x] = newRect;
}
Problem is, the C++ compiler complains on the first line of the above code about not having a default constructor for Rectangle. I can get around this using some absolutely disgusting tricks with void pointers and reinterpret_cast (although it seems to me I shouldn't have to do this), but where I really get stuck is in trying to assign to *rect[x]. When I do this, the compiler complains about not being able to access various private functions of Rectangle, and some error related to "operator ="
That's because in reality, Rectangle and Point are part of a library (and are actually much more sophisticated structures than I wrote above), and modifying those structures is not an option in this case.

Ideally, I'd like to be able to avoid this pointer business altogether and just use a std::vector<Rectangle> instead, but I'll settle for anything that allows me to create and iterate through a collection of Rectangles.

Much appreciated!
  • 0

Advertisements


#2
victor!

victor!

    New Member

  • Member
  • Pip
  • 4 posts
Hello, a way to solve your problem problem is to modify the the rectangle's constructor in the following way:
struct Rectangle
{
	Point p;
	int h, w;
	Rectangle(): p(0,0), h(0), w(0) {}/* a default constructor would be nice to add, incase you want to create a rectangle with no predefined values */
	Rectangle( int x, int y, int _h, int _w ): p(x,y), h(_h), w(_w) {} /* main modification here, since your Point struct doesn't have a copy constructor I think it would be easier if we just gave it the values for the point, instead of another point */
};

And for creating the Rectangles using a std::vector:
#include <vector>
void some_function()
{
	std::vector<Rectangle> r;
	for( int x = 0; x < 10; ++x )
		r.push_back( Rectangle( 0, x*10, 10, 20 ) );
}

That should solve your problems.
Good Luck.
  • 0

#3
W-Unit

W-Unit

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 170 posts

That's because in reality, Rectangle and Point are part of a library (and are actually much more sophisticated structures than I wrote above), and modifying those structures is not an option in this case.

:)

I do appreciate the reply, but are there any other options?
  • 0

#4
victor!

victor!

    New Member

  • Member
  • Pip
  • 4 posts
Sorry, I missed that. Well, the problem is in the struct Rectangle's constructor. It should look like this:
struct Rectangle
{
	Point p;
	Rectangle( Point ep, int x, int y ): p( ep.x, ep.y) { /* code here */ }

	/* more stuff here */

};

Since the Point struct has no default constructor, when you call Rectangle's constructor it tries to initialize every member, including Point p, in their respective default ways( since no other way was specified ), but the Point struct lacks one. But you said they are much more sophisticated then you wrote above, so maybe they will work fine with just:
std::vector<Rectangle> vec;
for( int i = 0; i < 10; ++i )
   vec.push_back( Rectangle( Point( 0, i*10 ), 10, 20 ) );

Maybe I can help you more if you show the actual code for both structures.
Good Luck!
  • 0

#5
Father0fNine

Father0fNine

    Member

  • Member
  • PipPip
  • 29 posts
Here's your problem line:



*rect[x] = newRect;


Most compilers will treat structures just like they do classes. If you declare an explicit constructor (you did) the compiler will not include any other constructor (default constructor, copy constructor, etc).

The proper way to solve this is with the copy constructor. Something like:

Rectangle( const Rectangle &new ) {
	p = new.p;
	w = new.w;
	h = new.h;
  }

Of course, you'll need a copy constructor for Point, too... or you'll have to explicitly set each member in p from new.p.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP