E0349 Error while Extending istream to support a Person class - c++

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

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.

Understanding the ambiguating new declaration of function error

I don't understand this error
here is a link to view the code online:
https://onlinegdb.com/rkirYvU_M
I am trying to add the names of the drivers, owners, and model to vectors, and we need to use pointers and files.
Here is my main file:
#include "person.h"
#include "car.h"
#include <iostream>
#include <vector>
std::vector <Person*>people;
std::vector <Car*> cars;
int main()
{
bool done = false;
Person person;
while(! done)
{
std::cout << "\n Please enter the owners ";
Person*prompt_info();
std::cout << "\n Please enter the drivers ";
Car*prompt_info();
Car*set();
Car*print();
}
return 0;
}
Here is the person.h file:
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <iostream>
//using namespace std;
class Person
{
public:
Person();
std::string get_name();
int get_age();
void prompt_info();
private:
std::string name;
int age;
};
#endif
Here is the person.c++ file:
#include "person.h"
Person::Person()
{
}
void Person::prompt_info()
{
std::cout << " name: ";
std::cin >> name;
std::cout << "enter their age: ";
std::cin >> age;
}
std::string Person::get_name()
{
return name;
}
int Person::get_age()
{
return age;
}
Here is the car.h file:
#ifndef CAR_H
#define CAR_H
#include <string>
#include <iostream>
#include "person.h"
using namespace std;
class Car
{
public:
Car();
std::string get_model();
Person* get_owner();
Person* get_driver();
void print();
void set(Person _owner,Person get_driver);
void prompt_info();
private:
std::string model;
Person* owner;
Person* driver;
};
#endif
I am trying to understand this error.
main.cpp:23:25: error: ambiguating new declaration of 'Car* prompt_info()'
Car*prompt_info();
^
You seem to be confusing function declarations with member functions. Just declare a Person object on the stack and call the method through it's object. Do the same for your Car object. You can use your objects like this.
while(! done)
{
Person person; ///< Person object named 'person'
Car car; ///< Car object named 'car'
std::cout << "\n Please enter the owners ";
person.prompt_info();
std::cout << "\n Please enter the drivers ";
car.prompt_info();
car.set();
car.print();
// TODO do something with your objects (store to vector?)
// next time through the loop your person and car will
// get initialized all over again
}
return 0;
You will have to store your temporary objects before they go out of scope if you want to use them later.

'OBJECT' not declared in scope

My first ever c++ program and i cant seem to figure out why it says that my 'object' was not declared in the scope. I've included the header files, i've prototyped my functions. i'm used to coding in java and the separation is pretty confusing to me.
The aim of the program is simple: create a class: Semester.
Semester: will have ID, name, credit, marks.
provide get/set methods to input and output from kb
Main
#include <iostream>
#include <stdio.h>
#include "Semester.h"
using namespace std;
int main()
{
Semester s1;
cout << "Please enter ID" << endl;
cin >> sl.setid();
cout << "ID: " << s1.getid();
}
cpp
#include <stdio.h>
#include <iostream>
#include "Semester.h"
using namespace std;
Semester::Semester(){
}
//setters
void Semester::setid(string t_id) {id = t_id;}
void Semester::setunit(string t_name) {unit_name = t_name;}
void Semester::setcredit(int t_credit){credit = t_credit;}
void Semester::setmark(int t_marks) {marks = t_marks;}
//getters
string Semester::getid() {return id;}
string Semester::getunit() {return unit_name;}
int Semester::getcredit() {return credit;}
int Semester::getmark() {return marks;}
header
#ifndef SEMESTER_H
#define SEMESTER_H
#include <stdio.h>
#include <iostream>
class Semester{
private: //variables
std::string id;
std:: string unit_name;
int credit, marks;
public:
Semester(); //constructor
//setters
void setid(std::string id);
void setunit(std::string name);
void setcredit(int credit);
void setmark(int marks);
//getters
std::string getid();
std::string getunit();
int getcredit();
int getmark();
};
#endif // SEMESTER_H
The following line is incorrect.
cin >> sl.setid();
Problems with that line:
You are calling a function that expects an argument with no arguments.
The return type of setid() is void. That cannot be used as the RHS of the stream extraction function.
What you need is:
std::string id;
cin >> id;
s1.setid(id);
Suggestion for improvement: Make all the get functions const member functions.
//getters
std::string getid() const;
std::string getunit() const;
int getcredit() const;
int getmark() const;
Make sure to update the .cpp file accordingly.

c++ undefined reference in extraction operator overloading

Learning c++ bit by bit in codeblocks, I encountered this error. I have tried it for hours and saw various links but its still not working,
main.cpp
#include <iostream>
#include "Person.h"
using namespace std;
int main()
{
foo:: Person p;
//std:: cin >> p;
std:: cout << p;
return 0;
}
Person.h
#ifndef PERSON_H
#define PERSON_H
namespace foo{
class Person
{
public:
int age;
Person();
friend std:: ostream& operator << (std::ostream&,const Person&);
//friend std:: istream& operator << (std:: istream& in, Person& p);
protected:
private:
// std::string name;
};
}
#endif // PERSON_H
Person.cpp
#include <iostream>
#include "Person.h"
using namespace foo;
Person::Person()
{
age = 0;
//name = "noname";
}
std:: ostream& operator <<(std:: ostream& out, Person const &p)
{
out << "Extraction operator" << std:: endl << p.age << std:: endl;
// out << p.name << std::endl;
return out;
}
/*std:: istream& operator >>(std:: istream& in, Person &p)
{
in >> p.age >> p.name;
return in;
}*/
Q1. Why do we need to create the header file and Person.cpp in a seperate namespace?
Guess: Is it because cout would simply mean the global namespace and then we again have a overloaded cout so which definition is the compiler going to call its not sure?
Q2. By creating a object p of Person class in foo namespace in main and calling std:: cout << p, what are we trying to do? (std is in std namespace and we want to call the overloaded operator)
Error:
undefined reference to foo:: operator<<(std::ostream&, foo::Person const&)')
If we write the age in private it says that its is private but being a friend it should have access to private members. I know it's something related to namespace but I can't find out the reason.
no match for operator >> in std::cin
Earlier it was giving the above and many other error so I tried using two namespaces but still its not working.
You defined operator<< in the global namespace, but declared it inside namespace foo.
Define it inside the namespace foo:
namespace foo
{
// definition
}
I think you using wrong operator declaration
//friend std:: istream& operator << (std:: istream& in, Person& p);
instead of
friend std:: istream& operator >> (std:: istream& in, Person& p);
and also define your class methods in namespace where it were declared
#include <iostream>
#include "Person.h"
namespace foo
{
Person::Person()
{
age = 0;
//name = "noname";
}
std:: ostream& operator <<(std:: ostream& out, Person const &p)
{
out << "Extraction operator" << std:: endl << p.age << std:: endl;
// out << p.name << std::endl;
return out;
}
std:: istream& operator >>(std:: istream& in, Person &p)
{
in >> p.age >> p.name;
return in;
}
}
the code below runs perfectly on MS Visual C++
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
namespace foo{
class Person
{
public:
Person();
friend std:: ostream& operator << (std::ostream&,const Person&);
friend std:: istream& operator >> (std:: istream& in, Person& p);
protected:
private:
int age;
std::string name;
};
}
namespace foo
{
Person::Person()
{
age = 0;
name = "noname";
}
std:: ostream& operator <<(std:: ostream& out, Person const &p)
{
out << "Extraction operator" << std:: endl << p.age << std:: endl;
out << p.name << std::endl;
return out;
}
std:: istream& operator >>(std:: istream& in, Person &p)
{
in >> p.age >> p.name;
return in;
}
}
int main()
{
foo:: Person p;
std:: cin >> p;
std:: cout << p;
_getch();
return 0;
}

Error with const member in class

I have a class named Student with its Name and Address classes.
#ifndef ADDRESS_H
#define ADDRESS_H
//This is address class
#include <string>
class Address{
public:
Address(std::string street, std::string city, std::string state, std::string zip) :
street(street), city(city),state(state),zip(zip)
{}
std::string street,city,state,zip;
std::string aString;
aString=street+city+state+zip;
//private:
};
#endif
and the Name class is
#ifndef NAME_H
#define NAME_H
#include <iostream>
#include <string>
class Name {
friend std::ostream &operator <<(std::ostream &os, const Name &name) {
if(name.middle!="")
os << name.last << ", "<<name.middle<<" ," << name.first;
else
os<< name.last <<", "<<name.first;
return os;
}
public:
Name(std::string last, std::string middle, std::string first) : last(last), first(first),middle(middle) {}
private:
std::string last, first, middle;
};
#endif
And the Student class is like:
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
#include "name.h"
#include "Address.h"
class Person {
friend std::ostream &operator <<(std::ostream &os, const Person &person);
public:
Person(const Name &name, int age, const Address &address);
Address address;
std::string adr=address.aString;
//private:
Name name;
int age;
};
#endif
Finally, to call them.
#include <iostream>
#include "student.h"
#include <string>
using namespace std;
Person::Person(const Name &name, int age, const Address &address) : name(name), age(age),address(address) {}
ostream &operator <<(ostream &os, const Person &person) {
os << person.name << " " << person.age<<person.adr;
return os;
}
#include <iostream>
#include "student.h"
using namespace std;
int main() {
Person p(Name("Doe","","Jane"), 21, Address("Park Ave","New York","NY","10002"));
Person p2(Name("Bane","IHateM","Jane"), 21, Address("Bay parkway","Brooklyn","NY","11223"));
cout << p<<endl;
cout<< p2<<endl;
return 0;
}
However, there are some errors during compilation.
(1) The following line is wrong based on complier, how to fix it please?
std::string adr=address.aString;
(2) In my address class, the compiler said that "string does not name a type Error", but following this Why am I getting string does not name a type Error? can't fix the problem, why is that?
Simple Solution
The simplest solution is to move your aString=street+city+state+zip; inside the Address constructor.
Do the same for your adr = ... statement (you'll still need a 'declaration' for std::string adr; within your class header).
To understand why what you wrote won't work, consider this:
When you write (within a class declaration, like in your header)
class myClass
{
int a = 5;
};
you assign a default value to the int a that you have declared - this is both declaration and (default) assignment.
When you write
class Address{
public:
Address(std::string street, std::string city, std::string state, std::string zip) :
street(street), city(city),state(state),zip(zip)
{}
std::string street,city,state,zip;
std::string aString;
aString=street+city+state+zip;
};
you're trying to give a default assignment to aString, but this is invalid code.
You could do this using
std::string aString = ...;
but not
std::string aString;
aString = ...;
because the last line is a 'statement' - something to be executed.