帮助运算符重载 [英] Help with operator overloading
问题描述
我有以下简单的程序。我只是希望能够在Timer子阶段上进行数学运算(+, - ,=),但是想要处理案例
其中rhs或lhs是一个内在函数但是,编译
在我的g ++ 2.95编译器中在
main()的第2行到最后一行期间失败了
template.cpp:在函数`int main()''中:
template.cpp:24:`operator +< int>(int,const int&)''必须有一个
类或枚举类型的参数
template.cpp:在方法`int& Timer :: operator +< int>(const int&)'':
template.cpp:97:从这里实例化
template.cpp:42:request对于't''中的成员`_value'',这是非
聚合类型`int''
template.cpp:43:来自`Timer *的static_cast ''到'int *''
似乎正在调用operator +(T& t)函数,而不是
运算符+(int)函数。为什么?
#include< iostream.h>
class Timer {
template< class T>
朋友T& operator +(const int value,const T& t);
public:
Timer();
~Timer();
模板< class T>
T& operator +(const T& t);
模板< class T>
T& operator +(const int value);
template< class T>
T& operator =(const T& t);
模板< 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 +值;
返回温度;
}
Timer :: Timer()
{
}
计时器::〜计时器()
{
}
模板< class T>
T& Timer :: operator +(const T& t)
{
// T *这= dynamic_cast< T *>(this);
this-> _value + = t._value;
return *(static_cast< T *>(this));
}
模板< class T>
T& Timer :: operator +(const int value)
{
T *这= dynamic_cast< T *>(this);
这个 - > _value + = value;
return * This;
}
模板< class T>
T& Timer :: operator =(const T& t)
{
T * This = dynamic_cast< T *>(this);
这个> _value = t.value;
返回*这个;
}
模板< class T>
T& Timer :: operator =(const int value)
{
T *这= dynamic_cast< T *>(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<<结束;
返回0;
}
Zilla写道:
我有以下简单程序。我只是希望能够在Timer子阶段上进行数学运算(+, - ,=),但是想要处理案例
其中rhs或lhs是一个内在函数但是,编译
在我的g ++ 2.95编译器中在
main()的第2行到最后一行期间失败了
template.cpp:在函数`int main()''中:
template.cpp:24:`operator +< int>(int,const int&)''必须有一个
类或枚举类型的参数
template.cpp:在方法`int& Timer :: operator +< int>(const int&)'':
template.cpp:97:从这里实例化
template.cpp:42:request对于't''中的成员`_value'',这是非
聚合类型`int''
template.cpp:43:来自`Timer *的static_cast ''到'int *''
似乎正在调用operator +(T& t)函数,而不是
运算符+(int)函数。为什么?
#include< iostream.h>
没有这样的标准标题。试试< iostream>。
>
class Timer {
template< class T>
朋友T& operator +(const int value,const T& t);
public:
Timer();
~Timer();
模板< class T>
T& operator +(const T& t);
由于''T''是参数,你可以预期从参数推断出't''将是b
$ b你给定时器添加了一些东西,
所以这个没问题。
模板< class T>
T&安培; operator +(const int value);
这里,''T''只是返回值。为什么?您如何期望
使用此运算符? lhs是''计时器''。 rhs是''int''。
T来自哪里?
模板< class T>
T& operator =(const T& t);
好的,我可以理解从某种类型分配应该是
。但是你为什么要把返回值''T&''代替
''Timer&''?
template< class T>
T& operator =(const int value);
现在,这看起来很虚伪,抱歉。从''int'分配给计时器'
并返回其他一些值?当你写作时,你怎么把''T''变成你的表达因子
计时器t;
t = 42; //这里有什么?
???
>
int _value;
};
模板< class T>
T& operator +(const int value,const T& t)
{
static T temp(value);
temp._value = t._value +值;
返回温度;
}
Timer :: Timer()
{
}
计时器::〜计时器()
{
}
模板< class T>
T& Timer :: operator +(const T& t)
{
// T *这= dynamic_cast< T *>(this);
this-> _value + = t._value;
return *(static_cast< T *>(this));
}
模板< class T>
T& Timer :: operator +(const int value)
{
T *这= dynamic_cast< T *>(this);
这个 - > _value + = value;
return * This;
}
模板< class T>
T& Timer :: operator =(const T& t)
{
T * This = dynamic_cast< T *>(this);
这个> _value = t.value;
返回*这个;
}
模板< class T>
T& Timer :: operator =(const int value)
{
T *这= dynamic_cast< T *>(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;
返回0;
}
你能解释一下你究竟在尝试什么
完成?
V
-
请删除大写''A'' s通过电子邮件回复
我没有回复最热门的回复,请不要问
" Zilla" < zi ***** @ bellsouth.netwrote in message
news:11 ********************* @ b75g2000hsg.googlegro ups.com ...
>我有以下简单程序。我只是希望能够在Timer子阶段上进行数学运算(+, - ,=),但是想要处理案例
其中rhs或lhs是一个内在函数但是,编译
在我的g ++ 2.95编译器中在
main()的第2行到最后一行期间失败了
template.cpp:在函数`int main()''中:
template.cpp:24:`operator +< int>(int,const int&)''必须有一个
类或枚举类型的参数
template.cpp:在方法`int& Timer :: operator +< int>(const int&)'':
template.cpp:97:从这里实例化
template.cpp:42:request对于't''中的成员`_value'',这是非
聚合类型`int''
template.cpp:43:来自`Timer *的static_cast ''到'int *''
似乎正在调用operator +(T& t)函数,而不是
运算符+(int)函数。为什么?
#include< iostream.h>
class Timer {
template< class T>
朋友T& operator +(const int value,const T& t);
public:
Timer();
~Timer();
模板< class T>
T& operator +(const T& t);
模板< class T>
T& operator +(const int value);
template< class T>
T& operator =(const T& t);
模板< class T>
T& operator =(const int value);
int _value;
};
template< class T>
T& operator +(const int value,const T& t)
template.cpp:24:`operator +< int>(int,const int&)''必须有一个类或枚举类型的
参数
这里结束了(int,const int& t)
这就是错误所在。显然你不能覆盖int + int
(这很有意义)。
{
static T temp(value);
temp._value = t._value + value;
return temp;
}
Timer :: Timer()
{
}
Timer :: ~Timer()< br $>
{
}
模板< class T>
T& Timer :: operator +(const T& t)
{
// T *这= dynamic_cast< T *>(this);
this-> _value + = t._value;
template.cpp:42:在't''中请求成员`_value'',这是非
聚合类型`int''
Parm是const int& t。
然后你将它转换为int *。
int没有._value成员
return *(static_cast< T *>(this));
template.cpp:43:static`cast从`Timer *''到`int *''
a计时器*是不是int *而且它是婊子。
}
模板< class T>
T& Timer :: operator +(const int value)
{
T *这= dynamic_cast< T *>(this);
这个 - > _value + = value;
return * This;
}
模板< class T>
T& Timer :: operator =(const T& t)
{
T * This = dynamic_cast< T *>(this);
这个> _value = t.value;
返回*这个;
}
模板< class T>
T& Timer :: operator =(const int value)
{
T *这= dynamic_cast< T *>(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;
返回0;
}
修复错误。
>
你能解释一下你究竟想要实现什么吗?
V
-
请在通过电子邮件回复时删除资金''A'
我没有回应top-发布回复,请不要问 - 隐藏引用文字 -
- 显示引用文字 - 隐藏引用文字 -
- 显示引用文本 -
我想为
对象编写常规数学表达式(*,/,+,=等...)类型为Timers及其子类。所以...
班级计时器{
operator-blah(...)
};
class tRCD:public Timer {
blah();
};
class tRP:公共计时器{
blah();
};
int main()
{
tRCD trcd1;
tRCD trcd2;
tRP trp;
trcd1 = trcd1 + 1;
trcd1 = 2 * trcd2;
trcd1 = trcd1 + 2 + trcd2;
}
IOW,我想在Timer中进行操作,但需要在运行中返回子类
类型...
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::operator +<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::operator+(const T& t)
{
// T* This=dynamic_cast<T*>(this);
this->_value+=t._value;
return *(static_cast<T*>(this));
}
template<class T>
T& Timer::operator+(const int value)
{
T* This=dynamic_cast<T*>(this);
This->_value+=value;
return *This;
}
template<class T>
T& Timer::operator=(const T& t)
{
T* This=dynamic_cast<T*>(this);
This->_value=t.value;
return *This;
}
template<class T>
T& Timer::operator=(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;
}
Zilla wrote: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::operator +<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>There is no such standard header. Try <iostream>.
>
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);Since ''T'' is the argument, you can expect that ''t'' would be
deduced from the argument when you add something to a Timer,
so this one is OK.
template<class T>
T& operator+(const int value);Here, ''T'' is only the return value. WHY? How do you expect
to use this operator? The lhs is ''Timer''. The rhs is ''int''.
Where would T come from?
template<class T>
T& operator=(const T& t);OK, I can understand that assigning from some type should be
possible. But why do you make the return value ''T&'' instead of
''Timer&''?
template<class T>
T& operator=(const int value);Now, this looks bogus, sorry. Assign to a timer from an ''int''
and return some other value? How do you even get ''T'' to factor
into your expression when you write
Timer t;
t = 42; // what''s T here?
???
>
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::operator+(const T& t)
{
// T* This=dynamic_cast<T*>(this);
this->_value+=t._value;
return *(static_cast<T*>(this));
}
template<class T>
T& Timer::operator+(const int value)
{
T* This=dynamic_cast<T*>(this);
This->_value+=value;
return *This;
}
template<class T>
T& Timer::operator=(const T& t)
{
T* This=dynamic_cast<T*>(this);
This->_value=t.value;
return *This;
}
template<class T>
T& Timer::operator=(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;
}Could you explain what exactly it is you''re trying to
accomplish?
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
"Zilla" <zi*****@bellsouth.netwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...>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::operator +<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)template.cpp:24: `operator +<int>(int, const int &)'' must have an
argument of class or enumerated type
Here it winds up being ( int, const int& t)
which is what the error is about. Apparently you can''t override int + int
(which makes sense).
{
static T temp(value);
temp._value=t._value+value;
return temp;
}
Timer::Timer()
{
}
Timer::~Timer()
{
}
template<class T>
T& Timer::operator+(const T& t)
{
// T* This=dynamic_cast<T*>(this);
this->_value+=t._value;template.cpp:42: request for member `_value'' in `t'', which is of non-
aggregate type `int''
Parm is const int& t.
Then you cast this to an int*.
an int does not have a ._value member
return *(static_cast<T*>(this));template.cpp:43: static_cast from `Timer *'' to `int *''
a Timer* is not an int* and it''s bitching.
}
template<class T>
T& Timer::operator+(const int value)
{
T* This=dynamic_cast<T*>(this);
This->_value+=value;
return *This;
}
template<class T>
T& Timer::operator=(const T& t)
{
T* This=dynamic_cast<T*>(this);
This->_value=t.value;
return *This;
}
template<class T>
T& Timer::operator=(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;
}Fix the errors.
>Could you explain what exactly it is you''re trying to
accomplish?
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -I want to program regular math expressions (*,/,+,=, etc...) for
objects of type Timers, and its sub-classes. So...
class Timer {
operator-blah(...)
};
class tRCD : public Timer {
blah();
};
class tRP : public Timer {
blah();
};
int main()
{
tRCD trcd1;
tRCD trcd2;
tRP trp;
trcd1=trcd1+1;
trcd1=2*trcd2;
trcd1=trcd1+2+trcd2;
}
IOW, I want the operations in Timer, but need to return the sub-class
type, on the fly...
这篇关于帮助运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!