简体   繁体   中英

Error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [4]' (or there is no acceptable conversion) 22

First, sorry for my bad english. English is not my primary language and my Writing skill is so terrible...

I've searched some topic about this problem. I got some similar topic but I can't get the solution for mine. [ Ex: error : binary '>>' : no operator found which takes a right-hand operand of type 'const char [1] And program crashes after taking first input , VS2013 C++ error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Fraction' (or there is no acceptable conversion) ]

I just figure out the trouble is I can't use something like: " + " in istream because it's not a variable so I can't use it. But I want to enter my class value in form like: a + bj , so I can export this to txt file with fstream. Now I don't know how to do that.

My code:

#include "stdafx.h"
#include "iostream"
#include "fstream"
using namespace std;

class Complex
{
     public:
     Complex( double = 0.0, double = 0.0);
     Complex(const Complex &);
     Complex(const Complex *);
     Complex operator+( const Complex & ) const;
     Complex operator-( const Complex & ) const;
     Complex operator*( const Complex & ) const;
     Complex operator/( const Complex & ) const;
     Complex operator=( const Complex & );
     friend istream &operator>>(istream & in, const Complex & Com){
         in >> Com.real >> " + " >> Com.imaginary >> "j\n"; //Error occur here
         return in;
     }
     friend ostream &operator<<(ostream & out, const Complex & Com){
         out << Com.real << " + " << Com.imaginary << "j\n"; 
         return out;
     }
     void print() const;
     private:
     double real;
     double imaginary; 
};

Why reinvent the wheel? The standard library already provides <complex> and stream support.

#include <iostream>
#include <complex>

int main()
{
    std::complex<double> c;
    std::cin >> c;
    std::cout << c;
}

If you're looking for more complicated serialization, try something like Boost.Spirit.

There is no overload of the >> operator that takes string literals (string literals are constant arrays of char , eg const char [4] for a 3-character string literal), hence you can't use pattern matching like that.

If you want to do pattern matching you should read a line into a string , then you can use regular expressions to match a specific pattern .

Formatted extraction from an std::istream doesn't work like C's sscanf , in that you cannot specify literal pieces of text to "match" or "skip over". You can only extract data into variables and validate it after the fact.

So, std::cin >> " + " and std::cin >> "j\\n" are simply invalid.

You could make it work, though…

#include <iostream>
#include <cstring>
#include <stdexcept>

struct eat
{
    explicit eat(const char* in)
        : str(in)
        , len(std::strlen(in))
    {}

    template <size_t N>
    explicit eat(const char (&in)[N])
        : str(in)
        , len(N)
    {}

private:

    const char* str;
    size_t len;
    friend std::istream& operator>>(std::istream&, eat);
};

std::istream& operator>>(std::istream& is, eat e)
{
    char c;
    for (size_t i = 0; i < e.len; i++) {
        if (!is.get(c) || c != e.str[i]) {
            is.setstate(std::ios::failbit);
            break;
        }
    }

    return is;
}

int main()
{
    int x = 0, y = 0;
    std::cin >> x >> eat(" + ") >> y >> eat("j");
    if (!std::cin)
        throw std::runtime_error("Input was invalid!");

    std::cout << "Real part: " << x << "; imaginary part: " << y << '\n';
}

// Input: 3 + 4j
// Output: Real part: 3; imaginary part: 4

( live demo )

I've opted to require users to wrap the string literals in eat() , so that you can't use this functionality accidentally. You could make it work with your existing code, by defining only a template <size_t N> std::istream& operator>>(std::istream&, const char (&str)[N]) , and have it do the same job, but I wouldn't recommend it.

The error is because " + " can't be the destination for things from istream

If the data was written out using cout << ... << " + " << ... , the input would need to read the " + " into a std::string , and ensure it was correct.

All the existing answers are good; i'll add another (simplest?) way to do the same. You imagine that "read" and "write" should have roughly the same implementation, changing only the "direction" of data flow. Unfortunately, this cannot work in C++.

However, this will work in C, using printf and scanf :

// Hypothetical "print" function; you don't need to use it
void print(FILE* out, const Complex & Com){
    fprintf(out, "%f + %fj\n", com.real, com.imaginary);
}

If you replace printf with scanf , you get something like this:

void read(FILE* in, Complex & Com){
    fscanf(in, "%lf + %lfj\n", &com.real, &com.imaginary);
}

There are a few problems with this, however:

  • This didn't help you learn anything about C++
  • It's not trivial to detect errors
  • It uses FILE* , not istream

It's possible to fix the third deficiency - use getline to read a line of input and sscanf to parse it:

friend istream &operator>>(istream & in, const Complex & Com){
    std::string str;
    std::getline(in, str);
    sscanf(str.c_str(), "%lf + %lfj\n", &com.real, &com.imaginary);
    return in;
}

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 'const VerseKey' (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) c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion) Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'rational' (or there is no acceptable conversion) Error 2 error C2679: binary '/' : no operator found which takes a right-hand operand of type (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) error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'RatNum' (or there is no acceptable conversion) Error C2679 binary '=': no operator found which takes a right-hand operand of type 'int' (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)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM