>> operator on ifstream - c++

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.

Related

C++ a reference of type "std::ostream &" (not const-qualified) cannot be initialized with a value of type "std::ofstream"

I have this section of code:
std::ofstream RBXMX_Out;
std::string RBXMX_FileName;
Whenever I try to compile, I always get this error:
RBXMX_Out = std::ofstream(RBXMX_FileName, std::ios_base::out | std::ios_base::binary);
Since there's no answers to be found on Google, I wanted to ask the Stack Overflow community, now, I have included this headers:
#include "stdafx.h"
#include "Scanner.h"
#include <sstream>
#include <vector>
#include <iostream>
#include <string>
Scanner.h is only a memory scanning header file, and does not have any errors in it.

ostringstream gives me Implicit instantiation error

I'm trying the elegant solution provided in this answer but no matter what I try, I cannot get passed the error Implicit instantiation of undefined template for this line:
std::ostringstream strs;
What I need to do? I've included the following, which I'm sure is overkill. As a second question it seems hard to track down what exactly needs to be included for ostringstream:
#include <iostream>
#include <fstream>
#include <iosfwd>
#include <ios>
stringstream classes are defined in sstream header, so write
#include <sstream>
and all will be fine.

`operator>>' is not a member of `Instance'

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?

Visual C++ No operator ">>" matches these operands

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.

Printing out std::string

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.