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

"Please leave a message at the beep, we will get back to you when your support contract expires."

Moderators: phlip, Moderators General, Prelates

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

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

Postby Durandal » Sun Aug 02, 2009 4:38 pm UTC

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[0] = Point(1,1);
   xy[1] = Point(3,3);
   cout << xy[0].xcoor() << ',' << xy[0].ycoor() << endl;
   cout << xy[1].xcoor() << ',' << xy[1].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.

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

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

Postby Area Man » Sun Aug 02, 2009 4:53 pm UTC

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.

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

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

Postby Durandal » Sun Aug 02, 2009 4:56 pm UTC

I do not understand. Could you provide an example?

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

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

Postby Area Man » Sun Aug 02, 2009 4:59 pm UTC

Bisquick boxes are a dead medium.

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

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

Postby Durandal » Sun Aug 02, 2009 5:24 pm UTC

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[0].xcoor() << ',' << xy[0].ycoor() << endl;
   cout << xy[1].xcoor() << ',' << xy[1].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.

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

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

Postby Ran4 » Tue Aug 04, 2009 6:39 pm UTC

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.


Return to “The Help Desk”

Who is online

Users browsing this forum: No registered users and 5 guests