*C++ 中的这个错误
Posted
技术标签:
【中文标题】*C++ 中的这个错误【英文标题】:*this Error in C++ 【发布时间】:2014-04-09 02:33:07 【问题描述】:我正在尝试比较并查看隐式参数是否大于函数的显式参数。这样做时,它说使用*this
是无效的,因为它只能在非静态成员函数中使用,我不确定它为什么会给我这个错误,也不知道如何修复它。任何人都可以提供任何帮助将不胜感激。
bool isGreaterThan(English_length&L)
if (*this > L)
return true;
else
return false;
这是我的班级标题:
class English_length
public:
//------------------------------------------------------
//----- Constructors -----------------------------------
//------------------------------------------------------
//----------------------------------
// default constructor --
// initializes a newly created length
// to zero
//----------------------------------
English_length() ;
//------------------------------------------------------
//----- Inspectors -------------------------------------
//------------------------------------------------------
//----------------------------------
// returns inches "left over" (0-11)
//----------------------------------
int inches() const ;
//----------------------------------
// returns total inches in measurement:
// 2 feet 3 inches returns 27
//----------------------------------
int totalInches() const ;
//----------------------------------
// returns feet "left over" (0-2)
//----------------------------------
int feet() const ;
//----------------------------------
// returns total feet in measurement:
// 1 yard 2 feet 3 inches returns 5
//----------------------------------
int totalFeet() const ;
//----------------------------------
// returns total yards
//----------------------------------
int yards() const ;
//------------------------------------------------------
//----- Mutators ---------------------------------------
//------------------------------------------------------
//----------------------------------
// increases the length of the measure by amt_to_add
//----------------------------------
void add_inches(int amt_to_add) ;
//------------------------------------------------------
//----- Facilitators -----------------------------------
//------------------------------------------------------
//----------------------------------
// return EnglishLength in string form:
// "x yards, y feet, and z inches"
//----------------------------------
string toString() const ;
bool isGreaterThan(English_length & L);
private:
int inches_; // 0-11
int feet_; // 0-2
int yards_; // > 0
;
【问题讨论】:
函数只有在它们是非静态成员函数时才具有隐式参数。他们需要知道实例。否则this
会是什么?
什么使函数非静态?我的函数不是非静态的吗?
请提供有关您的English_length
课程的更多详细信息 - 甚至可以发布其标题。
@user3403855:你忘记了 chris 句子中最重要的一半。
@user3403855 它是非静态的,但它也是非成员(即“独立”)。
【参考方案1】:
由于isGreaterThan
是类的成员函数,所以需要定义如下:
bool English_length::isGreaterThan(English_length&L)
if (*this > L)
return true;
else
return false;
【讨论】:
这会解决这个问题,但现在当我使用大于号比较两者时会引发错误。 不确定您比较两个对象的方法是什么。编译器不提供默认的operator>
函数。如果要使用*this > L
,则必须实现该功能。另一方面,如果您只想比较对象的地址,可以将该测试更改为this > &L
。
@user3403855 在您的情况下,您可能希望使用 totalInches() > L.totalInches()
比较总英寸数【参考方案2】:
this
是指向类实例的指针。
class Foo
void Instance() *this; // Yep, this has been instanced.
static void Static() *this; // Nope, method is static.
Foo f; // Instance of class Foo
void Global() *this; // Nope, not associated with a class instance.
【讨论】:
【参考方案3】:您必须通过以下方式在类定义之外定义函数
bool English_length::isGreaterThan(English_length&L)
if (*this > L)
return true;
else
return false;
但是该函数没有任何意义,因为类定义中没有operator >
。
【讨论】:
那么我怎么看隐式参数是否大于函数的显式参数呢? @user3403855 我不知道标准是什么。您应该决定如何比较对象。【参考方案4】:也许
bool isGreaterThan(English_length&L)
需要改成
bool English_length::isGreaterThan(English_length&L)
^^^^^^^^^^^^^^^^
然后,需要正确实现该方法。有两种选择:(1) 以当前形式的代码实现 operator>()
,但编译器报告缺少该代码,或者 (2) 以另一种可用方法 total_inches()
的形式实现。以下是选项 #2 的说明。
bool English_length::isGreaterThan( English_length const & rhs )
return total_inches() > rhs.total_inches();
【讨论】:
行得通!但现在它抛出一个错误,说我不能使用 > 符号来比较两者。以上是关于*C++ 中的这个错误的主要内容,如果未能解决你的问题,请参考以下文章