mdeforge
mdeforge

Reputation: 874

No instance of constructor?

I'm trying to create my own Vector2D class, similar to that of XNA's, to store coordinates in. Following the example of constructors found here, I created the code below. However, I get an error saying that there is no instance of constructor "Vector2D::Vector2D" that matches the argument list. I don't see how that can be... What seems to be my problem?

struct Vector2D {
    Vector2D(int *varX, int *varY);
    ~Vector2D();
    private: int *X, *Y;
};

Vector2D::Vector2D(int *varX, int *varY) {
    X = varX;
    Y = varY;
}

Vector2D::~Vector2D() {
    free(X);
    free(Y);

}

Upvotes: 0

Views: 4345

Answers (4)

Jason
Jason

Reputation: 32510

You are most likely trying to create your Vector2D class like this:

Vector2D vector(1, 2);

You can't do that using pointers to int like you've done in your Vector2D constructor definition because those values are not pointers, they are integer constants. Change to simple int objects like this:

struct Vector2D 
{
    Vector2D(int varX, int varY): X(varX), Y(varY) {}  //initialization list
    //~Vector2D();  delete the destructor since it's not needed anymore

    int X, Y;
};

There's also no need for the destructor since your data objects are not pointers, and therefore there is no extra cleanup of allocated memory on the heap needed at the object's destruction. I've just commented it out so you can see it's not needed anymore.

Finally, you probably don't want your data-members as private if you're using a struct ... you can do it, but you'll then have to add some functions to access those data memebers (i.e., if you declare them private then you can't do things like vector.x = 5;). Typically you'd use a class if you want to default to private access, as struct defaults to public access.

Upvotes: 5

Jesufer Vn
Jesufer Vn

Reputation: 13760

It seems to be that the problem is that you are passing int's instead of pointers to int.

If you want to keep the parameters as pointers to integer, you have to pass your parameters as pointers, or create a new int while passing the parameter this way:

Vector2D v(new int(4), new int(8));

As you can see, I pass as parameters the integers 4 and 8.

Be aware of destroying the variables using delete instead of free. Or you can also adapt the problem using malloc/free.

Here you do need a destructor because you are allocating space on-demand while passing the parameters.

Upvotes: 0

littleadv
littleadv

Reputation: 20272

The problem is not in definition, but in how you use the class. For example, code like this:

int main(){
    Vector2D v2D_none;
}

Will yield the error you're talking about, because there's no default (no parameters) constructor. Calling Vector2D v2D(1,2) will also fail because of the type mismatch.

Also, having free in the destructor is a bad choice, as was pointed out in comments. Why do you use pointers anyway?

Upvotes: 0

andrewdski
andrewdski

Reputation: 5505

I think you chose a bad example to base this on. The example is a string class, so it takes pointers, allocates and frees storage etc. Strings are variable length, so they need to do stuff like that.

I'm guessing the error came from something like

Vector2D v(3, 5);

The problem is your constructor takes int* arguments, and you are calling it with int arguments. You should not be using pointers here.

Upvotes: 2

Related Questions