What is wrong with my vector declaration in c++? - c++

I'm having trouble declaring a vector of class objects. The class I'm trying to reference is in a header file and its constructor calls for an array of 6 strings.
string Data[6];
vector<Gamer> fileOut;
void split(string input)
{
stringstream ss(input);
int count = 0;
while (ss.good()) {
string substr;
getline(ss, substr, ' ');
Data[count] = substr;
count++;
}
}
void readGamerfile()
{
ifstream myfile;
myfile.open("gamers.txt");
string line;
while (getline(myfile, line)) {
split(line);
fileOut.push_back(new Gamer(Data));
}
}
Above is my code and I'm not sure why when I declare the variable fileOut I get an error saying:
identifier "gamer" is undefined
I'm using visual studio code and I'm not really sure whats going on because I believe it should work. Any help would be greatly appreciated.

Since your vector is declared as follows
vector<Gamer> fileOut;
you do not need new here
fileOut.push_back(Gamer(Data));
or even
fileOut.emplace_back(Data);
Also make sure you include whatever header is required for the definition of Gamer
#include "Gamer.h"

If Gamer is a structure the declaration should be vector<struct Gamer> or if it is an object then vector<className>.

Related

C++ class getter not returning any value in main, but does in the class?

I am hoping someone can point me in the direction, why my getter function cannot seem to be accessed properly when it is used outside the function in which the setter is declared.
I am reading in a file, storing variables of each line using the getline function, and then assigning the message to a private variable in Message.h.
When I cout the m1.getmessage() in within the readfile() function it outputs absolutely fine, with the correct output (The message line from my text file), however it just gives blank output in the main(). I've been trying to get my head around it for quite a few hours now, I have been reading about local variables but as far as I can see the variable is already set, and in a public function, therefore I can't see where I'm going wrong. Any help would be really appreciated before I reach for the vino at 4am.
Message.h
#include <iostream>
class Message {
private:
std::string message;
std::string cipher;
public:
void readFile();
void setMessage(std::string msg) {
message = msg;
}
void setCipher(std::string ciph) {
cipher = ciph;
}
std::string getMessage() {
return message;
}
std::string getCipher() {
return cipher;
}
};
Message.cpp
#include "Message.h"
#include <fstream>
#include <iostream>
void Message::readFile() {
std::string fileUsername;
std::string fileForename;
std::string fileSurname;
std::string fileAge;
std::string fileTime;
std::string fileHour;
std::string fileMin;
std::string fileSec;
std::string fileCipher;
std::string fileMessage;
Message m1;
std::fstream file;
std::string filename;
std::cout << "Please enter file name: " << std::endl;
getline(std::cin, filename);
file.open(filename);
if (file.is_open()) {
std::cout << "File opened" << std::endl;
} else {
std::cout << "Wrong file name" << std::endl;
}
while(file.is_open()) {
getline(file, fileUsername);
getline(file, fileForename);
getline(file, fileSurname);
getline(file, fileAge);
getline(file, fileHour, ':');
getline(file, fileMin, ':');
getline(file, fileSec);
getline(file, fileCipher);
getline(file, fileMessage);
file.close();
}
m1.setMessage(fileMessage);
m1.setCipher(fileCipher);
m1.getMessage();
};
Main.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Message.h"
#include "Caesar.h"
#include "XOR.h"
int main() {
Message m1;
m1.readFile();
std::cout << m1.getMessage();
return 0;
}
The cout in the main is returning nothing, whereas if I transfer it into m1.readfile() it outputs the variable perfectly.
This is my first attempt at object orientated programming and it's definitely my weak spot. Thanks in advance for any advice.
In the Message::readFile() function, you are not calling the functions setMessage and setCipher on the current object, but on a local variable m1. The local variable is discarded at end of the function and the message and cipher don't end up getting saved. You should instead just call
setMessage(fileMessage);
setCipher(fileCipher);
getMessage();
instead of
m1.setMessage(fileMessage);
m1.setCipher(fileCipher);
m1.getMessage();
This will update the message and cipher variables of the current object and you can then print getMessage() from the main function.
you create a local variable m1 in readfile. And then it gets discarded when the function ends. Probably you want to either return the object, or set the current instance
You don't have to create an object inside that class's member function
My suggestion:
Omit / Remove Message m1; in the readFile()
Directly call the fuctions as
setMessage(fileMessage);
setCipher(fileCipher);
getMessage();
You can use this; pointer to object that your currently working on.
like below.
this->setMessage(fileMessage);
or
(*this).setMessage(fileMessage);

Can an istream variable be a class variable

While developing a program in C++ using VS2010 , can I define
std::istream streamRead(ReadBuf&); // struct ReadBuf : public std::streambuf declared before
and use this streamRead in multiple functions in my program?
If not, can anyone suggest me how to read a stream using getline. I have to read the same stream from different functions.
Thank you in advance.
EDIT:
The struct declared in my header file is as below:
struct ReadBuf : public std::streambuf
{
ReadBuf(PBYTE s,size_t n)
{
setg((char*)s,(char*) s,( char*)s + n);
}
};
I have a buffer in memory and the input to my program is its pointer and size. Using the above structure, I copy it to a streambuffer. Now I have to read this streambuffer line by line. This is my requirement.
For example some of my functions are:
int GetSessionN(int session_id,SessionDetail &N_session);
int GetInstanceId(string header,SessionDetail &N_session);
int GetDriverDetails(string body_data,SessionDetail &N_session);
I have to read the first n lines from the stream using GetSessionN and then the successive n lines in the next function and so on.
This is where I initialise the object of ReadBuf. I am not able to initialize it globally.
int SetupLogReader::ProcessLogFile(PBYTE &mem_ptr, ULONG &size)
{
string read;
ReadBuf buf(mem_ptr, size);
istream streamRead(&buf);// Not able use StreamRead declared in header here.
}
you should not copy the stream when returning it in the function but reference it, i.e:
std::istream &streamRead(ReadBuf&){
if (_stream == null){
// create stream
_stream = [newly created stream];
}
return _stream;
}
Edit:
You could also use std::istringstream as it already provides the functionality you are looking for:
from istringstream manual:
std::string stringvalues = "line1\nline2";
std::istringstream iss (stringvalues);
for (int n=0; n<2; n++)
{
char val[256];
iss.getline(val, 256);
std::cout << val << '\n';
}

Extract information from a colon delimited file - C++

I am trying to extract information into class objects from a colon delimited file. Each line of the file is set up in the same format. Here are the first few lines of the file:
s:Charles:Babbage:80:530213286:1133764834:mechanical engineering:3.8
e:Marissa:Meyer:37:549114177:53321:ceo:4456000
s:Alonzo:Church:92:586312110:1100539644:mathematics:4.0
e:Dana:Ulery:74:573811211:23451:engineer:124569
This is a school project and the purpose is to teach us about class inheritance. We have a base class Person and two child classes Student and Employee. We are supposed to import and store the information for students into Student objects and employee into Employee objects. I have an array of objects for each class; I'm sorting the students into the array of Student objects, same for employees, and in addition adding all people to the array of People objects.
I don't know what to do to get each piece of information with the delimiting commas. Right now I'm trying to use .getline but it doesn't seem to be working. How do I use this function (or another function) to extract information between delimiters into char arrays? Here's what I have so far for the case that the data is for an employee:
ifstream fin;
char* tempImport;
tempImport = new char[50];
int* tempIntArray;
tempIntArray = new int[10];
double tempDouble;
int tempInt;
// get the specifier of student or employee
fin.getline(tempImport, ':');
if(tempImport[0]=='e'){
// get first name
fin.getline(tempImport, ':');
employees[employeeIndex].setFirstName(tempImport);
allPeople[personIndex].setFirstName(tempImport);
// get last name
fin.getline(tempImport, ':');
employees[employeeIndex].setFirstName(tempImport);
allPeople[personIndex].setFirstName(tempImport);
// get age
fin.getline(tempImport, ':');
employees[employeeIndex].setAge(tempImport[0] - 0);
allPeople[personIndex].setAge(tempImport[0] - 0);
// get SSN
fin.getline(tempImport, ':');
for(int i=0;i<9;i++){
tempIntArray[i] = tempImport[i] - 0;
}
employees[employeeIndex].setSsn(tempIntArray);
allPeople[personIndex].setSsn(tempIntArray);
// get Employee ID
fin.getline(tempImport, ':');
for(int i=0;i<5;i++){
tempIntArray[i] = tempImport[i] - 0;
}
employees[employeeIndex].setEmpID(tempIntArray);
// get title
fin.getline(tempImport, ':');
employees[employeeIndex].setTitle(tempImport);
// get salary
fin >> tempDouble;
employees[employeeIndex].setSalary(tempInt);
employeeIndex++;
personIndex++;
}
It looks like you're missing a parameter when you call ifstream::getline(). See:
http://www.cplusplus.com/reference/istream/istream/getline/
You need the 3-parameter version of the method in order to specify a delimeter. When you call the 2-parameter version it interprets ':' as the streamsize. (Basically, ':' just resolves to the ASCII code for a colon, so that number gets passed in. What you really want for streamsize is the length of your tempImport buffer.)
However, if I may suggest (and your assignment allows it), the std::getline() version of the function may be better. (It allows you to use std::string instead of char*, which is a more C++ish way of doing things. Also you don't have to worry about if your input is bigger than your buffer.) Here's the documentation on that:
http://www.cplusplus.com/reference/string/string/getline/
So basically you could do something like this:
std::string tempImport;
std::getline(fin, tempImport, ':');
As a debugging suggestion, you could print tempImport after each time you call getline() on it (regardless of which kind you use). Take those out before you submit, but those print statements could help you debug your parsing in the meantime.
std::stderr << "getline(): " << tempImport << std::endl;
Edit:
Regarding the comment below, I was able to get this to compile. (It doesn't do anything useful, but shows that std::getline() is indeed present and compiles.) Does it compile for you?
#include <fstream>
int main (int argc, char** argv)
{
std::ifstream ifs;
std::string str;
std::getline(ifs, str, ':');
return 0;
}
If you'll pardon my saying so, you seem to have been taught one of the worst possible imitations of 'object oriented programming' (though if it's any comfort, it's also a fairly common one).
Personally, I think I'd write things quite a bit differently.
I'd probably start by eliminating all the setSalary, setTitle, etc. They're a horrible perversion of what OOP was supposed to do, losing a great deal in readability while gaining nothing in encapsulation.
Rather than providing member functions to manipulate all the members of the class, the class should provide a higher-level member to reconstitute an instance of itself from a stream.
When you do get the data, you probably do not want to create separate objects for your arrays of People/Employees/Students. Rather, each item will go into the array of either Employees or Students. Then the People will be just an array of pointers to the items in the other two arrays.
As to the details of reading the data: personally I'd probably write a ctype class that classified : as whitespace, and then just read data. For your class, you probably want to stick to using getline though.
class Person {
virtual std::istream &read(std::istream &is);
friend std::istream &operator>>(std::istream &is, Person &p) {
return p.read(is);
}
};
class Student : public Person {
std::string first_name;
std::string last_name;
std::string age;
std::string ssn;
std::string ID;
std::string title;
std::string salary;
virtual std::istream &read(std::istream &is) {
std::getline(is, first_name, ':');
std::getline(is, last_name, ':');
std::getline(is, age, ':');
// ...
return is;
}
};
With those in place, reading data from the file will normally be pretty simple:
std::string t;
Employee e;
Student s;
while (std::getline(infile, t, ':'))
if (t == "e") {
infile >> e;
Employees.push_back(e);
}
else if (t =="s") {
infile >> s;
Students.push_back(s);
}

arrays of structures and file I/O

//i have two errors in my code
#include <iostream>
#include<iomanip>
#include<fstream>
using namespace std;
struct PLAYER
{
string first_name;
string last_name;
};
void showFile(fstream&, PLAYER&); // line 13
int main()
{
const int MAX=21;
PLAYER array[MAX];
ifstream inputFile;
inputFile.open("PlayerNames.txt",ios::in);
if(inputFile)
{
showFile(inputFile, array); // line 22
}else
cout<<"\n\nError opening file";
inputFile.close();
return 0;
}
void showFile(fstream &file, PLAYER &array )
{
int index=0;
int p_num=1;
string lname, fname;
file>>lname>>fname;
while(!file.eof())
{
// array[index].last_name=
cout<<lname<<" "<<fname;
//array[index].first_name=fname;
//array[index].player_number=p_num;
index++;
p_num++;
file>>lname>>fname;
}
// cout<<"\n"<<index;
//cout<<lname<<" "<<fname;
}
This program worked finally untill i put it in functions.
I have two errors in this program
line 22 error: invalid intialization of reference type std:: fstream
line 13 error: in passing argument 1 of void showFile(std:: fstream&, PLAYER&)
An ifstream can't be converted to an fstream, only an istream.
In the documentation you can see basic_ifstream is derived from basic_istream, not basic_fstream.
Make your function:
void showFile(istream&, PLAYER&);
This is actually better in many ways. For one it's correct (c: But also it means you can test it with any input stream rather than just a file stream. It is more loosely coupled and programs to the more abstract interface.
You function declaration at line 13 shows you are passing 1 PLAYER object, not an array. If you want to stay with arrays, search StackOverflow for "[C++] pass array function".
I highly recommend using std::vector as it has easier syntax when passing to functions.

How to pass cin or ifstream object as argument function

I though this would work since ifstream inherits from istream
string getFileContents(istream& file_contents)
{
string result;
string line;
while (getline(file_contents, line))
result += line + "\n";
return result;
}
then I want to call this function like so:
ifstream file_input;
getFileContents(file_input);
...
getFileContents(cin);
but I get this error in visual studio:
'getFileContents' : cannot convert parameter 1 from std::istream to std::ifstream &
It should work; are you sure you didn't leave around a wrong prototype that has a parameter of type ifstream & instead of istream &?