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

Started by
1 comment, last by Zahlman 16 years, 6 months ago
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_; }

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

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! :)
Advertisement
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 :)
Consider also boost::multi_array instead of reinventing the wheel.

This topic is closed to new replies.

Advertisement