Sign in to follow this  
ehudros

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

Recommended Posts

ehudros    194
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 <vector>

template <typename T>
class Matrix
{
public:
  Matrix(){};
  Matrix(int rows, int cols)
  {
    for(int i=0; i<rows; ++i)
    {
      data_.push_back(std::vector<T>(cols));
    }
  }
  
  inline std::vector<T> & operator[](int i) { return data_[i]; }

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

private:
  std::vector<std::vector<T> > 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<Socket> Sockets;
// Ctor that initializes the matrix with a dynamic size
        BallManagerClass::BallManagerClass() : 
	Sockets(GameData->GetMaxRows(),GameData->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:
.
.
.
   void UpdateSocket(Matrix<Socket> Sockets);
};

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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this