c++ - copy constructor for template class - c++

My class is a matrix composed out of vectors of vectors, and I seem to have problems in my constructor:
#include <vector>
#include <exception>
#include <iostream>
using namespace std;
template<class T>
class Matrix
{
private:
unsigned int _rows;
unsigned int _cols;
vector <vector<T>> _matrix;
public:
const unsigned int INITIAL_ROW_SIZE = 1;
const unsigned int INITIAL_COL_SIZE = 1;
Matrix () : _rows(INITIAL_COL_SIZE), _cols(INITIAL_COL_SIZE),
_matrix(1,vector<T>(1)) {
cout << "ctor" << endl;
}
Matrix (unsigned int rows, unsigned int cols) : _rows(rows), _cols(cols),_matrix(_rows,vector<T>(_cols) {
}
Matrix (const Matrix<T> &other) :Matrix(other._rows, other._cols)
{
for(int i = 0; i < _rows; i++)
{
copy(other._matrix[i].begin(), other._matrix[i].end(), _matrix[i]);
}
};
}
the code will not compile when i try to use the copy constructor,
as in Matric<int> m(5,5); Matrix<int> n=(m); because:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/vector:60:0,
from /cppex3/ex3/Matrix.hpp:8,
from /cppex3/ex3/Tester.cpp:6:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algobase.h: In instantiation of '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const std::vector<int, std::allocator<int> >*; _OI = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >]':
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algobase.h:438:45: required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >; _OI = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >]'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algobase.h:471:8: required from '_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const std::vector<int, std::allocator<int> >*, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >; _OI = std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >]'
/ex3/Matrix.hpp:42:13: required from 'Matrix<T>::Matrix(const Matrix<T>&) [with T = int]'
/ex3/Tester.cpp:25:20: required from here
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algobase.h:394:57: error: no type named 'value_type' in '[01mstruct std::iterator_traits<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >'
typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
^
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/bits/stl_algobase.h:399:9: error: no type named 'value_type' in 'struct std::iterator_traits<std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > >'
&& __are_same<_ValueTypeI, _ValueTypeO>::__value);
^
I suspect my initalization of the _matrix member is not correct, but I'm not sure. it could also by std::copy but the syntax seems correct.

Try with
copy(other._matrix[i].begin(), other._matrix[i].end(),
std::back_inserter(_matrix[i]));
or with
Matrix (const Matrix<T> &other)
: _rows{other._rows}, _cols{other._cols}, _matrix{other._matrix}
{
}

Try this code, it works
Replace your
copy(other._matrix[i].begin(), other._matrix[i].end(), _matrix[i]);
with
copy(other._matrix[i].begin(), other._matrix[i].end(), _matrix[i].begin());
Below is the complete code.
#include <vector>
#include <exception>
#include <iostream>
using namespace std;
template<class T>
class Matrix
{
private:
unsigned int _rows;
unsigned int _cols;
vector <vector<T> > _matrix;
public:
Matrix () : _rows(1), _cols(1),
_matrix(1,vector<T>(1))
{
cout << "ctor" << endl;
}
Matrix (unsigned int rows, unsigned int cols) : _rows(rows), _cols(cols)
{
_matrix=vector< vector<T> >(rows,vector<T>(cols));
}
Matrix (const Matrix<T> &other)
: _rows{other._rows}, _cols{other._cols}, _matrix{other._matrix}
{
for(int i = 0; i < _rows; i++)
{
std::copy(other._matrix[i].begin(), other._matrix[i].end(), _matrix[i].begin());
}
}
};
int main(int argc, char const *argv[])
{
Matrix<int> m(5,5);
Matrix<int> n1(m);
Matrix<int> n2(m);
return 0;
}
Also
Matrix (const Matrix<T> &other) :Matrix(other._rows, other._cols);
replaced it with
Matrix (const Matrix<T> &other) {
Matrix(other._rows, other._cols);
...

Related

Passing arguments to constructor results in an error

I have a constructor which constructs a matrix, represented by a 2D vector, rows and columns number.
Matrix(const vector<vector<T> >* vect, const size_t rows, const size_t cols) :
matrix(&vect), matrixRows(rows), matrixCols(cols){}
I want to find the transpose of a matrix in one of my functions.
Matrix& trans() const
{
vector<vector<T> > trans(matrixCols, vector<T>(matrixRows));
for(unsigned int i = 0; i < matrixRows; i++)
{
for(unsigned int j = 0; j < matrixCols; j++)
{
trans[j][i] = matrix[i][j];
}
}
return Matrix(&trans, matrixCols, matrixRows);
}
The last line results in an error, because passing a vector this way is wrong.
What is the correct way?
Full error:
Multiple markers at this line
- invalid initialization of non-const reference of type 'Matrix<Complex>&' from an rvalue of type
'Matrix<Complex>'
- required from 'Matrix<T>& Matrix<T>::trans() const [with T = int]'
- invalid initialization of non-const reference of type 'Matrix<double>&' from an rvalue of type
'Matrix<double>'
- invalid initialization of non-const reference of type 'Matrix<int>&' from an rvalue of type 'Matrix<int>'
- required from 'Matrix<T>& Matrix<T>::trans() const [with T = Complex]'
- required from 'Matrix<T>& Matrix<T>::trans() const [with T = double]'
constructor error:
Multiple markers at this line
- invalid conversion from 'const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >**' to 'std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::size_type
{aka long long unsigned int}' [-fpermissive]
- candidate expects 3 arguments, 1 provided
- no matching function for call to 'std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::vector(const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > >
>**)'
- no known conversion for argument 1 from 'std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >' to 'const std::vector<std::vector<double, std::allocator<double> >,
std::allocator<std::vector<double, std::allocator<double> > > >*'
- candidate: Matrix<T>::Matrix(const std::vector<std::vector<T> >*, size_t, size_t) [with T = double; size_t = long long unsigned int]
- no known conversion for argument 1 from 'std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >' to 'const std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int>
> > >*'
- candidate: Matrix<T>::Matrix(const std::vector<std::vector<T> >*, size_t, size_t) [with T = int; size_t = long long unsigned int]
- no matching function for call to 'std::vector<std::vector<Complex, std::allocator<Complex> >, std::allocator<std::vector<Complex, std::allocator<Complex> > > >::vector(const std::vector<std::vector<Complex, std::allocator<Complex> >,
std::allocator<std::vector<Complex, std::allocator<Complex> > > >**)'
- invalid conversion from 'const std::vector<std::vector<Complex, std::allocator<Complex> >, std::allocator<std::vector<Complex, std::allocator<Complex> > > >**' to 'std::vector<std::vector<Complex, std::allocator<Complex> >,
std::allocator<std::vector<Complex, std::allocator<Complex> > > >::size_type {aka long long unsigned int}' [-fpermissive]
- no matching function for call to 'std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >::vector(const std::vector<std::vector<double, std::allocator<double> >,
std::allocator<std::vector<double, std::allocator<double> > > >**)'
- invalid conversion from 'const std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >**' to 'std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double,
std::allocator<double> > > >::size_type {aka long long unsigned int}' [-fpermissive]
- no known conversion for argument 1 from 'std::vector<std::vector<Complex, std::allocator<Complex> >, std::allocator<std::vector<Complex, std::allocator<Complex> > > >' to 'const std::vector<std::vector<Complex, std::allocator<Complex> >,
std::allocator<std::vector<Complex, std::allocator<Complex> > > >*'
- candidate: Matrix<T>::Matrix(const std::vector<std::vector<T> >*, size_t, size_t) [with T = Complex; size_t = long long unsigned int]
First off, you probably want the Matrix constructor to take the vector by reference instead of address:
Matrix(const vector<vector<T>>& vect, const size_t rows, const size_t cols)
: matrix(vect), matrixRows(rows), matrixCols(cols)
{
}
(Note that this will cause the vector to be copied into matrix. Depending on the size of the vector, this might be fine - it really depends on the performance requirements of your program.)
Secondly, trans() should not return a reference to a Matrix. Since you are returning the value of a local, the variable will be destroyed when execution leaves the function, so the caller would have no way to make use of it.
Combined with the change to the Matrix constructor above, trans() should compile now:
Matrix trans() const
{
vector<vector<T>> trans(matrixCols, vector<T>(matrixRows));
for(unsigned int i = 0; i < matrixRows; i++)
{
for(unsigned int j = 0; j < matrixCols; j++)
{
trans[j][i] = matrix[i][j];
}
}
return Matrix(trans, matrixCols, matrixRows);
}

Passing vector to a thread function in c++

I am trying to run a multi-threaded matrix sum function, so that each line will sum up in a different thread. I have tried implementing all workarounds for passing vector to a cpp thread function in a template class but I still get this common error.
For the code:
template <typename T> class Matrix
{
// variables for matrix size and variables in a one dimension vector
unsigned int _rows;
unsigned int _cols;
vector<vector<T> > _matrix;
// Matrix template class functions declarations (all common operators and constructors)
void sumLine(vector<T>& first, vector<T>& second, vector<T>& result);
Matrix<T> operator+(const Matrix<T> & other) const;
};
// Matrix template class functions implmentations
template <typename T> void Matrix<T>::sumLine(vector<T>& first, vector<T>& second, vector<T>& result)
{
for (unsigned int colIdx = 0; colIdx < _cols; colIdx++)
{
result[colIdx] = first[colIdx] + second[colIdx];
}
}
template <typename T> Matrix<T> Matrix<T>::operator+(const Matrix<T> & other) const
{
vector<thread> threads;
vector<vector<T> > results;
vector<T> newRow(_cols);
results.resize(_rows, newRow);
for (unsigned int rowIdx = 0; rowIdx < _rows; rowIdx++)
{
vector<T> first = _matrix[rowIdx];
vector<T> second = other._matrix[rowIdx];
vector<T> result = results[rowIdx];
threads.push_back(thread(Matrix<T>::sumLine, std::ref(first), std::ref(second), std::ref(result)));
}
for (unsigned int thrdIdx = 0; thrdIdx < _rows; thrdIdx++)
{
threads[thrdIdx].join();
}
// do something with vector<vector<T>> results
}
and still after compiling with gcc I get:
In file included from /usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/thread:39:0,
from Matrix.hpp:12,
from main.cpp:13:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/functional: In instantiation of 'struct std::_Bind_simple<std::_Mem_fn<void (Matrix<Complex>::*)(std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&)>(std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >)>':
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/thread:137:47: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (Matrix<Complex>::*)(std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&); _Args = {std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >}]'
Matrix.hpp:404:102: required from 'Matrix<T> Matrix<T>::operator+(const Matrix<T>&) const [with T = Complex]'
main.cpp:59:14: required from here
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/functional:1665:61: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Matrix<Complex>::*)(std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&)>(std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >)>'
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/include/c++/functional:1695:9: error: no type named 'type' in 'class std::result_of<std::_Mem_fn<void (Matrix<Complex>::*)(std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&, std::vector<Complex, std::allocator<Complex> >&)>(std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >, std::reference_wrapper<std::vector<Complex, std::allocator<Complex> > >)>'
_M_invoke(_Index_tuple<_Indices...>)
When Complex is a complex number class I wrote for the matrix template class, and my main function is trying to use this calculation with it. What is wrong here? If it is in the complex class, is there a simpler way to pass the parameters to the thread function to avoid this?
you should post your Complex class, it may be the problem.
also in your code:
vector<T> first = _matrix[rowIdx];
vector<T> second = other._matrix[rowIdx];
vector<T> result = results[rowIdx];
you should write it like this: vector<T>& first = _matrix[rowIdx];
because you are copying those vectors... or even const vector<T>& first = _matrix[rowIdx]; if you don't modify them.
then you can remove the std::ref
the following code compiles (with clang 3.5) and avoids passing references to temporaries to any thread ...
#include <vector>
#include <thread>
#include <stdexcept>
using namespace std; // for expose only (don't do this in real code!!)
template <typename T> class Matrix
{
using row = vector<T>;
size_t _rows, _cols;
vector<row> _matrix;
Matrix(vector<row>&&); // defined elsewhere
row sumLine(row const& first, row const& second) const
{
row result; // don't tempt the compiler to default
result.reserve(_cols); // initialise result[i] for all columns
for(size_t c=0; c!=_cols; ++c)
result.emplace_back(first[c]+second[c]);
return result; // return is fast (no copy!)
}
Matrix<T> operator+ (const Matrix<T>& other) const;
};
template <typename T>
Matrix<T> Matrix<T>::operator+(const Matrix<T>&other) const
{
if(other._cols != _cols)
throw std::runtime_error("column number mismatch in Matrix+Matrix");
if(other._rows != _rows)
throw std::runtime_error("row number mismatch in Matrix+Matrix");
vector<thread> threads; threads.reserve(_rows);
vector<row> result(_rows);
for(size_t r=0; r!=_rows; ++r)
threads.emplace_back([&,r]() {
result[r] = Matrix<T>::sumLine(_matrix[r],other._matrix[r]);
});
for(size_t r=0; r!=_rows; ++r)
threads[r].join();
return move(result);
}
template class Matrix<double>;

How could I implement a vector of stack?

I am currently trying to implement a class with a vector of stacks. The class shall work as a stack to other objects but distribute the data to different stacks of a maximum size internally. If a stack is full a new one is created and pushed to the internal vector.
My current approach generates errors:
prog.cpp: In instantiation of ‘SetOfStack<T>::SetOfStack(int) [with T = int]’:
prog.cpp:54:32: required from here
prog.cpp:13:17: error: no matching function for call to ‘std::vector<std::stack<int, std::deque<int, std::allocator<int> > >, std::allocator<std::stack<int, std::deque<int, std::allocator<int> > > > >::push_back(std::stack<int, std::deque<int, std::allocator<int> > >*)’
stacks.push_back( new stack<T> );
^
/usr/include/c++/4.8/bits/stl_vector.h:901:7: note: no known conversion for argument 1 from ‘std::stack<int, std::deque<int, std::allocator<int> > >*’ to ‘const value_type& {aka const std::stack<int, std::deque<int, std::allocator<int> > >&}’
/usr/include/c++/4.8/bits/stl_vector.h:919:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::stack<int, std::deque<int, std::allocator<int> > >; _Alloc = std::allocator<std::stack<int, std::deque<int, std::allocator<int> > > >; std::vector<_Tp, _Alloc>::value_type = std::stack<int, std::deque<int, std::allocator<int> > >]
push_back(value_type&& __x)
^
/usr/include/c++/4.8/bits/stl_vector.h:919:7: note: no known conversion for argument 1 from ‘std::stack<int, std::deque<int, std::allocator<int> > >*’ to ‘std::vector<std::stack<int, std::deque<int, std::allocator<int> > >, std::allocator<std::stack<int, std::deque<int, std::allocator<int> > > > >::value_type&& {aka std::stack<int, std::deque<int, std::allocator<int> > >&&}’
prog.cpp: In instantiation of ‘void SetOfStack<T>::push(T) [with T = int]’:
prog.cpp:55:21: required from here
prog.cpp:22:25: error: ‘__gnu_cxx::__alloc_traits<std::allocator<std::stack<int, std::deque<int, std::allocator<int> > > > >::value_type’ has no member named ‘push_back’
stacks[current_stack].push_back(new stack<T>);
^
prog.cpp: In instantiation of ‘T SetOfStack<T>::pop() [with T = int]’:
prog.cpp:56:27: required from here
prog.cpp:34:25: error: ‘__gnu_cxx::__alloc_traits<std::allocator<std::stack<int, std::deque<int, std::allocator<int> > > > >::value_type’ has no member named ‘pop_back’
stacks[current_stack].pop_back();
^
Sample Code:
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
template <class T>
class SetOfStack {
public:
SetOfStack( int max_size ): current_stack(0), max_stack_size(max_size) {
stacks.reserve(10);
stacks.push_back( new stack<T> );
}
~SetOfStack() {
stacks.clear();
}
void push( T value ) {
stacks[current_stack].push(value);
if(stacks[current_stack].size() > max_stack_size) {
stacks[current_stack].push_back(new stack<T>);
current_stack++;
if(current_stack % 10 == 0 && current_stack > stacks.size()) {
stacks.reserve(stacks.size() + 10);
}
}
}
T pop() {
T value = stacks[current_stack].top();
stacks[current_stack].pop();
if(stacks[current_stack].size() == 0 && current_stack != 0 ) {
stacks[current_stack].pop_back();
current_stack--;
}
}
T popAt( int index ) {
T value = stacks[index].top();
stacks[index].pop();
}
private:
int current_stack;
int max_stack_size;
vector< stack<T> > stacks;
};
int main() {
// Test code
SetOfStack<int> s_o_s(3);
s_o_s.push(1);
cout << s_o_s.pop() << endl;
return 0;
}
The error you're getting is this line:
stacks.push_back( new stack<T> );
As well as this line:
stacks[current_stack].push_back(new stack<T>);
Because you have declared stacks as a non-pointer:
vector< stack<T> > stacks;
So you want to use just stack<T>() instead of new stack<T>
Additionally, std::stack does not have a function push_back, you need to use push instead.
stacks.push_back(stack<T>());
stacks[current_stack].push(value);
As well as pop and not pop_back():
stacks[current_stack].pop();
At which point, you no longer need the deletes I mentioned in my original comment, since it looks like you were not intending to call new

What is wrong with this template code?

#include <iostream>
#include <map>
#include <vector>
template<typename T>
class foo {
public:
foo(T val) : m_Value(val) { };
T get_value() const { return m_Value; };
void set_value(const T& t) { m_Value=t; };
bool operator<(const foo<T>& x) { return x.get_value() < m_Value; };
bool operator==(const foo<T>& x) { return x.get_value() == m_Value; };
private:
T m_Value;
};
template<typename T>
class bar {
public:
bar() { };
void print_first() const {
typename std::map<foo<T>,std::vector<foo<T> > >::iterator it;
it = m_Map.begin(); //ERROR!
std::cout << it->first.get_value() << std::endl;
};
private:
std::map<foo<T>,std::vector<foo<T> > > m_Map;
};
int main() {
bar<int> b;
b.print_first();
return 0;
};
I am trying to write a container, but the member functions require use of an iterator, but when I try to actually use an iterator, I get an error:
testcase.cpp: In member function `void bar<T>::print_first() const [with T =
int]':
testcase.cpp:33: instantiated from here
testcase.cpp:24: error: no match for 'operator=' in 'it = std::map<_Key, _Tp,
_Compare, _Alloc>::begin() const [with _Key = foo<int>, _Tp =
std::vector<foo<int>, std::allocator<foo<int> > >, _Compare =
std::less<foo<int> >, _Alloc = std::allocator<std::pair<const foo<int>,
std::vector<foo<int>, std::allocator<foo<int> > > > >]()'
/usr/include/c++/3.3.3/bits/stl_tree.h:184: error: candidates are:
std::_Rb_tree_iterator<std::pair<const foo<int>, std::vector<foo<int>,
std::allocator<foo<int> > > >, std::pair<const foo<int>,
std::vector<foo<int>, std::allocator<foo<int> > > >&, std::pair<const
foo<int>, std::vector<foo<int>, std::allocator<foo<int> > > >*>&
std::_Rb_tree_iterator<std::pair<const foo<int>, std::vector<foo<int>,
std::allocator<foo<int> > > >, std::pair<const foo<int>,
std::vector<foo<int>, std::allocator<foo<int> > > >&, std::pair<const
foo<int>, std::vector<foo<int>, std::allocator<foo<int> > >
>*>::operator=(const std::_Rb_tree_iterator<std::pair<const foo<int>,
std::vector<foo<int>, std::allocator<foo<int> > > >, std::pair<const
foo<int>, std::vector<foo<int>, std::allocator<foo<int> > > >&,
std::pair<const foo<int>, std::vector<foo<int>, std::allocator<foo<int> > >
>*>&)
What is it that i'm doing wrong?
Thanks in advance.
print_first is a const method. Therefore the member m_Map is also const, and its begin method doesn't return an ordinary iterator, but a const_iterator. Change
typename std::map<foo<T>,std::vector<foo<T> > >::iterator it;
to
typename std::map<foo<T>,std::vector<foo<T> > >::const_iterator it;
and you should be good to go.

c++ problem with vector push_back

UPDATE:
The following code gives me an error
Graph.cpp: In function 'std::ostream&
operator<<(std::ostream&, const
Graph&)': Graph.cpp:43: error: passing
'const std::map >,
std::less,
std::allocator > > > >' as
'this' argument of '_Tp&
std::map<_Key, _Tp, _Compare,
_Alloc>::operator[](const _Key&) [with _Key = long int, _Tp = std::vector >, _Compare =
std::less, _Alloc =
std::allocator > > >]'
discards qualifiers Graph.cpp:44:
error: passing 'const std::map >,
std::less,
std::allocator > > > >' as
'this' argument of '_Tp&
std::map<_Key, _Tp, _Compare,
_Alloc>::operator[](const _Key&) [with _Key = long int, _Tp = std::vector >, _Compare =
std::less, _Alloc =
std::allocator > > >]'
discards qualifiers make[2]: *
[build/Debug/GNU-MacOSX/Graph.o] Error
1
class Graph {
public:
Graph();
Graph(const Graph& orig);
virtual ~Graph();
void clear();
void rgg(lint, double);
bool is_directed() { return directed; }
friend ostream& operator<< (ostream&, const Graph&);
private:
map< lint, vector<lint> > adjList;
vector< pair<double, double> > xy;
double radius;
MTRand Rand;
bool directed;
};
void Graph::rgg(lint n, double r) {
radius = r; directed = false;
clear();
for(lint i = 0; i < n; i++)
xy.push_back(pair<double, double>(Rand.rand(), Rand.rand()));
}
ostream& operator<< (ostream& os, const Graph& inGraph) {
for(lint i = 0; i < inGraph.nodes; i++) {
os << i << " ";
if( inGraph.adjList.find(i) != inGraph.adjList.end() ) {
for(lint idx = 0; idx < (inGraph.adjList[i]).size(); idx++ )
os << inGraph.adjList[i].at(idx) << " ";
}
os << endl;
}
}
Thank you in advance,
I suspect that you mean Rand instead of MTRand:
Rand.rand()
MTRand is the name of the type. Rand is the name of the instance you created.
Just a guess, but did you try
xy.push_back(pair<double, double>(MTRand.rand(), MTRand.rand())
according to the declation of xy?
EDIT: seems the OP has changed it's code, now my answer does not match the new question any more. Nevertheless, hope my answer was useful.
The cause of your problems is that map's operator[] is a mutable operation (if the key doesn't exist, it will be added to the map).
You will have to use the iterator returned from find():
map< lint, vector<lint> >::const_iterator it = inGraph.adjList.find(i);
if (it != inGraph.adjList.end();
for(lint idx = 0; idx < it->size(); idx++ )
os << it->at(idx) << " ";