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.
Related
So I was starting to write my code and I was going to test to see if I still remember how to cast, until I get a red line under my operator.
This is the compiler error:
Error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' (or there is no acceptable conversion) (12)
I honestly never had a problem with outputting a string/vector so I do not know how to fix this. Can someone please tell me how to fix this. It would also be awesome if you could tell me what is wrong with the code.
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string>hello;
hello.push_back("9");
for (auto i : hello)
cout << i << " "; <-- The first operator is underlined. Why?
return 0;
}
You need one more include in your program:
#include <string>
While <iostream> does declare/define some string related functions, not all of them.
With some compilers, the iostream header incldues string internally, but that isn't required by the standard - and Visual Studio doesn't, that's why you receive this error.
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 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?
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;
In the sample code below
std::string result = exec( "dir" ) ;
cout<<result;
I get the following error
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string
I suspect there is a special method to print out an std::string.
Please help me debug this.
Also, I have included iostream.h, fstream.h and stream header files.
I suspect that you need to qualify cout with std::
std::cout << result;
or add using namespace::std to the top of your cpp file.
You need to include <string>
cout is defined in <iostream>. Getting the << syntax to work with std::strings requires <sstream>.
#include <iostream>
#include <sstream>
std::string result = "something";
std::cout << result << " and something else";
Answering my own question on behalf of #MrLister since he was inactive.
I should have included <iostream> and <fstream> without .h. Also using namespace std; should have been typed after that.
Ex:
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib>
using namespace std;
Many many thanks to #MrLister.
And thanks to #dasblinkenlight. His answer enhanced a little bit.