Jump to content
  • Advertisement
Sign in to follow this  
jregan

2d vector array template class, assignment problems

This topic is 4845 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

hi, I am trying to use a 2d vector implementation that I found online, which I like because it allows me to do subscripting like x[j] however, I'd like to be able to assign new values within the vector and I am not sure how to properly define the = operator for the class. i have:
template <typename T>
class dynamic_array
{
public:
	dynamic_array(){};
	
	dynamic_array(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_; }
	// other accessors, like at() ...
	
	void resize(int rows, int cols)
	{
		data_.resize(rows);
		for(int i = 0; i < rows; ++i)
			data_.resize(cols);
	}
	
	// other member functions, like reserve()....
private:
		std::vector<std::vector<T> > data_;  
};
for the class definition, but when I try to use it like this, m[1][1] = a11; the compiler will say error: no match for `std::vector<float, std::allocator<float> >& = float&' operator Maybe you can get away with that using a VC compiler, however I am using xcode 1.5 so I believe it is GCC 3.3. I figured I would have to add the = operator myself, something like std::vector<T> & operator=(float rop) { //not sure how to tell it where in T to assign! T[][] = rop; } hope someone can tell me how to fix this problem. and also that it is not something glaring and simplistic so I will feel silly =).

Share this post


Link to post
Share on other sites
Advertisement
cool... ok, thanks for the pointer smack0007 that was helpful. U know I figured out what I was doing wrong with the implementation that I have as well.


What I was doing wrong is, I was instantiating a new dynamic_array as a member of a class so I just moved the sizing of it into the constructor and then I can set values to it no problem.

so I had been using:

...
private:
dynamic_array<float> m(2,2);

where I should have just had

...private
dynamic_array<float> m;//...over in ctor m.resize(2,2);

Share this post


Link to post
Share on other sites
Quote:
Original post by jregan
cool... ok, thanks for the pointer smack0007 that was helpful. U know I figured out what I was doing wrong with the implementation that I have as well.


What I was doing wrong is, I was instantiating a new dynamic_array as a member of a class so I just moved the sizing of it into the constructor and then I can set values to it no problem.

so I had been using:

...
private:
dynamic_array<float> m(2,2);

where I should have just had

...private
dynamic_array<float> m;//...over in ctor m.resize(2,2);


Even better would be to do this in your constructor:

class Foo
{
dynamic_array<float> m;
public:
Foo() : m(2,2) {}
Foo(size_t x, size_t y) : m(x, y) {}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by jregan

template <typename T>
class dynamic_array
{
public:
dynamic_array(){};

dynamic_array(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_; }
// other accessors, like at() ...

void resize(int rows, int cols)
{
data_.resize(rows);
for(int i = 0; i < rows; ++i)
data_.resize(cols);
}

// other member functions, like reserve()....
private:
std::vector<std::vector<T> > data_;
};



Erm whats with the redundant explicit loop code for constructor & resize, no need for it:


#include <vector>

template < typename Tp, typename Alloc = std::allocator<Tp> >
struct dynamic_array {

typedef std::vector<Tp, Alloc> vec1d;

typedef typename Alloc::template rebind<vec1d>::other vec1d_allocator;

typedef std::vector<vec1d, vec1d_allocator> vec2d;

typedef typename vec2d::size_type size_type;

typedef typename vec2d::allocator_type allocator_type;

private:

vec2d data_;

public:

explicit dynamic_array(const allocator_type& a = allocator_type())
: data_(a) {}

dynamic_array(size_type rows, size_type cols, const Tp& value,
const allocator_type& a = allocator_type())
: data_(rows, vec1d(cols, value), a) {}

explicit dynamic_array(size_type rows, size_type cols)
: data_(rows, vec1d(cols)) {}

void resize(size_type rows, size_type cols, const Tp& val = Tp()) {
data_.resize(rows, vec1d(cols, val));
}

void resize(size_type n, const Tp& val = Tp()) {
resize(n, n, val);
}

const vec1d& operator[](size_type i) const {
return data_;
}

vec1d& operator[](size_type i) {
return data_;
}

// ...
};

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!