overloading istream operator >> with getline gives error - c++

I really don't know why, but I'm struggling with my >> operator getting it to work properly with getline.
Basically, I have a command class in which I want to assign the user input to its chain attribute and after that I want to split user input with space as delimiter to its string array param
For now, I just want to get the first one to work correctly, for the split, I'll do it later with strok.
Edit:
My error:
Commande.cpp: In function 'std::istream& operator>>(std::istream&, Commande&)':
Commande.cpp:82:39: error: no matching function for call to 'std::basic_istream<char>::getline(std::__cxx11::string&, int)'
stream.getline(commande.chaine, 256);
Command.cpp
Command::Command() {
}
Command::Command(std::string _chain) {
chaine = _chain;
}
Command::Command(const Command& orig) {
}
Command::~Command() {
}
std::istream& operator >> (std::istream &stream, Command& command)
{
stream.getline(command.chain, 256);
return stream;
}
Command.h
#ifndef COMMAND_H
#define COMMAND_H
#include <iostream>
#include <assert.h>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include "Command.h"
public:
std::string chain;
std::string param[10];
Command();
Command(std::string _chain);
Command(const Command& orig);
virtual ~Command();
private:
friend std::istream& operator >> (std::istream&, Command&);
};
#endif /* COMMAND_H */
Main.cpp
include "command.h"
int main(int argc, char** argv) {
Command command;
std::cin >> command;
}

I changed my fonction as #Thomas Mattews recommanded and it works perfectly. thumbs up for all the other answers too.
std::istream& operator >> (std::istream &stream, Command& command)
{
std::getline(stream, command.chain);
return stream;
}

Related

"no match for ‘operator>>’" while using stringstream with operator overloading in a class inside a namespace

I'm trying to overload the >> operator in a class inside a namespace, but as soon as I try to use it with a string stream it doesn't work. Here's a distilled version of my code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
namespace Foo {
class Bar {
public:
string str;
friend istream& operator >>(istream& in, Bar& t);
};
}
inline istream& operator >>(istream& in, Foo::Bar& t) {
in >> t.str;
return in;
}
int main() {
Foo::Bar foo;
stringstream("foo") >> foo;
cout << foo.str << endl;
return 0;
}
and here's the error:
main.cpp:22:22: error: no match for ‘operator>>’ (operand types are ‘std::stringstream {aka std::__cxx11::basic_stringstream<char>}’ and ‘Foo::Bar’)
The thing is these other ways of doing it work:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
namespace Foo {
class Bar {
public:
string str;
friend istream& operator >>(istream& in, Foo::Bar& t) {
in >> t.str;
return in;
}
};
}
int main() {
Foo::Bar foo;
stringstream("foo") >> foo;
cout << foo.str << endl;
return 0;
}
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Bar {
public:
string str;
friend istream& operator >>(istream& in, Bar& t);
};
inline istream& operator >>(istream& in, Bar& t) {
in >> t.str;
return in;
}
int main() {
Bar foo;
stringstream("foo") >> foo;
cout << foo.str << endl;
return 0;
}
The thing is, I have no idea why the first way of doing it should be wrong. I'm using the g++ compiler on linux if that helps. Could someone help me understand what's going on?
Thanks to hints from Sam Varshavchik (in the comments above), I've been able to come up with a correct version of the first version:
#include <iostream>
#include <string>
#include <sstream>
namespace Foo {
class Bar {
public:
std::string str;
friend std::istream& operator >>(std::istream& in, Bar& t);
};
std::istream& operator >>(std::istream& in, Bar& t);
}
std::istream& Foo::operator >>(std::istream& in, Foo::Bar& t) {
in >> t.str;
return in;
}
using namespace std;
int main() {
Foo::Bar foo;
stringstream("foo") >> foo;
cout << foo.str << endl;
return 0;
}
The key was making sure that the operator>> function was both declared and defined in the same scope. I still wanted to be able to define the function outside of the namespace braces, so I had to add a declaration inside the namespace so the compiler would know that there's supposed to be that function in the namespace. Keeping the function definition separate allowed me to separate my code into three files, main.cpp, foo.hpp, and foo.cpp:
// main.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "foo.hpp"
using namespace std;
int main() {
Foo::Bar foo;
stringstream("foo") >> foo;
cout << foo.str << endl;
return 0;
}
// foo.hpp
#ifndef FOO_HPP
#define FOO_HPP
#include <string>
#include <iostream>
namespace Foo {
class Bar {
public:
std::string str;
friend std::istream& operator >>(std::istream& in, Bar& t);
};
std::istream& operator >>(std::istream& in, Bar& t);
}
#endif
// foo.cpp
#include "foo.hpp"
std::istream& Foo::operator >>(std::istream& in, Foo::Bar& t) {
in >> t.str;
return in;
}
Anyways, thanks so much for the help! And thanks for not hand-feeding me a solution; it's so much better to learn by figuring it out myself, even if I did get some help pointing me in the right direction.

E0349 Error while Extending istream to support a Person class

I tried with and without #include <iostream> #include <string> or even using namespace std and nothing changed. The parameters must be references. When trying to run the code I receive the following error: E0349 no operator ">>" matches these operands in line 9.
#include <iostream>
#include <string>
#include "Person.h"
#include <cstdio>
using namespace std;
//Extending istream to support the Person class
std::istream & operator>>(std::istream & is, Person & p) {
is >> p.getName() >> p.getAge();
return is;
}
//Extending ostream to support the Person class
std::ostream & operator<<(std::ostream & os, Person & p) {
os << "[" << p.getName()<<"," << p.getAge()<<"]";
}
int main() {
Person *pOne = new Person();
cout << "Person1's name is: " << pOne->getName() << endl;
cin >> *pOne;
getchar(); //Just to leave the console window open
return 0;
}
Code from Person.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string name;
short age;
public:
Person();
virtual ~Person();
Person(string, short);
string getName();
short getAge();
};
And here the code from Person.cpp:
#include "Person.h"
#include <iostream>
#include <string>
using namespace std;
Person::Person()
:name("[Unassigned Name]"), age(0)
{
cout << "Hello from Person::Person" << endl;
}
Person::~Person()
{
cout << "Goodbye from Person::~Person" << endl;
}
Person::Person(string name, short age)
:name(name), age(age)
{
cout << "Hello from Person::Person" << endl;
}
string Person::getName()
{
return this->name;
}
short Person::getAge()
{
return this->age;
}
The first >> in line 9 right after is is underlined red. I'm using Visual Studio 2017 Community.
The problem is that operator>> needs something it can alter. The return value of a function like getName is not such a thing.
Here's how you normally do this which is to make operator>> a friend function so that it can directly access the internals of the Person class.
class Person
{
// make operator>> a friend function
friend std::istream & operator>>(std::istream & is, Person & p);
private:
string name;
short age;
public:
Person(string, short);
string getName();
short getAge();
};
//Extending istream to support the Person class
std::istream & operator>>(std::istream & is, Person & p) {
is >> p.name >> p.age;
return is;
}
Not the only way though. Here's another approach that doesn't need to make anything a friend function. It uses temporary variables and then makes and assigns a Person object from those temporary variables.
//Extending istream to support the Person class
std::istream & operator>>(std::istream & is, Person & p) {
string name;
short age;
is >> name >> age;
p = Person(name, age);
return is;
}

Error overloading >> Operator reading from file into class

I am currently working on a program for class that requires me to overload the stream extraction operator, >>, to take data from a file straight into a class. I am getting a:
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion)
Here is the specific code the error is affecting.
int main()
#include <iostream>
#include <fstream>
#include <iomanip>
#include "stockType.h"
#include "stockListType.h"
using namespace std;
stockType myStock;
stockListType stockList;
ifstream infile;
infile.open("StockData.txt");
infile >> myStock;
stockType.h Header File
#ifndef STOCKTYPE_H
#define STOCKTYPE_H
#include <string>
#include <fstream>
#include <iostream>
class stockType
{
public:
stockType();
void printStock();
void calcPercent();
char Symbol[3];
float openingPrice;
float closingPrice;
float todayHigh;
float todayLow;
float prevClose;
int volume;
float percent;
friend std::ifstream &operator >> (std::ifstream &in, const stockType &myStock);
};
#endif
stockType.cpp Resource File
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "stockType.h"
std::ifstream& operator>> (std::ifstream &in, const stockType &myStock)
{
in >> myStock.Symbol;
in >> myStock.openingPrice;
in >> myStock.closingPrice;
in >> myStock.todayHigh;
in >> myStock.todayLow;
in >> myStock.prevClose;
in >> myStock.volume;
return in;
}
most of the searching I have done is people having problems using ostream to do this and are getting their data during program use. Trying to erorr-correct using the ifstream and reading straight from a txt file has been difficult. I can provide whatever additional information is needed. Any help is much appreciated. thanks.
Your input operator signature
std::ifstream& operator>> (std::ifstream &in, const stockType &myStock);
// ^^^^^
makes no sense. To input anything from the stream to the myStock parameter, it has to be non const of course. Also you usually don't want to overload specific implementations of std::istream, thus your signature should look like this:
std::istream& operator>> (std::istream &in, stockType &myStock);

const char* usage in constructor

#include "Board.hpp"
#include <iostream>
using namespace std;
Board::Board (const char* filename){
filename = "puz1.txt";
Board::fin (filename);
if(!fin) fatal("Error in opening the file");
}
This is my cpp file...my hpp file is:
#ifndef BOARD_H
#define BOARD_H
#include <iostream>
using namespace std;
#include "tools.hpp"
#include "square.hpp"
class Board {
private:
SqState bd[81];
ifstream fin;
public:
Board(const char* );
ostream& print(ostream& );
};
inline ostream& operator <<(ostream& out, Board& b) { return b.print(out);}
#endif //Board.hpp
I got the below errors while I compile:
Error at line in cpp filename = "puz1.txt".
and error is:
const char* shadows a //parameter.
Error at line in cpp Board::fin (filename);
and error is:
no match call to //(std::basic_ifstream})
How do I fix them?
You can only initialize fin in the contructor initialization list. You also need to #include <fstream>. This would work:
Board::Board (const char* filename): fin(filename)
{
....
}
It is unclear why you are setting filemane to something different to what is passed in the constructor. If you want a default parameter, use
Board::Board (const char* filename="puz1.txt"): fin(filename) {}
About the first error:
filename = "puz1.txt";
You are supposed to pass the filename as an argument, not to assign it there. If you just need to use "puz1.txt" then use than instead of filename.
The second error:
Board::fin (filename);
You can't initialize the ifstream object like that. Simply call open().
fin.open("puz1.txt");
if(fin.is_open()) // you can pass additional flags as the second param
{
}

Simple questions about cin/cout overloading 4

#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;
class hugeint
{
public:
int size;
int number[100];
friend istream& operator>>(istream&,hugeint&);
friend ostream& operator<<(ostream&,hugeint&);
};
istream& operator>>(istream& in,hugeint& c)
{
// code not shown
return in;
}
ostream& operator<<(ostream& out,hugeint& c)
{
return out;
}
void main()
{
system( "color 70" );
hugeint o;
hugeint y;
hugeint z;
cin >> o;
cout<<"now y "<<endl;
cin>>y;
}
The compiler complains that operator >> is ambigious... what should I do ?
Nope, the code compiles.
However please note that I did also remove your extraneous and non-C++ headers, and fixed your incorrect main return type.
Try putting the operator<< and operator>> functions in the std namespace