C++: getline not part of argument list - c++

I'm fairly new to C++, sorry if my questions aren't quite specific enough. Here goes.
I'm trying to overload the >> operator for a class which I have called "book."
"Book" contains 'title,' 'author,' and 'publisher' string objects, a 'student count' int, and a 'price' double variable. Part of my assignment is to take these values from a provided .txt file and load values into their corresponding variables/objects. All values are on their own lines within the .txt file, and they each follow this format:
//Title, Author, Publisher, Price
Starting Out with Java
Gaddis
Scott/Jones
105.99
I try to use getline() to take the string values (I use a temp string after I take the price double), but when I type it in, Visual Studio says:
Error: no intsance of overloaded function 'getline' matches the argument list.
I don't understand this. I included both <iostream> and <string>, which I believe are both required for getline to work. I'm working on getting the class file down before moving to the main code, so I apologize for not having a main code to post. Here's the .cpp file for class book:
#include <iostream>
#include <string>
#include "book.h"
using namespace std;
book::book()
{
}
book::~book()
{
}
istream& operator>> (istream &in, book &bookInfo) {
string temp;
getline(in, bookInfo.title);
return in;
}
There's question number 1 down...
Assuming I can get getline to work, I have another problem.
Visual Studio says that bookInfo.title is inaccessible, even though this is the accompanying .cpp file to the class. I even have the istream& function listed as a friend function in the class itself:
#include <iostream>
#include <string>
class book {
friend istream& operator>> (istream&, book&);
public:
book();
virtual ~book();
private:
string title;
string author;
string publisher;
double price;
};
It should be noted that I used much the same syntax for another class, and was given no error messages.
Thanks for a very quick reply.

In your header, you're not using std::. Fix that:
class book
{
friend std::istream& operator>> (std::istream&, book&);
public:
book();
virtual ~book();
private:
std::string title;
std::string author;
std::string publisher;
double price;
};

getline is a method of std::istream class, see here:
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
You should call it on class instance e.g.
your_input_stream.getline( your_params ... )

Related

More than one instance of overloaded function matches the argument list and I can't find where the error happens

I get the above error when I use this code.
//Programming Assignment 1
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Function Prototypes
void getname(ofstream);
//void Evaluate_holesterol(ofstream);
//void Evaluate_BMI(ofstream);
//void Evaluate_bloodpressure(ofstream);
int main()
{
//Open output file
ofstream pfile;
pfile.open("Profile.txt");
getname(pfile);
//Evaluate_holesterol(pfile);
//Evaluate_BMI(pfile);
//Evaluate_bloodpressure(pfile);
//pfile.close();
system("pause");
return 0;
}
//Function to get patient's name
void getname(ofstream &pfile)
{
string name;
int age;
cout<<"What is the patient's full name (middle initial included)?";
getline(cin, name);
cout<<endl<<"What is the patient's age?";
cin>>age;
string line = "Patient's Name: ";
string ageline = "Patient's Age: ";
pfile<<line+name<<endl;
pfile<<age<<endl;
}
I've checked my functions and arguments and I don't see any function that its can be confusing its arguments with anywhere else. Apologies in advance if its something simple and I just didn't see it.
As the comments by cigien and Peter already pointed out: The declaration and the definition of getname() have mismatched parameters. To fix that, change the line
void getname(ofstream);
to
void getname(ofstream&);
Note the & after ofstream.
Furthermore, any function that gets an ofstream as parameter should get that by reference (i. e. as ofstream& and not just ofstream), because there is no copy constructor for ofstream and any attempt to pass ofstream by value will cause compile errors.

(C++) Issue with overloading ">>" operator [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Once I try to compile and run program, visual shows this error.
Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [2]' (or there is no acceptable conversion)
Overload function:
istream& operator>> (istream& InputStream, Description& rhs) {
InputStream >> rhs.mNumber >> "," >> rhs.mLenght >> "," >> rhs.mName;
return InputStream;
}
Class Description defintion:
class Description {
private:
int mNumber;
int mLenght;
string mName;
public:
Description();
Description(int, int, string);
Description& operator= (const Description&);
friend ostream& operator<< (ostream&, Description&);
friend istream& operator>> (istream&, Description&);
};
And yes I did:
#include <iostream>
#include <string>
#include <fstream>
#include <istream>
In the line
InputStream >> rhs.mNumber >> "," >> rhs.mLenght >> "," >> rhs.mName;
the "," parts are wrong. You can't read anything into a string literal.
If you expect to see the token , in the input stream, you may use:
char dummy;
InputStream >> rhs.mNumber >> dummy >> rhs.mLenght >> dummy >> rhs.mName;

Template overload of insertion operator >> causing cin issue

so I've got a project where I am inserting user entered data into classes. So I've overloaded the >> operator. Because I have classes with similar structures, I made the overload a template to try to save time. Making this template overload a friend of the classes seems to work, as I get no compile errors.
My ClassDefs.h:
// ClassDefs.h
using namespace std;
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <tuple>
#include <map>
#include <typeinfo>
class measurement {
//friends
template<class measT> //measT are other similar measurement classes
friend istream & operator >> (istream&, measT&);
private:
string words;
public:
//constructors and things like that here
};
template<class measT>
istream& operator >> (istream& is, measT& meas) {
//a series of checks to ensure user entry is valid
//enter user entry into private data
//e.g.
string line;
getline(is,line);
meas.words = line;
return is;
}
The problem comes when using cin >> foo in main - I get a warning caption saying "more than one operator >> matches these operands". Accompanied by:
error C2593: 'operator >>' is ambiguous
This makes sense, but my crude understanding of overloads was that they allow you to use different types with that operator and the compiler "understands" which definition to use for each situation.
My main.cpp:
// main.cpp
#include "ClassDefs.h"
int main(){
string entry;
cin >> entry;
^^ error here
return 0;
}
I have looked around and seen things involving explicit, inline, namespace (my using namespace std; seems dodgy) but I haven't seen anyone use this template overload arrangement and have this problem. I'm using Visual Studio 2015 Community.
Any help or advice would be greatly appreciated :)
So I solved this partially with the help of #user4581301 - I really needed to keep the >> overload a template so that I could use that parameter for a template exception class inside it.
It has become a bit convoluted but I thought I'd share in case anyone else is having issues overloading operators with templates.
Firstly I changed measurement from an abstract base class to a base class (so that it could be used as a template parameter) and changed the friend declaration inside to:
//ClassDefs.h
template <class measT>
friend istream& operator >> (istream&, measurement&);
Where measT was the type of measurement involved, to be used as the parameter for the exception class.
Then later on in the header file, >> was overloaded with the definition:
//ClassDefs.h
template<class measT>
istream& operator >> (istream& is, measurement& meas) {
//take user input
//do checks on it, throw Exception<measT> depending on which set of
exceptions are relevant
}
And most importantly I wasn't sure how to use an operator with a template parameter but it turns out you can do it explicitly:
//ClassDefs.h
template<class measT>
measT userNewMeas() { //function to take user input and make into a type
// of measurement
try { ::operator>><measT> (cin,newMeas); }
catch (Exception<measT>& error) {
cout << error << endl; // this output depends on which exception
// was called
}
And now I can use template userNewMeas() in main.cpp with template >> and template Exception. The goal is probably hard to see for everyone but I was trying to overload >> for any measurement type, and have one Exception class containing different error messages to be thrown depending on which measurement was being entered.

Can't create a class using headers

I'm learning C++ from the book Primer C++.
Differently from what I used to do in Java, I learnt that is good practice defining classes and classes' methods' prototypes in a header file.
So I did it, following step by step the book, implementing the methods and constructors in a .cpp file.
The result is an endless amount of error reports.
Student.h
#ifndef STUDENT_H_
#define STUDENT_H_
class Student{
private:
string firstname;
string lastname;
double gpa;
public:
Student();
Student(string fname,string lname, double aGpa);
~Student();
void Show();
double getGpa();
};
#endif
On compile returns the following errors:
unknown name type "class"
expected { etc etc after the Student
attribute (of course, since it can't understand it's a class)
I also tried to include the cstring and the string library (both couldn't be found by the compiler, I suppose because it's something only a .cpp file can access) ending with using the library, but that didn't change a thing.
Student.cpp
#include <iostream>
#include <cstring>
#include "student.h"
using std::cout;
using std::cin;
using std::endl;
Student::Student(){
cout << "Default constructor called\n";
cout << "No info regarding the student. Object not initialized";
}
Student::Student(string fname,string lname, double aGpa){
this->firstname=fname;
this->lastname=lname;
this->gpa=aGpa;
}
void Student::Show(){
cout << lastname <<", " << firstname <<"\nGPA: " << gpa << endl;
}
double Student::getGpa(){
return this->gpa;
}
Student::~Student(){
cout<< "Object student has been destroyed";
}
On compile returns the following errors
string (in student.h) does not name a type (even if I include string.h)
in Student.h, expected ')' before fname (in the constructor declaration)
in Student.cpp, expected constructor before '(' token (in the constructor declaration)
In function Show(), lastname and firstname etc are not declared in this scope (but GPA is apparently)
I don't know about your compiler errors, but your header file is missing something to use the std::string:
#include <string>
using std::string;
are your sure you use a c++ compiler and not a c compiler?

c++ class implementation with string function

I need to implement a class for one of my assignment and one of the function in the class that has string as datatype doesn't work
my definition code is :
#include <string>
class expression {
public:
expression();
void promptUser();
int getNum1();
int getNum2();
int calculate();
st::string str;
string numToString(int num);
string opToString();
private:
int num1;
int num2;
char op;
};
And in my implementation file
when I try to definite numTostring
string expression::numToString(int num) {
string digit;
...
It says that the declaration is incompatible with the header file(my class definition)
I have no idea why because both the function heading are the same.
the header file of expression.cpp( the implementation file) are :
#include "expression1.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
Your class uses the unqualified name string, but there is no string data type defined in any enclosing scopes. There's a std::string data type defined in namespace std. That's looks to be the type that you need:
std::string str;
std::string numToString(int num);
std::string opToString();
You can keep from having to type out std:: everywhere by specifying a using statement:
using std::string;
But you might not want to do that inside a header file, so stick with fully qualifying the type.
If you want to use , you need to refer to it with std::
For example, your expression class declares:
st::string str;
string numToString(int num);
string opToString();
Which should be:
std::string str; // you typed st:: instead of std::
std::string numToString(int num); // lack of std::
std::string opToString(); // lack of std::
If you dont use 2 files (cpp + h) to define and declare your class then you can add line
using namespace std;
just after your includes. This way you wont have to type std:: each time you try to refer to string and similar types. However, using this is often called a bad "beginner" practice.
If you do use cpp+h then just add std:: before every string type and add using namespace std; to your cpp file.
If you want to know more then read:
1. http://www.cplusplus.com/doc/tutorial/namespaces/
2. Why is "using namespace std" considered bad practice?
3. How do you properly use namespaces in C++?
You also need to move
#include "stdafx.h"
up so it is the first header included. The compiler ignores everything that comes before that magic line.