Jump to content
  • Advertisement
Sign in to follow this  
ehudros

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.

If you intended to correct an error in the post then please contact us.

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 <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! :)

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!