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

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;

Related

"no operator >> matches these operands"

I'm a complete noob at C++, and the first problem I am encountering is the following:
no operator >> matches these operands
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "hello world!";
cin >> "hello world!";
}
std::cin needs to write to a variable, but you are passing it a const char[13] string literal instead.
You need to pass it something like a std::string instead:
std::string str;
std::cin >> str;
P.S. This is a good time to a) read compiler messages, b) avoid using namespace std; globally, c) get a good C++ book.

Why is g++ Giving Me Conflicting Errors While Compiling? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am attempting to compile my second, (still noobish) C++ program, and g++ is giving me these errors:
new.cpp: In function ‘int main()’:
new.cpp:10:4: error: ‘cin’ was not declared in this scope
cin >> name;
is the first. Here's the second:
^~~
new.cpp:10:4: note: suggested alternative:
In file included from new.cpp:1:
/usr/include/c++/8/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^~~
and I believe these are telling me to change both ways to write it to the other. I have tried changing both, and I'm not sure how to fix this. Here is the program:
#include <iostream>
#include <string>
int main() {
std::string age;
std::string name;
std::cout << "Please input your age.";
std::cin >> age;
std::cout << "Please input your name.";
cin >> name;
return 0;
}
(CLOSED)
Here is a little bit of explanation for a c++ and g++ newbie:
new.cpp:10:4: error: ‘cin’ was not declared in this scope
cin is declared under the std namespace. See https://en.cppreference.com/w/cpp/io/cin
The second one is not an error, but a suggestion by the compiler by pointing to the alternative found by the compiler. It gives a hint about std::cin.
note: suggested alternative:
In file included from new.cpp:1:
/usr/include/c++/8/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^~~
At line 10, you are using cin from the global namespace. Therefore, the compiler complains that it can't find the declaration of cin.
Our fellow already provided a fix for you by changing line 10 to: std::cin >> name;.
#include <iostream>
#include <string>
int main() {
std::string age;
std::string name;
std::cout << "Please input your age.";
std::cin >> age;
std::cout << "Please input your name.";
std::cin >> name;
return 0;
}

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.

How does stringstream work with <<? [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 7 years ago.
Improve this question
I am trying to use << as a means of moving integers into a stringstream. There must be something fundamental and basic I am overlooking. The simplest of code does not even compile:
std::stringstream ss;
ss << "simple test ";
produces this error:
error C2297: '<<' : illegal, right operand has type 'const char [13]'
That is not a valid C++ program.
First, you need to include sstream. Then, you need to put that expression with << into a function.
Like this:
#include <sstream>
int main()
{
std::stringstream ss;
ss << "simple test ";
}
This worked:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
stringstream ss;
string s;
ss << "simple test ";
s = ss.str();
cout << s;
return 0;
}

C++: getline not part of argument list

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 ... )