## Vectors and Classes in C++ (very basic question)

Moderators: phlip, Moderators General, Prelates

Durandal
Posts: 659
Joined: Thu Nov 08, 2007 12:12 am UTC

### Vectors and Classes in C++ (very basic question)

Right, I'm taking a computer science course at university, and right now we're doing vectors and classes. I was playing around and made a simple program to combine these two concepts:

Code: Select all

`#include <iostream>#include <vector>using namespace std;class Point{public:   Point(int,int);   int xcoor() const;   int ycoor() const;private:   int x;   int y;};Point::Point(int a, int b){   x = a;   y = b;}int Point::xcoor() const{   return x;}int Point::ycoor() const{   return y;}int main(){   vector<Point> xy(2);   xy = Point(1,1);   xy = Point(3,3);   cout << xy.xcoor() << ',' << xy.ycoor() << endl;   cout << xy.xcoor() << ',' << xy.ycoor() << endl;   return 0;}`

As you can see, all it does is create two objects of type Point (in this case points with (x,y) coordinates) and then prints out their respective x and y coordinates. This worked perfectly when instead of a vector I used:

Point p1 (1,1), p2 (3,3);

Now I get a compiler error telling me there is no matching function call to Point::Point. I assume this is a really simple syntactical error having to do with what I put as parameters in the constructor (possibly has to do with overloading?), but I can't find anything telling me what I'm doing wrong in my textbook/online. Trial & error has also been unsuccessful thus far.

Thanks for any help.

Area Man
Posts: 256
Joined: Thu Dec 25, 2008 8:08 pm UTC
Location: Local

### Re: Vectors and Classes in C++ (very basic question)

You wrote a constructor that takes 2 int arguments, which means that the compiler will no longer create a default constructor for you. You can add default arguments to the one you have.
Bisquick boxes are a dead medium.

Durandal
Posts: 659
Joined: Thu Nov 08, 2007 12:12 am UTC

### Re: Vectors and Classes in C++ (very basic question)

I do not understand. Could you provide an example?

Area Man
Posts: 256
Joined: Thu Dec 25, 2008 8:08 pm UTC
Location: Local

### Re: Vectors and Classes in C++ (very basic question)

Bisquick boxes are a dead medium.

Durandal
Posts: 659
Joined: Thu Nov 08, 2007 12:12 am UTC

### Re: Vectors and Classes in C++ (very basic question)

Okay, so here is my new main function (everything else is the same):

Code: Select all

`int main(){   vector<Point> xy(2, Point(1,1));   cout << xy.xcoor() << ',' << xy.ycoor() << endl;   cout << xy.xcoor() << ',' << xy.ycoor() << endl;   return 0;}`

Everything compiles and works, except I can only have one repeating object in the vector if I initialize it like that. Is there any way I can have different objects (for instance, a point with coordinates (3,3)) using vectors?

Edit: Nevermind, I have figured it out. You can initialize it as anything, as long as you initialize it as something. Then after that you can put in individual objects. Thanks.

Ran4
Posts: 131
Joined: Mon May 04, 2009 2:21 pm UTC

### Re: Vectors and Classes in C++ (very basic question)

Now, just a follow-up question, wouldn't it be inefficient to have to call the constructor twice? Both when creating the vector and when setting it to something.

Maybe your default constructor (as in the one you call when creating the vector, which could be your own constructor) should do just about nothing, so that the performance difference will be small? E.g. setting all the values of your variables. There's no point in doing that twice.