简体   繁体   中英

Error C++ 2679 (binary '>>': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion))

HI. Before my question, I've looked in other threads with same error, nothing worked for me. My problem code is at in >> a.name >> a.extension; . As I tested myself, if I change the type from string to char for my variables, it will work, but I can't make it work with a string type of value.

Am I doing something wrong? Below full error code on compillation (Visual Studio 2015)

Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)

Thanks in advance.

#include <iostream>
#include <ctime>
#include <string>
using namespace std;

class Soft {
private:
    int *size;
    time_t *dateTime;
public:
    string name;
    string extension;
    Soft();
    Soft(string, string);
    Soft(const Soft&source);
    ~Soft();

    friend istream& operator>> (istream& in, const Soft& a) 
    {
        in >> a.name >> a.extension;
        return in;
    };

    void changeName(string);
    void changeExtension(string);
};

Soft::Soft() {
    size = new int;
    *size = 0;
    name = string("");
    extension = string("");
    dateTime = new time_t;
    *dateTime = time(nullptr);
}

Soft::Soft(const Soft&source) {
    name = source.name;
    extension = source.extension;
    size = new int;
    *size = *source.size;
    dateTime = new time_t;
    *dateTime = time(nullptr);
}

Soft::Soft(string a, string b) {
    name = a;
    extension = b;
    dateTime = new time_t;
    *dateTime = time(nullptr);
}

Soft::~Soft() {
    delete[] size;
    delete[] dateTime;
}

void Soft::changeExtension(string a) {
    extension = a;
}
void Soft::changeName(string a) {
    name = a;
}


int main() {

    Soft a;

    getchar();
    getchar();
    return 0;
}

The key word here is const , which means that the thing cannot be modified.

You are trying to modify a const thing. You cannot do that.

Your function, like any operator>> in general, should be declared like this:

friend istream& operator>>(istream& in, Soft& a) 

The change I have made is to remove the const .

By the way, I see no reason at all for your member variables size and dateTime to be pointers to dynamically allocated integers. Your code will be much simpler if you just make them normal integers.

Assignment operator's l-Value mustn't be constant because this operator changes the values so trying to change a constant value is a trying to break the rules.

look at insertion operator:

ostream& operator <<(ostream&, const myCalss&); // here const is ok

here it's ok because the insertion operator is just used to print values (const and non-const) without changing them.

*** if your really need to Break the rules of constness declare your member data as mutable:

     mutable int *size;
     mutable time_t *dateTime;

but in your example you needn't to do so, I just explain that you can change cont variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Error:C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string_view' (or there is no acceptable conversion) Error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [4]' (or there is no acceptable conversion) 22 error C2679: binary '[' : no operator found which takes a right-hand operand of type 'const VerseKey' (or there is no acceptable conversion) error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty> *' (or there is no acceptable conversion) c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion) C2679 binary '-=': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion) error C2679: binary '<<':no operator found which takes a right-hand operand of type 'std::string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM