monkeyking

September 16th, 2008, 08:55 PM

Hi I'm doing a template container class abstraction of a math vector.

That should have some basic math stuff.

Now the problem is, that I can access private vars in other instatiations, but not in all cases.

The copyconstructor works but not the comparison operator.

I can of cause just make a const accessor, but I'd like to avoid the function call overhead.

But I don't really understand the problem,

I don't understand why compileren allows access to the private variables sometimes, and sometimes not.

Some consistency would be nice :)

thanks in advance

#include <iostream>

using namespace std;

template<typename T>

class Array {

public:

Array() {data_=NULL;x_=0;numOnes_=0;}

Array(int length):x_(length),data_(new T[length]), numOnes_(0){}

Array(const Array<T>& var);

~Array(){delete [] data_;}

int length() const { return x_; }

T& operator() (uint r);

T operator() (uint r) const;

void print(int x=0, char sep=' ');

void fillUp(T var) {for(int i=0;i<x_;i++) data_[i]=var;}

Array<char> operator< (const float &f);

int numTrues() const {return numOnes_;}

Array<T> extract(const Array<char>&other);

private:

int x_;

T* data_;

int numOnes_;

};

template<typename T>

Array<T>::Array(const Array<T>& var){

puts("array const constructor");

x_ = var.x_;//this works

data_ = new T[x_];

for(int i=0;i<var.x_;i++)

data_[i]=var.data_[i];

}

template<typename T>

void Array<T>::print(int x,char sep){

printf("printing array with dim=%d\n",x_);

for(int i=0;i<x_;i++)

std::cout << data_[i] << sep;

std::cout <<endl;

}

template<>

void Array<char>::print(int x,char sep){

printf("printing array with dim=%d\n",x_);

for(int i=0;i<x_;i++)

std::cout <<(int) data_[i] << sep;

std::cout <<endl;

}

template <typename T>

T& Array<T>::operator() (uint r){

return data_[r];

}

template <typename T>

T Array<T>::operator() (uint r) const{

return data_[r];

}

//these are comparison operators, these will return a Array<char> with elems {0,1}

template<class T>

Array<char> Array<T>::operator< (const float &f){

Array<char> tmp(x_);

for(int i=0;i<x_;i++){

if (data_[i]<f){

tmp.data_[i]=1;

tmp.numOnes_++;//this doesn't work

}

else

tmp.data_[i]=0;

}

return tmp;

}

int main(){

Array<int> vars(5);

vars(0)=5;vars(1)=1;vars(2)=4;vars(3)=5;vars(4)=0;

vars.print();

Array<char> posList = vars<3;

posList.print();

}

That should have some basic math stuff.

Now the problem is, that I can access private vars in other instatiations, but not in all cases.

The copyconstructor works but not the comparison operator.

I can of cause just make a const accessor, but I'd like to avoid the function call overhead.

But I don't really understand the problem,

I don't understand why compileren allows access to the private variables sometimes, and sometimes not.

Some consistency would be nice :)

thanks in advance

#include <iostream>

using namespace std;

template<typename T>

class Array {

public:

Array() {data_=NULL;x_=0;numOnes_=0;}

Array(int length):x_(length),data_(new T[length]), numOnes_(0){}

Array(const Array<T>& var);

~Array(){delete [] data_;}

int length() const { return x_; }

T& operator() (uint r);

T operator() (uint r) const;

void print(int x=0, char sep=' ');

void fillUp(T var) {for(int i=0;i<x_;i++) data_[i]=var;}

Array<char> operator< (const float &f);

int numTrues() const {return numOnes_;}

Array<T> extract(const Array<char>&other);

private:

int x_;

T* data_;

int numOnes_;

};

template<typename T>

Array<T>::Array(const Array<T>& var){

puts("array const constructor");

x_ = var.x_;//this works

data_ = new T[x_];

for(int i=0;i<var.x_;i++)

data_[i]=var.data_[i];

}

template<typename T>

void Array<T>::print(int x,char sep){

printf("printing array with dim=%d\n",x_);

for(int i=0;i<x_;i++)

std::cout << data_[i] << sep;

std::cout <<endl;

}

template<>

void Array<char>::print(int x,char sep){

printf("printing array with dim=%d\n",x_);

for(int i=0;i<x_;i++)

std::cout <<(int) data_[i] << sep;

std::cout <<endl;

}

template <typename T>

T& Array<T>::operator() (uint r){

return data_[r];

}

template <typename T>

T Array<T>::operator() (uint r) const{

return data_[r];

}

//these are comparison operators, these will return a Array<char> with elems {0,1}

template<class T>

Array<char> Array<T>::operator< (const float &f){

Array<char> tmp(x_);

for(int i=0;i<x_;i++){

if (data_[i]<f){

tmp.data_[i]=1;

tmp.numOnes_++;//this doesn't work

}

else

tmp.data_[i]=0;

}

return tmp;

}

int main(){

Array<int> vars(5);

vars(0)=5;vars(1)=1;vars(2)=4;vars(3)=5;vars(4)=0;

vars.print();

Array<char> posList = vars<3;

posList.print();

}