-1

I am trying to create Voronoi diagram for some given points. Each points have different attributes and I want to denote it as color. To map my own Point structure with Boost Point concept, I have written some code. I have the following setup:

struct Point {
  double a;
  double b;
  Point(double x, double y) : a(x), b(y) {}
};

// This Point structure is mapped to Boost Point concept. Code emitted 

I have another structure as :

struct Point_Collection {
  Point xy(double x, double y);
  short color;

};

Visual Studio created an automatic definition as :

Point Point_Collection::xy(double x, double y)
{
    return Point();
}

Now if I try to instantiate an object of Point_collection as:

std::vector<Point_Collection> *test;
test = new std::vector<Point_Collection>();

Point_Collection xy_color;

for (int i = 0; i < 5000; i++) {
    xy_color.xy(rand() % 1000, rand() % 1000);
    xy_color.color = rand() % 17;
    test->push_back(xy_color);
}

I get an error.

 error C2512: 'Point': no appropriate default constructor available

Can someone point me in the right direction why is this happening?

Mojo Jojo
  • 356
  • 3
  • 14
  • Whats error you get? – MKR Jul 23 '17 at 08:02
  • 1
    What's purpose of `Point xy(double x, double y);` declaration as part of member of `Point_Collection`? Why cant you just declare `Point xy`. – MKR Jul 23 '17 at 08:04
  • `xy` is a member function, you would have to write `xy_color.xy(.....);` . And provide a body for the function – M.M Jul 23 '17 at 08:16
  • Please clarify in addition to the exact error message: Did you really want to declare a member function called `xy` that takes two `double`s and returns a `Point`? In your third snippet are you trying to access the `xy` and `color` members of `xy_color`? If not, what are `xy` and `color` in that context? – besc Jul 23 '17 at 08:17

1 Answers1

3

Point xy(double x, double y); declares a member function in Point_Collection that is identified by xy, accepts two doubles and returns a Point object by value.

If you want a simple aggregate that holds a point, the C++11 and onward way would be to define it like this:

struct Point_Collection {
  Point xy;
  short color;
};

Point_Collection xy_color{ { rand()%100, rand()%100 }, static_cast<short>(rand()%16)};

The above is a simple aggregate initialization using value initialization syntax. You should prefer it for two reasons:

  1. It will not allow narrowing conversions. (Which int to short is, therefore the cast).
  2. It's easy to implement. It requires no typing if your class has all public members.

(Also rand has better alternatives in C++11, check out the header <random>)


If you don't have access to C++11, then you can either write a constructor for Point_Collection.

struct Point_Collection {
  Point xy;
  short color;

  Point_Collection(Point xy, short color)
    : xy(xy), color(color) {}
};

Point_Collection xy_color (Point(...,...), ...);

Or use aggregate initialization with more verbose syntax:

struct Point_Collection {
  Point xy;
  short color;
};

Point_Collection xy_color = { Point(rand()%100, rand()%100), rand()%16 };

(Since the above is C++03, rand()%16 will be silently converted to short, despite it being narrowing).

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Can you please suggest me a resource where I can read information like this about C++. Also , honestly I did not understand the mapping of my Point structure to Boost Point concept, I just copy pasted from Boost documentation and it worked. Where I can learn those basics? – Mojo Jojo Jul 23 '17 at 08:38
  • @MojoJojo - The proper way to learn C++ is from a [good, vetted, book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – StoryTeller - Unslander Monica Jul 23 '17 at 08:39
  • Thanks, was just going through that thread. Seems I have lot to learn. Thanks again. – Mojo Jojo Jul 23 '17 at 08:43