字符串问题
我正在使用一个 cstring 函数,该函数应该比较两个字符串 MyString 和 m2 的值。 我有 #include 所以绝对不是那样。 这些是我的错误。
(74) : error C2275: 'MyString' : illegal use of this type as an expression
(74) : error C2660: 'MyString::length' : function does not take 1 arguments
(76) : error C2275: 'MyString' : illegal use of this type as an expression
这是我从中获取它们的代码。
bool MyString::operator ==(const MyString &m2) const
{
int temp = 0;
if (length(MyString) = length(m2)) // 74
{
if (strcmp(MyString, m2)) // 76
{
temp = 1;
}
else
{
temp = 2;
}
}
if(temp = 2)
{
return "true";
}
else
{
return "false";
}
}
对此的任何帮助将不胜感激,谢谢。
Im working with a cstring function that is supposed to compare the values of two strings, MyString and m2. I have #include so its definitely not that. these are my errors.
(74) : error C2275: 'MyString' : illegal use of this type as an expression
(74) : error C2660: 'MyString::length' : function does not take 1 arguments
(76) : error C2275: 'MyString' : illegal use of this type as an expression
and this is the code that I am getting them from.
bool MyString::operator ==(const MyString &m2) const
{
int temp = 0;
if (length(MyString) = length(m2)) // 74
{
if (strcmp(MyString, m2)) // 76
{
temp = 1;
}
else
{
temp = 2;
}
}
if(temp = 2)
{
return "true";
}
else
{
return "false";
}
}
Any help on this would be appreciated, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题:
第 74 行应该是:
第 76 行应该是:
在第 76 行中,假设类型 MyString 有一个函数 c_str(),该函数返回一个指向以零结尾的 char[] 缓冲区的指针。
函数结构:
函数的结构非常糟糕。 考虑更像这样的事情:
注意:在上面的函数中, this->; 可以省略。
Problems:
Line 74 should be:
Line 76 should be:
In line 76 this assumes that the type MyString has a function c_str() that returns a pointer to a char[] buffer that is zero terminated.
Structure of function:
The structure of the function is really bad. Consider something more like this:
Note: In the above functions this-> can be omitted.
大多数问题已经得到解决。 我只是有一个上面没有写过的建议:
使用比较运算符的自由函数形式而不是成员函数:
如果它依赖于私有数据/成员,则必须将其声明为友元,但您将获得对称性来电者。 假设(与 std::string 一样)您确实有一个从 const char * 到字符串定义的隐式转换,则 == 的成员函数实现不是对称的。 成员函数要求左侧为所需类型。 编译器不会在比较的左侧执行转换:
如果您实现为成员函数,则在第一个测试中,编译器将创建一个未命名的 MyString 并调用转换运算符。 在第二次检查中,不允许编译器将
literal
转换为 MyString,因此它永远找不到您的operator==
实现。如果您将比较作为自由函数提供,那么编译器将在 == 的两侧应用相同的转换规则,并且代码将编译并正常工作。
一般来说,这同样适用于其余运算符(不包括必须作为成员函数实现的operator[]和operator=)。 使用自由函数版本提供了对称性。
Most of the problems have already been addressed. I just have a suggestion that has not been written above:
Use the free function form of the comparison operator instead of the member function:
You will have to declare it as a friend if it depends on private data/members but you will get symmetry for the callers. Assuming that (as with std::string) you do have a implicit conversion defined from
const char *
to your string then the member function implementation of == is not symmetric. Member functions require the left hand side to be of the required type. The compiler will not perform conversions on the left hand side of the comparison:If you implement as a member function, in the first test the compiler will create an unnamed MyString and call the conversion operator. In the second check, the compiler is not allowed to convert
literal
into a MyString, so it will never find youroperator==
implementation.If you provide the comparison as a free function then the compiler will apply the same conversion rules on both sides of the == and the code will compile and work properly.
In general, the same applies to the rest of the operators (excluding operator[] and operator= that must be implemented as member functions). Using the free function version provides symmetry.