Creating 2-dimensional vector in class C++ - c++

I need to create a vector of vectors full of integers. However, I continuously get the errors:
error: expected identifier before numeric constant
error: expected ',' or '...' before numeric constant
using namespace std;
class Grid {
public:
Grid();
void display_grid();
void output_grid();
private:
vector<int> row(5, 0);
vector<vector<int> > puzzle(9, row);
int rows_;
int columns_;
};

You cannot initialize the member variables at the point where you declare them. Use an initialization list in the constructor for that:
Grid::Grid()
: row(5,0), puzzle(9, row),
rows_(5), columns_(9)
{
}

C++ class definitions are limited in that you cannot initialise members in-line where you declare them. It's a shame, but it's being fixed to some extent in C++0x.
Anyway, you can still provide constructor parameters with the ctor-initializer syntax. You may not have seen it before, but:
struct T {
T() : x(42) {
// ...
}
int x;
};
is how you initialise a member, when you might have previously tried (and failed) with int x = 42;.
So:
class Grid {
public:
Grid();
void display_grid();
void output_grid();
private:
vector<int> row;
vector<vector<int> > puzzle;
int rows_;
int columns_;
};
Grid::Grid()
: row(5, 0)
, puzzle(9, row)
{
// ...
};
Hope that helps.

You can't initialize a member in a class declaration unless it's const static, because in C++ no code is being run/generated when you are declaring a class. You'll have to initialize them in your constructor.

You should initialize members in the class constructor, not the declaration. The following doesn't seem to be right in any way:
vector<int> row(5, 0);
vector<vector<int> > puzzle(9, row);
If row and puzzle are functions - the parameters should be types. If they're member variables - initialize them in the class constructor.

You cannot initialize mutable members as a part of class definition itself. Instead do assign it in in the constructor.
// ....
Grid()
{
row.resize(5,0) ;
puzzle.resize(9,row) ;
}
private:
vector<int> row;
vector<vector<int> > puzzle ;
// ..

Related

How to use () initializers for vector in class?

This code gives an error
class Board {
vector<vector<int>> sudoku(9, vector<int>(9));
// Error; Expected a parameter declarator
};
How can I fix it?
You can't use parentheses to directly initialize a member when you do in class initialization. You have to either brace ({}) or equal (= ...;) initialize the member. That means you need to refactor it to have the form of
vector<vector<int>> sudoku = vector<vector<int>>(9, vector<int>(9));
// or
vector<vector<int>> sudoku{9, vector<int>(9)};
One way:
class Board {
vector<vector<int>> sudoku = vector<vector<int>>(9, vector<int>(9));
};
Second way:
class Board {
public:
Board() : sudoku(9, vector<int>(9)) {}
private:
vector<vector<int>> sudoku;
};

How to do constructor for 2D vector in C++?

I tried initializing a 2D vector with a constructor in 3 different ways but always get an
"error: no matching function to call"
Could you tell me where I am wrong?
class Node
{
public:
int to;
int length;
Node(int to, int length) : to(to), length(length){}
};
class Graph
{
public:
vector<vector<Node>> nodes_list;
int n;
Graph();
};
Graph::Graph(){
nodes_list = vector<vector<Node> >(n, vector<Node>(n,0x3fffffff));
}
vector<Node>(n,0x3fffffff);
is (roughly) equivalent to:
vector<Node> v;
for ( size_t i = 0; i < n; i++ )
{
v.push_back(Node(0x3fffffff));
}
As your Node class doesn't have a constructor taking a single integer this fails to compile. The correct code is:
vector<Node>(n,Node(0x3fffffff,0));
By the way I assume you have using namespace std; in your header for Graph, don't do that, it will cause you issues at some point.
Your code has two problems:
At the following line, you should have provided the parameters for
constructing the Node, which are to and legth.
vector<vector<Node>>(n, vector<Node>(n,0x3fffffff));
// ^^^^^^^^^^^--> here
In Graph, the member n is un-initialized, at the
point, you call the default constructor. That would lead you to have
a garbage value in n and hence the size of the nodes_list would
be undefined.
The fixed code will look like:
struct Node
{
int _to;
int _length;
Node(int to, int length) : _to{ to }, _length{ length } {}
};
class Graph
{
using VecNode = std::vector<Node>; // type alias for convenience
private:
int _n;
std::vector<VecNode> _nodes_list;
public:
Graph()
: _n{ 2 } // initialize _n
, _nodes_list{ _n, VecNode(_n, { 1, 3 }) }
// ^^^^^^-> initialize with the default 'Node(1, 3)'
{}
};
Also some suggestions:
Use member initializer
lists
to initialize the vector, instead of creating and assign to it.
It's not a good idea to name both constructor parameters and the
members with same in Node. At some point, that may lead to
confusions.

initializing a vector of vectors of a user defined

I have this struct
struct myStruct {
int a;
int b;
}
I want to create a vector <vector<myStruct> > V and initialize it to n empty vectors of type vector<myStruct>
I'm trying to use the the fill constructor
like this:
vector<edge> temp;
vector<vector<edge> > V(n, temp);
This code works fine in main, but when I have V inside a class how can I do that inside the class constructor.
EDIT:
when I do it in my class constructor I get the following error:
no match for call to '(std::vector<std::vector<edge> >) (int&, std::vector<edge>&)'
the code generating the error is:
vector<myStruct> temp;
V(n, temp); // n is a parameter for the constructor
First, note that temp is not necessary: your code is identical to
vector<vector<edge> > V(n);
Now to your main question: When your vector is inside a class, use initializer list if the member is non-static, or initialize the member in the declaration part if it is static.
class MyClass {
vector<vector<edge> > V;
public:
MyClass(int n) : V(n) {}
};
or like this:
// In the header
class MyClass {
static vector<vector<edge> > V;
...
};
// In a cpp file; n must be defined for this to work
vector<vector<edge> > MyClass::V(n);
Just omit temp. The constructor for the class that V is inside should look like:
MyClass(size_t n) : V(n) {}
class A
{
private:
std::vector<std::vector<myStruct>> _v;
public:
A() : _v(10) {} // if you just want 10 empty vectors, you don't need to supply the 2nd parameter
A(std::size_t n) : _v(n) {}
// ...
};
You use initializer lists for this kind of initialization.

How can I create an array of class objects whose constructor require few arguments?

I've read about solution const A a[3] = { {0,0}, {1,1}, {2,2} }, but in my program const can't be used:
class Paper: public PaperQueue{
...
protected:
typedef int (Utils::*funcPtr) (int, int); //I use external function there
funcPtr p;
Utils* fptr;
public:
int pricefunc(){
addprice = (fptr->*p) (t,price);
}
Paper(int n, unsigned int pr):PaperQueue(n){
...
p=&Utils::commonpricefunc;
}
void Put(int a){
...
}
...
}
class Bank{
...
void Buy(Paper &p){
(/*this function modifies many parameters in 'p'*/)
...
}
...
}
int main(){
Bank B;
int pn=5;
/* ? */ const Paper p[pn] = {{5,15},{5,15},{5,15},{5,15},{5,15}}; /* ? */
int paperloop=0;
...
p[paperloop].Put(p[paperloop].addprice);
B.Buy(p[paperloop]);
...
That gives me a LOT of errors(with pricefunc(),Put(),Buy(),...), or just "variable-sized object ā€˜pā€™ may not be initialized". Is there any way to make this array work? (Everything works fine if not to pass any parameters to constructor!)
You can't use initializer lists for classes (non-PODs) because that would bypass the call to the constructor. You'll have to change it to a POD or use std::vector in the following ways:
If the class is copyable, as it appears to be, you can create a std::vector and fill it with the values you want:
const vector<Paper> papers(5, Paper(5, 15));
If you want to initialize it with different values, you can use an initializer list, but this is only supported in C++11:
const vector<Paper> papers = {Paper(1, 1), Paper(2, 2)};
Without C++11, you'll have to add the elements one by one, but then you can't make the vector const:
vector<Paper> papers;
papers.push_back(Paper(1, 1));
papers.push_back(Paper(2, 2));
Please check the code below, it can be compiled:
class Paper {
public:
int x, y;
};
int main() {
Paper p[5] = {{5,15}, {5,15}, {5,15}, {5,15}, {5,15}};
}
Please refer this post for more details, I think it explains very well

Problems Initializing Vector

I'm new to c++ so I'm kind of consfused
I wanted to do something like that:
`
int max = 30;
class MyClass{
vector<int> data(max);
};
but it wasn't working, because it was not recognizing that "max" was that int I had just initialized.
so i changed to that:
class MyClass{
MyClass();
int max;
vector<int> data(max);
}
MyClass::MyClass(){
max = 40;}
Don't work unless I initialize the vector in the constructor, but I don't know the correct sintax.
How can I make this work? All I want is to initialize "max" and then use it as the initial size of the vector.
You prof/teacher should have told you about initializer lists. The syntax looks something like this:
class MyClass {
std::vector<int> data;
public:
MyClass(int max) : data(max) { }
};