I have the InstancePool class (part of it below) with the Instance.h header included, but I get the error in the title in the operator>> function of InstancePool.
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <stdlib.h>
using namespace std;
#include "Instance.h"
#include "InstancePool.h"
istream &operator >> (istream &in , InstancePool &ip) {
ip.Instances->clear();
string input;
getline(in , input);
while (!in.eof()) {
Instance inst;
Instance::operator >>(in , inst); // <- line giving me the error
ip.Instances->push_back(inst);
getline(in , input);
}
}
The InstancePool operator>> function is a 'friend' function BTW, and so is the same function in Instance. Probably I'm trying to access the Instance 'operator>>' in the wrong way but I'll be damned if I know the correct one...
Any help?
Friend functions aren’t member functions, you cannot explicitly qualify the function’s name the way you did because it’s simply not inside a namespace called Instance.
The good news is: you don’t need to. Just call it normally:
in >> inst;
There are more bugs in your code, though. First off, while (in.eof()) will result in an infinite loop when there’s an error while reading – never do this.
Second of all, you’re reading and discarding lines with getline. This is probably not what you want to do, right? Do you want to read each instance from the line or directly from the input stream?
Related
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.
wondering what the mistake is..
c:lab1a.cpp(16): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion)
#include "StdAfx.h" // precompiled
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
// No checking of end of stream etc.
void getw(string& t, ifstream& in)
{
in >> t;
}
The key error with this code is that it is missing the <string> header.
Other than that the interface is inherently broken as it doesn't support checking for errors (I realize that the stream's state can still be checked but returning the stream's state makes it much more likely that it is checked). Also, a function like that should be implemented in terns of std::istream rather than std::ifstream as it doesn't use any specifics of std::ifstrean.
I got a strange problem with my program. So in the header I got something like this:
#ifndef SET1_INCLUDED
#define SET1_INCLUDED
#include <iostream>
using namespace std;
typedef std::string ItemType;
class Set1{
public:
------some public constructor and method in here-------
private:
ItemType setMember[100];
}
in 1 part of my function in the Set1.cpp file I got something like this :
if (setMember[i] == "foo") {exist = true;}
In this case, I got an error message that says "no operator found which takes a left-hand operand of type 'ItemType' ". However, if I change the std::string in the typedef into int or unsigned long, and change "foo" to some random number, the code works perfectly. Any suggestion? thx
You are missing the header file <string>, which means that you don't have all of the global operator == definitions visible in your program. This is likely the case of your problem.
To fix this, try adding in this line:
#include <string>
Hope this helps!
You need to include string header file to bring relative types and operator to scope.
#include <string>
Note:
It's bad coding practice to pull in everything from std namespace in header file.
//using namespace std;
I have been searching around for a solution to this for a while. I'm trying to use the ">>" and "<<" operators as shown below but keep getting the error "No operator '>>' matches these operands". Here are the code snippets:
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
...
int blah;
std::string level;
level >> blah;
thisDot.setX(blah);
level >> blah;
thisDot.setY(blah);
Several of the posts I have found on this topic have had the solution of simply including #include <string>, but as you can see I have included that and am still getting the error. I have also tried #include string.h but that doesn't help either. Any ideas as to why this would still be happening?...it may be worth noting that I'm using the SDL library.
You misread the example. The example is reading from a file, not a string.
std::string level;
// ...
load >> offset;
Notice that it is load that appears on the left of the >>, not level.
I am trying to change a certain text box message. It will display my output.
This is what I have in my main()
#include "form2.h"
....
string recvMSG = "random";
182:: Form2::changeOutput(recvMSG);
...
within my form2.h I have:
#include <string.h>
#include <iostream>
#include <stdlib.h>
...
void Form2::changeOutput(string s)
{
QString s1 = i18n(s);
output_box.setText(s1);
}
But i still get:
.ui/form2.h:56: error: ‘string’ has not been declared
Thanks.
Edit:: kk so now its showing::
TCPClient.cpp:182: error: cannot call member function ‘virtual void Form2::changeOutput(std::string)’ without object
string is in the std namespace, so you either need to refer to it as std::string, or you need to make the name available in the current scope with using namespace std; or using std::string;.
Also the header is called string, not string.h, so include it this way:
#include <string>
Generally you also might want to use QT's QString instead of std::string if you are using it in connection with QT components that usually take QString parameters.
I guess you should use the header <string> and then use std::string (even better would be const std::string &)