write to ostream inside of function with c++ - c++

consider the following function:
void writer(ofstream &output) {
output << "a string to write" << endl;
}
but when i try to call this, i get an error that '<<' is not defined. how can I fix this?

Add #include <fstream> or change ofstream to ostream
Most likely you lack an include, so that ofstream is only forward declared.

Related

cout not working with ostream header file

I have read that cout is an object of ostream...
But why does this code
#include<ostream>
using namespace std;
int main()
{
cout << "ostream included!" << endl;
return 0;
}
Throwing an error :-
practice1.cpp: In function 'int main()':
practice1.cpp:6:1: error: 'cout' was not declared in this scope
cout << "ostream included!" << endl;
^~~~
Am I going wrong in my understanding or is there some other fault?
(MinGW windows 10)
Thanks in advance!
Description
The reason of why this doesn't work is because cout is of type OStream but is inside the IOStream header. Hence, to get the definition of cout you need to include iostream library but not the ostream class.
Solution
Include iostream instead of ostream, as OriBS mentioned.
References
Object cout found in IOStream objects list
"Including iostream automatically includes also ostream..." see http://www.cplusplus.com/reference/iostream/
"The standard objects cout, cerr and clog are objects of this type." see http://www.cplusplus.com/reference/ostream/ostream/
You should include iostream
#include <iostream>

How do I assign text to the "void PrintIntro" function?

So far I have typed this,
#include iostream
using namespace std;
void PrintIntro();
I want to assign a actual text now to the PrintIntro function so that in my main program I can just type
PrintIntro() and when the program runs the text assign to the function will show.
So far I have tried this after "void PrintIntro();"
{ /*PrintIntro*/
cout <<
"==================================================" << endl;
cout <<
"Welcome to the Math Practice Program!!!!!" << endl;
cout <<
"This Program will help you practice elementary math" << endl;
cout <<
"==================================================" << endl;
/*PrintIntro*/
}
But then I get the error under the "{" symbol indicating that it is "expecting a declaration." I have been searching through notes and messing with this all day and I cannot figure it out. Any help would be appreciated. I am using MS Visual studio Express 2013.
Adding the semicolon after void PrintIntro() tells the compiler that there is a function called PrintIntro that takes no arguments and returns void, and that you are defining it later. This is called a forward declaration. Chances are this is what's happening:
void PrintIntro();
//Compiler: okay, that was a forward declaration
{
//Compiler: wth is this stuff?
}
You want this to happen:
void PrintIntro();
//Compiler: okay, that was a forward declaration
void PrintIntro()
{
//Compiler: oh, this is the definition for that function you told me about earlier
}
Or you want to do it without the forward declaration:
void PrintIntro() //no ';'
{
//Compiler: declaration and function body all in one part - simple!
}
You should also change #include iostream to #include <iostream>
Remove the ; after you declare you function like:
void PrintIntro()
When declaring a function, you do not need to end the function declaration line with a semicolon.
So your function should look like
void PrintIntro(){
blahblahblah...
}
You need to remove the semi column at the end of the declaration
void PrintIntro();
Must be like this
void PrintIntro(){
}
A couple of errors, most of them in the syntax I think. Note that the line #include iostream should actually be #include <iostream> and the ; is missing after void PrintIntro().
Like so
#include <iostream>
using namespace std;
void PrintIntro(){
cout << "Hello world" << endl;
}

error no instance of overloaded function "getline" matches the argument list c++

error no instance of overloaded function "getline" matches the argument list
I cant seem to see what is wrong. I feel like I am passing the correct arguments (ie the std::ofstream and the std::string). Any help would be great thanks.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
ofstream yourFile;
myfile.open ("read.cc");
yourFile.open ("write.cpp");
string line;
This section in particular is the one that is getting an error.
if (myfile.is_open()){
The getline in the while loop is red and is giving me the overload error.
while(getline(myfile,line)){
yourFile << line <<"\n";
}
}
myfile.close();
yourFile.close();
return 0;
}
I thought I had set up the streams correctly.
An output stream is for writing to. For reading from, you want an input stream:
std::ifstream myFile;
// ^^

How do I use fstream (specifically ofstream) through a functions parameters

Hi I'm a c++ beginner and this is one of my assignments and I'm a bit stuck. This isn't my entire code it's just a snippet of what I need help with. What I'm trying to do is have one function dedicated to exporting everything with that function into a text file which is called results.txt. So the line "does this work" should show up when I open the file, but when I run the file I get errors like
"Error C2065: 'out' : undeclared identifier"
"Error C2275: 'std::ofstream' : illegal use of this type as an expression"
"IntelliSense: type name is not allowed"
"IntelliSense: identifier "out" is undefined"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//prototypes
void output(ofstream& out);
int main()
{
output(ofstream& out);
ifstream in;
in.open("inven.txt");
ofstream out;
out.open("results.txt");
return 0;
}
void output(ofstream& out)
{
out << "does this work?" << endl;
}
Right now it's really late and I'm just blanking out on what I'm doing wrong.
First of all, this is fine:
void output(ofstream& out)
{
out << "does this work?" << endl;
}
However, this is not:
int main()
{
output(ofstream& out); // what is out?
ifstream in;
in.open("inven.txt");
ofstream out;
out.open("results.txt");
return 0;
}
This is the first error you get: "Error C2065: 'out' : undeclared identifier", because the compiler doesn't know about out yet.
In the second fragment you want to call output with a specific ostream&. Instead of calling a function, you're giving a function declaration, which isn't allowed in this context. You have to call it with the given ostream&:
int main()
{
ifstream in;
in.open("inven.txt");
ofstream out;
out.open("results.txt");
output(out); // note the missing ostream&
return 0;
}
In this case you call output with out as parameter.
Since you described yourself as a begginer, I'll answer accordingly and hopefully in a educational manner. Here is what is happening: Think of fstream, ofstream and ifstream as smart variable types (even if you know what classes are, think like that for the sake of logical clarity). Like any other variable, you have to declare it before you use. After it is declared, that variable can hold a compatible value. The fstream variable types is for holding files. All variations of it hold the same thing, just what they do that is different.
You use the variable to open a file, use it in your program, then close.
Hope this helps

expected constructor, destructor, or type conversion before ‘(’ token

Compiling polygone.h and polygone.cc gives error:
polygone.cc:5:19: error: expected constructor, destructor, or type conversion before ‘(’ token
Code:
//polygone.h
# if !defined(__POLYGONE_H__)
# define __POLYGONE_H__
# include <iostream>
class Polygone {
public:
Polygone(){};
Polygone(std::string fichier);
};
# endif
and
//polygone.cc
# include <iostream>
# include <fstream>
# include "polygone.h"
Polygone::Polygone(string nom)
{
std::ifstream fichier (nom, ios::in);
std::string line;
if (fichier.is_open())
{
while ( fichier.good() )
{
getline (fichier, line);
std::cout << line << std::endl;
}
}
else
{
std::cerr << "Erreur a l'ouverture du fichier" << std::endl;
}
}
//ifstream fich1 (argv[1], ios::in);
My guess is that the compiler is not recognising Polygone::Polygone(string nom) as a constructor, but, if this actually is the case, I have no idea why.
Any help?
This is not only a 'newbie' scenario. I just ran across this compiler message (GCC 5.4) when refactoring a class to remove some constructor parameters. I forgot to update both the declaration and definition, and the compiler spit out this unintuitive error.
The bottom line seems to be this: If the compiler can't match the definition's signature to the declaration's signature it thinks the definition is not a constructor and then doesn't know how to parse the code and displays this error. Which is also what happened for the OP: std::string is not the same type as string so the declaration's signature differed from the definition's and this message was spit out.
As a side note, it would be nice if the compiler looked for almost-matching constructor signatures and upon finding one suggested that the parameters didn't match rather than giving this message.
The first constructor in the header should not end with a semicolon. #include <string> is missing in the header. string is not qualified with std:: in the .cpp file. Those are all simple syntax errors. More importantly: you are not using references, when you should. Also the way you use the ifstream is broken. I suggest learning C++ before trying to use it.
Let's fix this up:
//polygone.h
# if !defined(__POLYGONE_H__)
# define __POLYGONE_H__
#include <iostream>
#include <string>
class Polygone {
public:
// declarations have to end with a semicolon, definitions do not
Polygone(){} // why would we needs this?
Polygone(const std::string& fichier);
};
# endif
and
//polygone.cc
// no need to include things twice
#include "polygone.h"
#include <fstream>
Polygone::Polygone(const std::string& nom)
{
std::ifstream fichier (nom, ios::in);
if (fichier.is_open())
{
// keep the scope as tiny as possible
std::string line;
// getline returns the stream and streams convert to booleans
while ( std::getline(fichier, line) )
{
std::cout << line << std::endl;
}
}
else
{
std::cerr << "Erreur a l'ouverture du fichier" << std::endl;
}
}
You are missing the std namespace reference in the cc file. You should also call nom.c_str() because there is no implicit conversion from std::string to const char * expected by ifstream's constructor.
Polygone::Polygone(std::string nom) {
std::ifstream fichier (nom.c_str(), std::ifstream::in);
// ...
}
You need the return type, for example "void Polygon..."