字符串问题

发布于 2024-07-14 21:27:28 字数 792 浏览 6 评论 0原文

我正在使用一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扮仙女 2024-07-21 21:27:28

问题:

  • 返回 true 或 false,而不是“true”或“false”
  • 您正在使用 = 而不是 == 进行比较(您在代码中使用 = 进行比较两次)
  • 在 C++ 中引用您自己类,您需要使用关键字 this 而不是类名。
  • 第 74 行应该是:

    if (length() == m2.length()) // 74 
      
  • strcmp 采用 char* 而不是 MyString。
  • 第 76 行应该是:

    if (strcmp(this->c_str(), m2.c_str())) // 76 
      

在第 76 行中,假设类型 MyString 有一个函数 c_str(),该函数返回一个指向以零结尾的 char[] 缓冲区的指针。


函数结构:

函数的结构非常糟糕。 考虑更像这样的事情:

bool MyString::operator ==(const MyString &m2) const
{
    if(this->length() != m2.length())
      return false;

    return !strcmp(this->c_str(), m2.c_str()));
}

注意:在上面的函数中, this->; 可以省略。

Problems:

  • Return true or false, not "true" or "false"
  • You are doing compares with = instead of == (You use = for comparison twice in your code)
  • To refer to yourself in a C++ class, you need to use the keyword this not the class name.
  • Line 74 should be:

    if (length() == m2.length()) // 74
    
  • strcmp takes a char* not a MyString.
  • Line 76 should be:

    if (strcmp(this->c_str(), m2.c_str()))  // 76
    

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:

bool MyString::operator ==(const MyString &m2) const
{
    if(this->length() != m2.length())
      return false;

    return !strcmp(this->c_str(), m2.c_str()));
}

Note: In the above functions this-> can be omitted.

A君 2024-07-21 21:27:28

大多数问题已经得到解决。 我只是有一个上面没有写过的建议:

使用比较运算符的自由函数形式而不是成员函数:

bool operator == (MyString const &, MyString const &);

如果它依赖于私有数据/成员,则必须将其声明为友元,但您将获得对称性来电者。 假设(与 std::string 一样)您确实有一个从 const char * 到字符串定义的隐式转换,则 == 的成员函数实现不是对称的。 成员函数要求左侧为所需类型。 编译器不会在比较的左侧执行转换:

// assumes MyString( const char* ) is defined and not explicit
// operator== defined as member function

const char* literal = "hola";
MyString str( "hola" );

if ( str == literal ) {} // correct
if ( literal == str ) {} // compilation error

如果您实现为成员函数,则在第一个测试中,编译器将创建一个未命名的 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:

bool operator == (MyString const &, MyString const &);

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:

// assumes MyString( const char* ) is defined and not explicit
// operator== defined as member function

const char* literal = "hola";
MyString str( "hola" );

if ( str == literal ) {} // correct
if ( literal == str ) {} // compilation error

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 your operator== 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文