Z
Zilla
I have the following simple program. I just want to be able to do math
operations (+, -, =)on Timer sublcasses, but want to handle cases
where either rhs or lhs is an intrinsic value, However, the compile
fails in my g++ 2.95 compiler during the 2nd to last line of the
main() with
template.cpp: In function `int main()':
template.cpp:24: `operator +<int>(int, const int &)' must have an
argument of class or enumerated type
template.cpp: In method `int & Timer:perator +<int>(const int &)':
template.cpp:97: instantiated from here
template.cpp:42: request for member `_value' in `t', which is of non-
aggregate type `int'
template.cpp:43: static_cast from `Timer *' to `int *'
Seems like the operator+(T& t) function is being called, instead of
the operator+(int) function. Why?
#include <iostream.h>
class Timer {
template<class T>
friend T& operator+(const int value, const T& t);
public:
Timer();
~Timer();
template<class T>
T& operator+(const T& t);
template<class T>
T& operator+(const int value);
template<class T>
T& operator=(const T& t);
template<class T>
T& operator=(const int value);
int _value;
};
template<class T>
T& operator+(const int value, const T& t)
{
static T temp(value);
temp._value=t._value+value;
return temp;
}
Timer::Timer()
{
}
Timer::~Timer()
{
}
template<class T>
T& Timer:perator+(const T& t)
{
// T* This=dynamic_cast<T*>(this);
this->_value+=t._value;
return *(static_cast<T*>(this));
}
template<class T>
T& Timer:perator+(const int value)
{
T* This=dynamic_cast<T*>(this);
This->_value+=value;
return *This;
}
template<class T>
T& Timer:perator=(const T& t)
{
T* This=dynamic_cast<T*>(this);
This->_value=t.value;
return *This;
}
template<class T>
T& Timer:perator=(const int value)
{
T* This=dynamic_cast<T*>(this);
This->_value=value;
return *This;
}
class tRCD : public Timer {
public:
tRCD(const int value);
~tRCD();
};
tRCD::tRCD(const int value)
{
_value=value;
}
tRCD::~tRCD()
{
}
int main()
{
tRCD trcd1(1);
cout << "trcd1._value: " << trcd1._value << endl;
tRCD trcd2(2);
cout << "trcd2._value: " << trcd2._value << endl;
trcd1=trcd1+trcd2;
cout << "trcd1._value: " << trcd1._value << endl;
trcd1=1+trcd1;
cout << "trcd1._value: " << trcd1._value << endl;
trcd1=5;
cout << "trcd1._value: " << trcd1._value << endl;
// trcd1.operator=(trcd1.operator+(1));
trcd1=trcd1+1;
cout << "trcd1._value: " << trcd1._value << endl;
return 0;
}
operations (+, -, =)on Timer sublcasses, but want to handle cases
where either rhs or lhs is an intrinsic value, However, the compile
fails in my g++ 2.95 compiler during the 2nd to last line of the
main() with
template.cpp: In function `int main()':
template.cpp:24: `operator +<int>(int, const int &)' must have an
argument of class or enumerated type
template.cpp: In method `int & Timer:perator +<int>(const int &)':
template.cpp:97: instantiated from here
template.cpp:42: request for member `_value' in `t', which is of non-
aggregate type `int'
template.cpp:43: static_cast from `Timer *' to `int *'
Seems like the operator+(T& t) function is being called, instead of
the operator+(int) function. Why?
#include <iostream.h>
class Timer {
template<class T>
friend T& operator+(const int value, const T& t);
public:
Timer();
~Timer();
template<class T>
T& operator+(const T& t);
template<class T>
T& operator+(const int value);
template<class T>
T& operator=(const T& t);
template<class T>
T& operator=(const int value);
int _value;
};
template<class T>
T& operator+(const int value, const T& t)
{
static T temp(value);
temp._value=t._value+value;
return temp;
}
Timer::Timer()
{
}
Timer::~Timer()
{
}
template<class T>
T& Timer:perator+(const T& t)
{
// T* This=dynamic_cast<T*>(this);
this->_value+=t._value;
return *(static_cast<T*>(this));
}
template<class T>
T& Timer:perator+(const int value)
{
T* This=dynamic_cast<T*>(this);
This->_value+=value;
return *This;
}
template<class T>
T& Timer:perator=(const T& t)
{
T* This=dynamic_cast<T*>(this);
This->_value=t.value;
return *This;
}
template<class T>
T& Timer:perator=(const int value)
{
T* This=dynamic_cast<T*>(this);
This->_value=value;
return *This;
}
class tRCD : public Timer {
public:
tRCD(const int value);
~tRCD();
};
tRCD::tRCD(const int value)
{
_value=value;
}
tRCD::~tRCD()
{
}
int main()
{
tRCD trcd1(1);
cout << "trcd1._value: " << trcd1._value << endl;
tRCD trcd2(2);
cout << "trcd2._value: " << trcd2._value << endl;
trcd1=trcd1+trcd2;
cout << "trcd1._value: " << trcd1._value << endl;
trcd1=1+trcd1;
cout << "trcd1._value: " << trcd1._value << endl;
trcd1=5;
cout << "trcd1._value: " << trcd1._value << endl;
// trcd1.operator=(trcd1.operator+(1));
trcd1=trcd1+1;
cout << "trcd1._value: " << trcd1._value << endl;
return 0;
}