Hi, I'm doing a low-mem implementation of a matrix, than can contain values from 0-4, this is fine for my program.
I input 4 values (each 2bit) into 1 char(8bit). I'm using bitpatterns and shifting to extract values. But this is of cause hidden from the user.
I've done everything but I'm having problem overloading the "()" operator.
I can input values, and get values, using ordinary function like
int a = obj.get(a,b); //returns value at row a, column b
obj.set(a,b,c); //insert value c at row a, column b.
But since this is c++, I want a nice consistent interface with my other matrix template class.
This means I would like to implement overloading of the "()" in such a way that
int d = obj(a,b); //set value d to the value at row a, col b
obj(a,b) = c; // set value at row a, col b to c
Below is the interface for my class, along with my try at implementing the overloaded operators.
class snp{
private:
unsigned char **alloc(int x, int y);
unsigned char **m_data;
size_t x; //number of rows.
size_t y; // this is the number of hole used chars
size_t offset; //offset
public:
snp(int a,int b);
~snp() {delete [] *m_data; delete [] m_data;}
int get(int a, int b);
void set(int a, int b, int c);
int& operator() (uint r, uint c);
int operator() (uint r, uint c) const;
};
//below is implementation
int& snp::operator() (uint r, uint c){
return m_data[r][c]&0x03;//this returns 2 first bits of element at row r, col c,
}
int snp::operator() (uint r, uint c) const{
return m_data[r][c]&0x03;
}
In this code snippet I'm just using the least 2 sig bits. When I get the syntax/idea, I will of cause implement the other bits.
Lastly my compile errors.
make
g++ testMatrix.cpp alloc.o -Wall -ansi -pedantic -ggdb
In file included from testMatrix.cpp:6:
matrix.cpp: In member function ‘int& snp::operator()(uint, uint)’:
matrix.cpp:190: error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’
make: *** [allofit] Error 1
thanks in advance