# C++ Error C2440 (can't convert) when using a vector array

This topic is 3931 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Arrr... I'm struggling to get started using STL (yeah yeah, I know i should have done it a long time ago :) ) and it seems that my dynamic 2d array using vectors simply wont work. I have a matrix class as follows:
#include &lt;vector&gt;

template &lt;typename T&gt;
class Matrix
{
public:
Matrix(){};
Matrix(int rows, int cols)
{
for(int i=0; i&lt;rows; ++i)
{
data_.push_back(std::vector&lt;T&gt;(cols));
}
}

inline std::vector&lt;T&gt; & operator[](int i) { return data_; }

inline const std::vector&lt;T&gt; & operator[] (int i) const { return data_; }

private:
std::vector&lt;std::vector&lt;T&gt; &gt; data_;
};


and a socket class (basically just a simple struct): [
class Socket
{
public:
Socket::Socket() {};
Socket::Socket(Ball* ball_, int x, int y)
{
// do things
}

// copy ctor
Socket::Socket(const Socket& p)
{
// do things
}

Ball* ball;
cpBody* body;
};


and I have a data member thats a matrix of sockets:
class BallManagerClass
{
public:
Matrix&lt;Socket&gt; Sockets;
// Ctor that initializes the matrix with a dynamic size
BallManagerClass::BallManagerClass() :
Sockets(GameData-&gt;GetMaxRows(),GameData-&gt;GetBallsPerRow())

// has other methods - will post if needed
};


and lastly, i have a ball class that recieves the sockets matrix and does some manipulation on it:
class Ball
{
public:
.
.
.
};


this compiles but Sockets is passed by value. now here's the problem - i cant manage to pass the Sockets matrix by reference so that the UpdateSocket method can manipulate it. the way i see it i have 2 options: 1. change the sockets matrix to type <Socket*>, but then i have to 'new' all the objects and it seems kind of silly to me 2. pass the matrix<socket> as by reference (e.g: sending a pointer to the matrix). when i try to do that by chaning the header to UpdateSocket(Matrix<Socket>* Sockets) and sending it as such: obj->UpdateSocket(&Sockets) I get the following error: error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>' to 'Socket *' on the following line of code in UpdateSocket: Socket* newSocket = &Sockets[0][0]; could it be that the matrix is already all made pointers, therefor i should do something else here? Im trying all sorts of things, but nothing seems to work. If someone actually manages to read everything I just wrote and solve this I shall built him a little shrine in my study. Thanks! :)

##### Share on other sites
Never mind - I mixed up by ref parameters and pointers. all i need to do was change the decleration to a real by ref one and it worked :)

##### Share on other sites
Consider also boost::multi_array instead of reinventing the wheel.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 33
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631745
• Total Posts
3002000
×