c++ - 如何在 C++ 中修复 "no match for operator+"?

标签 c++ compiler-errors g++ operator-overloading

我正在编写一个重载一些基本运算符的类。我已经使用相同的概念和语法编写了其他类,它们工作正常,但现在我收到一个关于专门针对该类的 operator+ 函数的错误。当我的客户端程序中没有写入“+”运算符时,一切都可以正常编译,但是当我使用“+”运算符用前两个对象中的数据之和初始化对象的数据时,就会产生错误。

我已经查看了运营商重载常见问题解答,以及该网站上的其他一些类似问题,但没有一个对我有帮助。即因为该错误似乎没有出现在另一台计算机上。我将代码发送给了我的教授,他因为无法重现我遇到的错误而感到困惑。

我的代码位于三个单独的文件中:mystring.h mystring.cpp 和 simpleclient.cpp。我将包含每个文件中的相关代码,因为其余代码应该无关紧要,但是如果您希望我包含整个文件,我可以这样做。

我的字符串.h:

#include <iostream>
#include <cstring>
#ifndef MYSTRING_H
#define MYSTRING_H
namespace cs_mystring{
    class MyString {
        public:
            MyString(const char* = "");
            MyString(const MyString& right);
            ~MyString();
            MyString operator=(const MyString& right);
            friend MyString operator+(const MyString& left, const MyString& right);
            //OTHER FUNCTION DECLARATIONS
        private:
            char* data;
    };
}
#endif

我的字符串.cpp:

#include <iostream>
#include <cstring>
#include <cassert>
#include "mystring.h"

namespace cs_mystring{
    //DEFAULT CONSTRUCTOR
    MyString::MyString(const char* init){
        data = new char[strlen(init) + 1];
        strcpy(data, init);
    }
    //COPY CONSTRUCTOR
    MyString::MyString(const MyString& right){
        data = new char[strlen(right.data) + 1];
        strcpy(data, right.data);
    }
    //DESTRUCTOR
    MyString::~MyString(){
        delete[] data;
    }
    //TODO
    MyString operator+(const MyString& left, const MyString& right){
        MyString temp;

        const int bufferSize = sizeof(left.data) + sizeof(right.data) + 2;
        char buffer[bufferSize];
        strcpy(buffer, left.data);
        strcat(buffer, right.data);
        temp = MyString(buffer);

        return temp;
    }
    //ASSIGNMENT OPERATOR
    MyString MyString::operator=(const MyString& right){
        if(this != &right){
            delete[] data; //remove the value stored in the heap
            data = new char[strlen(right.data) + 1]; //allocate memory for the new value
            strcpy(data, right.data); //copy the right-hand value into the new memory
        }
        return *this; //return the left hand object with the new value in the pointed-to memory location
    }

    //OTHER FUNCTION IMPLEMENTATIONS

}

简单客户端.cpp:

#include "mystring.h"
#include <fstream>
#include <cctype>      // for toupper()
#include <string>     
#include <cassert>
#include <iostream>
using namespace std;
using namespace cs_mystring;

int main(){

    //Simplest way to produce the error
    MyString a = MyString("Hello, ");
    MyString b = MyString("World!");
    MyString c = a + b;//Error is produced here
}

这里的想法是 MyString c 的数据成员将包含“Hello, World!”。

我使用以下命令编译并执行程序:
$ cd ~/Directory/with/the/files
$ g++ mystring.h mystring.cpp simpleclient.cpp -o exec
$ ./exec

当客户端程序包含错误代码时,我会在第二个命令之后从终端收到以下输出:
$ g++ mystring.cpp simpleclient.cpp -o exec
simpleclient.cpp: In function ‘int main()’:
simpleclient.cpp:16:20: error: no match for ‘operator+’ (operand types are ‘cs_mystring::MyString’ and ‘cs_mystring::MyString’)
     MyString c = a + b;
                    ^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/5/bits/char_traits.h:39,
                 from /usr/include/c++/5/ios:40,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from mystring.h:1:
/usr/include/c++/5/bits/stl_iterator.h:334:5: note: candidate: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)
     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
     ^
/usr/include/c++/5/bits/stl_iterator.h:334:5: note:   template argument deduction/substitution failed:
simpleclient.cpp:16:22: note:   ‘cs_mystring::MyString’ is not derived from ‘const std::reverse_iterator<_Iterator>’
     MyString c = a + b;
                      ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from mystring.h:1:
/usr/include/c++/5/bits/basic_string.h:4783:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/5/bits/basic_string.h:4783:5: note:   template argument deduction/substitution failed:
simpleclient.cpp:16:22: note:   ‘cs_mystring::MyString’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
     MyString c = a + b;
                      ^
In file included from /usr/include/c++/5/string:53:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from mystring.h:1:
/usr/include/c++/5/bits/basic_string.tcc:1151:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator+(const _CharT* __lhs,
     ^
/usr/include/c++/5/bits/basic_string.tcc:1151:5: note:   template argument deduction/substitution failed:
simpleclient.cpp:16:22: note:   mismatched types ‘const _CharT*’ and ‘cs_mystring::MyString’
     MyString c = a + b;
                      ^
In file included from /usr/include/c++/5/string:53:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from mystring.h:1:
/usr/include/c++/5/bits/basic_string.tcc:1167:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator+(_CharT __lhs, const basic_string<_CharT, _Traits, _Alloc>& __rhs)
     ^
/usr/include/c++/5/bits/basic_string.tcc:1167:5: note:   template argument deduction/substitution failed:
simpleclient.cpp:16:22: note:   ‘cs_mystring::MyString’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
     MyString c = a + b;
                      ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from mystring.h:1:
/usr/include/c++/5/bits/basic_string.h:4820:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/5/bits/basic_string.h:4820:5: note:   template argument deduction/substitution failed:
simpleclient.cpp:16:22: note:   ‘cs_mystring::MyString’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
     MyString c = a + b;
                      ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from mystring.h:1:
/usr/include/c++/5/bits/basic_string.h:4836:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
     ^
/usr/include/c++/5/bits/basic_string.h:4836:5: note:   template argument deduction/substitution failed:
simpleclient.cpp:16:22: note:   ‘cs_mystring::MyString’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
     MyString c = a + b;
                      ^

如果你能解释这个错误和输出的含义以及如何修复它,那将解决我的问题。

最佳答案

此问题已通过在与 .cpp 文件不同的命令中编译 .h 文件来解决。

关于c++ - 如何在 C++ 中修复 "no match for operator+"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55895438/

相关文章:

c++ - 使用 MinGW 帮助在 Windows 上编译 GTK+

c++ - std::bind 于 std::array 的运算符[]

c++ - "<<"和 ">>"运算符的名称是什么?

在 C 中使用 while(scanf) 时出现代码块错误

c++ - 可变参数模板 : "Sorry, unimplemented: cannot expand ' Identifier. 的 GCC 错误。 .' into a fixed-length argument list"

c++ - GCC 与 Clang 关于临时绑定(bind)到另一个临时的右值引用的生命周期

C++接口(interface)多重继承的处理方式

c++ - SDL 和 g++ 总是产生错误?

java - 创建EAR文件编译并在eclipse中获取编译错误

c++ - g++ unicode 字符 ifstream