如何在C ++中修复“运算符+不匹配”的问题?

时间:2019-04-28 23:32:37

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

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

我在此网站上查看了运营商超载常见问题解答和其他一些类似问题,但没有一个对我有帮助。即是因为该错误似乎不在另一台计算机上出现。我将代码发送给我的教授,因为他无法重现我得到的错误,我对此感到沮丧。

我的代码在三个单独的文件中:mystring.h mystring.cpp和simpleclient.cpp。我将包含每个文件中的相关代码,因为其余的代码都将不相关,但是如果您希望我包含整个文件,则可以这样做。

mystring.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

mystring.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

}

simpleclient.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;
                      ^

如果您可以解释此错误以及输出的含义以及解决方法,那将解决我的问题。

1 个答案:

答案 0 :(得分:0)

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