0

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);

}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Stradigos
  • 814
  • 1
  • 13
  • 29

4 Answers4

5

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.

Jason
  • 31,834
  • 7
  • 59
  • 78
  • Yeah, that was definitely a bad example I choose to base things off of. Thanks for the help, I understand better now. I still have much to learn on pointers. It's still throwing some errors though. It's saying Vector2D is an undeclared identifier. There are some other errors too, but I know they are because of the above one. I don't understand why it wouldn't be an identifier after what we just did. – Stradigos May 21 '11 at 20:23
  • Are you creating a vector object using syntax like `Vector2D vector(1,2);`? Also are you trying to define `Vector2D` inside a header file, or inside a code-module (i.e., a .cpp file)? – Jason May 21 '11 at 20:32
  • You can see a working example of your vector struct in-use here http://ideone.com/wrg9U – Jason May 21 '11 at 20:38
  • Yes, I'm trying to use it with syntax like that, no I'm not trying to define it inside a header file, yes I am trying to define it inside a .cpp file. I have another struct in this .cpp file working fine. Should I move this one? – Stradigos May 21 '11 at 20:38
  • You might have accidentally forgotten a semi-colon, bracket, etc. on another struct/class declaration that is messing up the declaration of your `Vector2D` struct. So even though the syntax for Vector2D struct is fine, the C++ parser can't parse it correctly into a proper identifier. It's thinking the structure is part of something else. – Jason May 21 '11 at 20:42
  • I'm doing that code snippet verbatim! My solution file is one that my prof gave out, and it's using the irrlicht engine. Unfortunately, we can only edit the one cpp file. I'm not even sure we are allowed to add files to the project. We don't have to create a vector struct, but it's so much easier to work with coordinates like that =P – Stradigos May 21 '11 at 20:43
  • WOW. NEVER MIND. I had the struct declared at the end... moved it to the top and it worked. Lesson learned... wow. I'm retarded. Thanks for the help! – Stradigos May 21 '11 at 20:46
2

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.

andrewdski
  • 5,255
  • 2
  • 20
  • 20
  • Agreed, thanks for the help. It was stupid to try and use pointers without really knowing what they do fully. C++ isn't really something you can hack together real quick with if you're new, lol. – Stradigos May 21 '11 at 20:25
0

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?

littleadv
  • 20,100
  • 2
  • 36
  • 50
0

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.

Jesufer Vn
  • 13,200
  • 6
  • 20
  • 26
  • 1
    No, never mix `malloc`/`free` and `new`/`delete`/`delete[]`. It will break, crash and burn; especially for non-POD types. – Cat Plus Plus May 21 '11 at 19:49
  • Yeah, `free()` will just destroy the memory allocated to the pointer, but will not call the non-POD object's destructor, which will be very bad news ... – Jason May 21 '11 at 19:52
  • 1
    @Jason: Or it might be a completely different allocator, and it ends up corrupting the heap or crashing the program. – Cat Plus Plus May 21 '11 at 19:56