简体   繁体   English

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

[英]How to fix “no match for operator+” in C++?

I am writing a class that overloads some basic operators. 我正在写一个重载一些基本运算符的类。 I have written other classes using the same concepts and syntax, and they work fine, but now I am getting an error regarding my operator+ function specifically for this class. 我已经使用相同的概念和语法编写了其他类,并且它们工作正常,但现在我收到有关此类的操作符+函数的错误。 When the '+' operator is not written in my client program everything compiles fine, but when I use the '+' operator to initialize the data of an object with the sum of the data in the first two objects the error is produced. 当我的客户端程序中没有写入'+'运算符时,所有内容编译都很好,但是当我使用'+'运算符初始化对象的数据时,前两个对象中的数据总和会产生错误。

I have viewed the operator overloading FAQ, and some other similar questions on this website, but none have helped me. 我已经看过运营商在本网站上重载常见问题和其他一些类似的问题,但没有人对我有所帮助。 Namely because the error does not seem to arise on a different computer. 即因为错误似乎不是出现在另一台计算机上。 I sent the code to my professor who was stumped as he could not reproduce the error that I am getting. 我把代码寄给了我的教授,因为他无法重现我得到的错误而感到难过。

My code is in three separate files: mystring.h mystring.cpp and simpleclient.cpp. 我的代码在三个单独的文件中:mystring.h mystring.cpp和simpleclient.cpp。 I will include the relevant code from each file as the rest of the code should irrelevant, but if you would like me to include the entire file I can do that. 我将包含每个文件中的相关代码,因为其余代码应该无关紧要,但如果您希望我包含整个文件,我可以这样做。

mystring.h: 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: 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: 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
}

The idea here is that the data member for MyString c would contain "Hello, World!". 这里的想法是MyString c的数据成员将包含“Hello,World!”。

I compile and execute the program with the following commands: 我使用以下命令编译并执行程序:

$ cd ~/Directory/with/the/files
$ g++ mystring.h mystring.cpp simpleclient.cpp -o exec
$ ./exec

When the client program contains the errant code, I receive the following output from the terminal after the second command: 当客户端程序包含错误代码时,我在第二个命令后从终端收到以下输出:

$ 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;
                      ^

If you can explain what this error and the output means and how to fix it, that will solve my problem. 如果你能解释这个错误和输出意味着什么,以及如何解决它,那将解决我的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM