如何修复 C++ 中的“与 operator+ 不匹配”?

用户2616618

我正在编写一个重载一些基本运算符的类。我已经使用相同的概念和语法编写了其他类,它们工作正常,但现在我收到了一个关于我的 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;
                      ^

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

用户2616618

这个问题是通过在一个独立于 .cpp 文件的命令中编译 .h 文件来解决的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何修复ArrayAdapter中的类型不匹配?

C ++编译错误-不匹配'operator ='

如何在Scala中修复此类型不匹配错误?

如何理解C ++错误,“不匹配'operator =='(操作数类型为'std :: pair'和'const int')”?

如何修复C中的“不兼容的指针类型”错误?

如何完全绕过`error:'operator =='`不匹配?

如何修复“操作符不匹配[]”错误(C ++)

如何修复:RuntimeError:pyTorch中的大小不匹配

如何修复C ++中的“构造函数实例不匹配参数列表”错误?

使用累加时,C ++中的operator +不匹配

如何为sqlplus中的此列列表错误修复不匹配的唯一键或主键?

'std :: operator中的'operator <<'不匹配

在C ++中不匹配'operator <<'(包括字符串和ostream以及重载<<)

排序功能中“ operator =”不匹配

如何修复PowerPoint 2016演示文稿中某些幻灯片的母版不匹配?

如何在新的Docker映像更新中修复哈希总和不匹配错误?

C ++不匹配“ operator =”(后缀到后缀)

C ++错误:“输入>> Group1-> Entrepreneur :: Item” |中的“ operator >>”不匹配

如何快速找到C中的不匹配模式

C++初学者不匹配'operator-'

如何修复 TFS 2013 中不匹配的用户 SID?

C++ 设置迭代器错误:“operator+=' 不匹配

在if语句中比较字符串时如何修复“不匹配'operator=='”

如何修复“计算数据中的列与提供的元数据中的列不匹配”错误?

如何修复:错误:“operator=”不匹配(操作数类型为“Estado”和“Estado*”)

与 std 中的“operator<<”不匹配

C++ 设置迭代器错误:集合中的“operator+=' 不匹配

如何使用保存在 Nuxt 商店中的 Vuetify 选择值修复不匹配的 childNodes?

编译器错误:“operator[]”C++ 不匹配