M. Eugen
M. Eugen

Reputation: 27

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;
}

Upvotes: 0

Views: 1016

Answers (2)

Raindrop7
Raindrop7

Reputation: 3911

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.

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385325

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.

Upvotes: 3

Related Questions