why am I getting an error when I pass an ifstream&? - c++

I'm trying to code a simple program that uses an ifstream and scanner to read a text file. For some reason I'm getting this error: "In passing argument 1 of 'bool ReadVector(std::ifstream&, Vector<double>&)'". Any idea what I've done wrong?
#include <iostream>
#include <fstream>
#include <string>
#include "scanner.h"
#include "genlib.h"
#include "simpio.h"
#include "vector.h"
// prototype
bool ReadVector(ifstream & infile, Vector<double> & vec);
// main
int main(){
Vector<double> vec;
ifstream infile;
infile.open("SquareAndCubeRoots.txt");
if (infile.fail()) Error("Opening file screwed up");
bool foo = ReadVector(&infile, &vec); // stub
cout << foo;
infile.close();
return 0;
}
// stub
bool ReadVector(ifstream & infile, Vector<double> & vec){
return true;
}

ReadVector accepts a reference, but you are giving a pointer. Just call
bool foo = ReadVector(infile, vec);

You're trying to pass a pointer, while the argument is a reference. Remove address-of operators (ReadVector(infile, vec)).

Related

How to initialize an object that declared as const on header file?

I want to initialize std::ifstream object only in the main() function after declare it in the header.
Is there any way to do it in C++?
I wrote this but it's not compiling
//header.h
#include <iostream>
#include <fstream>
class class1{
static const std::ifstream fs;
};
//proj.cpp
#include "header.h"
void main(){
class1::fs("Employee.txt")
}
static variables need to be defined at global scope, not inside a function.
main should also return int not void.
A const std::ifstream doesn't make much sense as most of the methods you would need to use are non-const so wouldn't be callable on your const stream.
Fixing these issues gives:
//header.h
#include <iostream>
#include <fstream>
class class1{
static std::ifstream fs;
};
//proj.cpp
std::ifstream class1::fs("Employee.txt");
int main(){
return 0;
}
If you want to open the stream in main then you need to do:
const std::ifstream class1::fs;
int main(){
class1::fs.open("Employee.txt");
return 0;
}

C++ range-v3: trying to chain together transforms

I'm completely new to the range library, so I shouldn't be surprised that this code isn't compiling and I cannot figure out why:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>
#include <range/v3/all.hpp>
#include <range/v3/view/all.hpp>
using namespace ranges::v3;
std::ifstream open_file(const std::string &filename) {
return std::ifstream{filename};
}
int count_lines(std::ifstream &in) {
return std::count(std::istreambuf_iterator<char>{in},
std::istreambuf_iterator<char>{}, '\n');
}
std::vector<int>
count_lines_in_files(const std::vector<std::string> &filenames) {
auto a1 = filenames | view::transform(open_file) | view::transform(count_lines);
return a1;
}
int main() {
const std::vector<std::string> files{"listing1_1.cpp",
"listing1_2.cpp",
"listing1_4.cpp",
"listing1_5.cpp"};
const auto result = count_lines_in_files(files);
std::cout << ranges::view::all(result) << '\n';
}
It appears that the complaint is about a1, which the compiler tells me "error: variable has incomplete type 'void'."
Can someone see what I'm doing wrong, or tell me how to properly chain these together if possible?
Thanks in advance!
As noted by Porsche9II, "std::ifstream doesn't have a copy constructor". You can find more on this topic here:
Why are iostreams not copyable?
C++11 introduced a move constructor (6) for std::basic_ifstream, so you could write
auto open_file(const std::string &filename) {
return std::ifstream{filename};
}
auto count_lines(std::ifstream &&in) {
return std::count(std::istreambuf_iterator<char>{in},
std::istreambuf_iterator<char>{}, '\n');
}
Testable HERE.
std::ifstream doesn't have a copy constructor - returning std::ifstream by a function is not a good idea. One possible solution: opening and counting should take place in one function.

.txt to vector <string>, non-class type error

Hi I just started to learn C++ this week and I require some assistance.
Basically what I am trying to do is read from a .txt file and "convert" it into a vector string and then display it.
my error is at this line: text.readFile("scenario.txt"), it says: "request for member 'readFile' in 'text', which is of non-class type 'Conversion()'"
what does that mean?
and also my method getLines() could not be resolved.
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "Conversion.h"
using namespace std;
int main()
{
vector<string> lines;
Conversion text();
if(text.readFile("scenario.txt") == true)
lines = text.getLines();
for(int i = 0; i < lines.size(); ++i)
cout << lines[i] << endl;
return 0;
}
Conversion.cpp
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Conversion.h"
using namespace std;
vector<string> lines;
Conversion::Conversion(std::vector<std::string> lines) {
lines.clear();
}
Conversion::Conversion() {
}
Conversion::~Conversion() {
}
bool Conversion::readFile(string filename) {
ifstream file;
string line;
file.open(filename.c_str());
if(!file.is_open())
return false;
while(getline(file, line))
lines.push_back(line);
return true;
}
vector<string> Conversion::getLines(){
return lines;
}
Conversion.h
#ifndef CONVERSION_H_
#define CONVERSION_H_
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
class Conversion {
public:
Conversion();
Conversion(std::vector <std::string>);
~Conversion();
std::vector<std::string> getLines();
bool readFile(std::string);
private:
std::vector<std::string> lines;
std::string line;
std::ifstream file;
};
#endif
Once again, Conversion text(); is a function declaration, not a class instantiation. To call the default constructor, change it to Conversion text;
You probably wanted to copy the passed lines in your constructor:
Conversion::Conversion(std::vector<std::string> const& lines) : lines(lines) { }
Your code should work now, but there can be done some improvements. To avoid copy, getLines should return by reference-to-const:
std::vector<std::string> const& getLines();
// you don't have to create lines in main, you can print like this:
for(auto const& x : text.getLines())
cout << x << endl;
and I'd use it even here:
bool readFile(std::string const&);
I hope this is the last thing - std::ifstream constructor and open function also take std::string:
file.open(filename);
You need to remove the parentheses when instantiating the Conversion object:
Conversion text;
See this question for detailed answers: Is no parentheses on a constructor with no arguments a language standard?

const char* usage in constructor

#include "Board.hpp"
#include <iostream>
using namespace std;
Board::Board (const char* filename){
filename = "puz1.txt";
Board::fin (filename);
if(!fin) fatal("Error in opening the file");
}
This is my cpp file...my hpp file is:
#ifndef BOARD_H
#define BOARD_H
#include <iostream>
using namespace std;
#include "tools.hpp"
#include "square.hpp"
class Board {
private:
SqState bd[81];
ifstream fin;
public:
Board(const char* );
ostream& print(ostream& );
};
inline ostream& operator <<(ostream& out, Board& b) { return b.print(out);}
#endif //Board.hpp
I got the below errors while I compile:
Error at line in cpp filename = "puz1.txt".
and error is:
const char* shadows a //parameter.
Error at line in cpp Board::fin (filename);
and error is:
no match call to //(std::basic_ifstream})
How do I fix them?
You can only initialize fin in the contructor initialization list. You also need to #include <fstream>. This would work:
Board::Board (const char* filename): fin(filename)
{
....
}
It is unclear why you are setting filemane to something different to what is passed in the constructor. If you want a default parameter, use
Board::Board (const char* filename="puz1.txt"): fin(filename) {}
About the first error:
filename = "puz1.txt";
You are supposed to pass the filename as an argument, not to assign it there. If you just need to use "puz1.txt" then use than instead of filename.
The second error:
Board::fin (filename);
You can't initialize the ifstream object like that. Simply call open().
fin.open("puz1.txt");
if(fin.is_open()) // you can pass additional flags as the second param
{
}

no matching function for call when trying to use get function from fstream library

I'm getting two errors in main that have me stumped:
"no matching function for call"
"invalid initialization of non-const reference of type 'int&' from a temporary of type 'int*'"
Could anyone lend a hand? thanks!
header
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cstdlib>
using namespace std;
void extern input(ifstream&, ofstream&, int&, int&);
#endif // HEADER_H_INCLUDED
main
#include "header.h"
using namespace std;
int main()
{
int grade;
int list[8];
ifstream inData;
ofstream outData;
inData.open("Ch9_Ex4Data.txt");
if (!inData)
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
outData.open("DataOut.txt");
inData.get(grade); // << ERROR 1 HERE
while (inData)
{
input(inData, outData, grade, list); // << ERROR 2 HERE
}
output (outData, list)
return 0;
}
Error 1 is because inData.get() does not take an int, you should do
grade = inData.get();
and the second is because list is actually an int* and not an int so the fourth parameter in input() should be an int* and not an int&
An int[] is not an int&. An int& is a reference to an integer. int list[8] is an array of 8 integers. They are not the same thing.
For your first error, you're passing an int instead of a char or char*. See below for get prototypes:
istream::get
public member function
int get();
istream& get ( char& c );
istream& get ( char* s, streamsize n );
istream& get ( char* s, streamsize n, char delim );
istream& get ( streambuf& sb);
istream& get ( streambuf& sb, char delim );
For the second error, you're wrongly passing an array of int where the function is expecting a int&.