Originally Posted by
dribeas
... This means that Array<int> is a class, and Array<char> is a class, but they are different classes, even if you wrote the code only once.
Of cause of cause, that makes perfect sense. This template way of programming is still quite new for me.
Originally Posted by
dribeas
Code:
template <typename T>
class Array
{
// ...
template <typename U>
friend Array<char> Array<U>::operator<( float const & );
};
I'm having problems compiling this syntax, I'm getting this nasty compile error, can you elaborate on syntax.
PHP Code:
make
g++ testMatrix.cpp alloc.o -Wall -ansi -pedantic -ggdb
In file included from testMatrix.cpp:7:
array.cpp: In instantiation of ‘Array<char>’:
array.cpp:55: instantiated from here
array.cpp:19: error: no ‘Array<char> Array<T>::operator<(const float&)’ member
function declared in class ‘Array<T>’
array.cpp:136: error: expected 2 levels of template parms for ‘Array<char>
Array<T>::operator<(const float&)’, got 1
array.cpp: In member function ‘Array<char> Array<T>::operator<(const float&)’:
array.cpp:137: error: ‘x_’ was not declared in this scope
array.cpp:140: error: ‘data_’ was not declared in this scope
array.cpp:19: confused by earlier errors, bailing out
Preprocessed source stored into /tmp/ccyMgsiS.out file, please attach this to your
bugreport.
Traceback (most recent call last):
File "/usr/share/apport/gcc_ice_hook", line 34, in <module>
pr.write(open(apport.fileutils.make_report_path(pr), 'w'))
IOError: [Errno 13] Permission denied:
'/var/crash/_usr_lib_gcc_x86_64-linux-gnu_4.2.3_cc1plus.1000.crash'
make: *** [allofit] Error 1
Originally Posted by
dribeas
As a side note, why are you overriding operator() instead of operator[] that seems more appropriate? Why are operator() not implemented in the class definition? (if you did, the compiler would be allowed to inline it, and with your implementation that would faster [skip method call] and probably yield smaller binary). Why do you have a numOnes attribute/numTrues() member method? do you plan to use it in all classes or just in your Array<char>?
I'm overloading'()' since I'm also doing a matrix template,
and the parashift faq said that I should use subsetoperator '(a,b)' instead of '[a][b]'. So I choose '()' just for making the different classes more alike.
I was told, don't remember who though, that I should always avoid method calling when I had the option of accessing the data directly, something about avoid stack swap or something. That's why I don't just use the overloaded '()' method. Should I just do this?
numOnes_/numtrues() is only used in Array<char>, is there some nice way of just having them in Array<char>
I stripped my template class definition, for most stuff when I did my original post.
this is my full 'template class header'
PHP Code:
template<typename T>
class Array {
public:
Array() {puts("empty constructor");data_=NULL;x_=0;numOnes_=0;}
Array(int length):x_(length),data_(new T[length]), numOnes_(0){}
void init(int length) {x_=length;data_=new T[length]; }
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;}
void plus(const Array<T>&);
template <typename U>
friend Array<char> Array<U>::operator< (const float &f);//this makes nasty compile error
Array<char> operator> (const float &f);
Array<char> operator== (const float &f);
int numTrues() const {return numOnes_;}
Array<T>& operator= (const Array<T>&);
Array<T> operator+ (const Array<T>&);
Array<T> operator/ (const float &other);
Array<T> extract(const Array<char>&other);
private:
int x_;
T* data_;
int numOnes_;
};
thanks for your response
Bookmarks