• Create Account

#ActualMatt-D

Posted 20 June 2013 - 04:58 PM

Remember to read the error messages -- often the first one already contains a hint, just as in your case:

(did you forget a ';'?)


On a side note, please do NOT do that:

// an array of vectors is a grid, not a vector, so let's call it "grid" -- good variable names are important!
Vector* grid = new Vector[2]; // NO! memory leak if you forget to delete[]

In general, if you're just beginning, you should avoid using "new" like fire -- it implies manual memory management and should only be used if you have specialized needs (and, as such, you're no longer a beginner). Before you even consider using "new", make sure you have read and understood the following: http://www.informit.com/articles/article.aspx?p=1944072

If you meant to use a fixed-size array of "Vector", use an std::array:

std::array<Vector, 2> grid;

http://en.cppreference.com/w/cpp/container/array

If you'd like to have a resizable array instead, there's an std::vector for this:

std::vector<Vector> grid(2);

http://en.cppreference.com/w/cpp/container/vector

To add an element (automatically resizing your grid), simply use the "emplace_back" member function (or, if you'd like to waste some CPU cycles and/or memory, there's always "push_back" doing unnecessary copies (or moves, if you're lucky) ;-]):

A simple example: http://ideone.com/sVw9CO

#include <vector>
#include <iostream>

class Vector
{
public:
float x,y;
};

int main()
{
// uninitialized grid[0] and grid[1]
std::vector<Vector> grid(2);

// (5, 3) stored in grid[2]
// element constructed directly in "grid"
grid.emplace_back(Vector{5.f, 3.f});

// output: 5,3
std::cout << grid[2].x << "," << grid[2].y << '\n';
return 0;
}



HTH! :-)

#1Matt-D

Posted 20 June 2013 - 04:57 PM

Remember to read the error messages -- often the first one already contains a hint, just as in your case:

(did you forget a ';'?)


On a side note, please do NOT do that:

// an array of vectors is a grid, not a vector, so let's call it "grid" -- good variable names are important!
Vector* grid = new Vector[2]; // NO! memory leak if you forget to delete[]

In general, if you're just beginning, you should avoid using "new" like fire -- it implies manual memory management and should only be used if you have specialized needs (and, as such, you're no longer a beginner). Before you even consider using "new", make sure you have read and understood the following: http://www.informit.com/articles/article.aspx?p=1944072

If you meant to use a fixed-size array of "Vector", use an std::array:

std::array<Vector, 2> grid;

http://en.cppreference.com/w/cpp/container/array

If you'd like to have a resizable array instead, there's an std::vector for this:

std::vector<Vector> grid(2);

http://en.cppreference.com/w/cpp/container/vector

To add an element (automatically resizing your grid), simply use the "emplace_back" member function (or, if you'd like to waste some CPU cycles and/or memory, there's always "push_back" doing unnecessary copies (or moves, if you're lucky) ;-]):

A simple example: http://ideone.com/cbJI8P

#include <vector>
#include <iostream>

class Vector
{
public:
float x,y;
};

int main()
{
// uninitialized grid[0] and grid[1]
std::vector<Vector> grid(2);

// (5, 3) stored in grid[2]
// element constructed directly in "grid"
grid.emplace_back(Vector{5.f, 3.f});

// output: 5,3
std::cout << grid[2].x << "," << grid[2].y << '\n';
return 0;
}



HTH! :-)

PARTNERS