如何修复C++中的"no match for operator+"?



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

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

我的代码在三个单独的文件中: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
}

简单客户端.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 文件来解决。

相关内容

最新更新