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.
Related
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.
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 a problem that does no resurface (even no warnings) in XCode but does allow me to compile in Keil MDK.
void grammar::parse(std::string &_expr) {
std::transform(_expr.begin(), _expr.end(), _expr.begin(), std::tolower);
_expr.erase(std::remove_if(_expr.begin(), _expr.end(), std::isspace), _expr.end());
}
That is what I get
error: #304: no instance of overloaded function "std::transform" matches the argument list
error: #304: no instance of function template "std::remove_if" matches the argument list
Header included:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <algorithm>
Could you please advise me on where to look? I am surprised that XCode version works as expected...
You include ctype.h, that header declares a function tolower in the global namespace (this is part of the C library, so there are no other namespaces there). Maybe you meant to include cctype. For a given C standard library header X.h, there is a c++ version cX that provides some of the same functionality inside the ::std namespace.
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.
I'm trying to use std::getline, but my compiler is telling me that getline isn't identified?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <fstream>
#include <cstdlib>
int main(){
using namespace std;
string line;
ifstream ifile("test.in");
if(ifile.is_open()){
while(ifile.good()){
getline(ifile,line);
}
}
}
std::getline is defined in the string header.
#include <string>
Also, your code isn't using anything from cstring, cstdio, cmath, or cstdlib; why bother including these?
EDIT: To clarify the confusion regarding the cstring and string headers, cstring pulls the contents of the C runtime library's string.h into the std namespace; string is part of the C++ standard library and contains getline, std::basic_string<> (and its specializations std::string and std::wstring), etc. -- two very different headers.
As ildjarn points out, the function is declared in <string>, and I'm suprised you didn't get an error at:
string line;
Also, this:
while(ifile.good()){
getline(ifile,line);
}
is not the way to write a read loop. You MUST test the success of the read operation, not the current stream state. You want:
while( getline(ifile,line) ) {
}
this is happening because getline comes from the string library, you need to #include <string> or #include <cstring>