trying to overload the extraction operator in this class
class Mystring {
private:
char* cstring;
size_t size;
public:
friend std::istream &operator>>(std::istream &is, Mystring &str);
the function:
#include <iostream>
#include "mystring.h"
std::istream &operator>>(std::istream &is, Mystring &obj) {
char *buff = new char[1000];
is >> buff; /* Invalid operands to binary expression ('std::istream' (aka 'basic_istream<char>') and 'char *') */
obj = Mystring{buff};
delete [] buff;
return is;
}
the error I get is: Invalid operands to binary expression ('std::istream' (aka 'basic_istream<char>') and 'char *')
when I tried to do something like this:
is >> *buff;
It reads only the first char
Mystring name;
std::cout << "name: " << std::endl;
std::cin >> name;
std::cout << "Your name is " << name;
name: harry
Your name is h
You get the error because the overload reading into a
CharT*:was removed in C++20. In C++20, the closest you can get is to read into an array of known extent, using the new overload:
which in this case could be used by declaring
buffas an array instead of a pointer:I however suggest that you use a
std::stringinstead of this pair: