Jason Chimera SE 7039990101
Mike Knuble SSE 7039990102
Karl Alzner JSE 7039990202
Tom Poti SE 7039990203
Alex Ovechkin EGM 7039990001
I am trying to read in the information above from a file and sort it by last name. How can I fix this error:
Error:expected a ':'
for the strings I declared in the EmployeeInformation class?
class EmployeeInformation {
public string firstName;
public string lastName;
public string title;
public string phonenumber;
EmployeeInformation(&firstName, &lastName, &title, &phonenumber)
{
this->firstName = firstName;
this->lastName = lastName;
this->initials = title;
this->id = phoneumber;
}
};
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char**argv) {
ifstream file;
file.open("employees.txt");
if (!file.is_open())
cout << "Error opening file";
vector<EmployeeInformation> employees;
while (!file.eof())
{
string firstName, lastName, title, phonenumber;
file >> firstName >> lastName >> title >> phonenumber;
EmployeeInformation person(firstName, lastName, title, phonenumber);
employees.push_back(person);
// end file
}
sort(employees.begin(), employees.end(), [](EmployeeInformation a, EmployeeInformation b) { return a.lastName < b.lastName });
for (EmployeeInformation i : employees)
// print person info in sorted order
return 0;
}
Lot of minor errors.
In C++ public is followed by ':' to mark a section. Also you need to include the string
definition. Which lives in the std::
namespace.
public: std::string firstName;
public: std::string lastName;
public: std::string title;
public: std::string phonenumber;
You need to specify the types of all parameters:
EmployeeInformation(string const& firstName, string const & lastName, string const& title, string const& phonenumber)
this->initials = title;
// ^^^^^^^^ Not a member of this class. Did you mean title.
this->id = phoneumber;
// ^^ Not a member of this class. Did you mean phoneumber
return a.lastName < b.lastName
// ^^^^^ missing here.
Once you have those fixed it compiles. But I would take the fixed version to code review for a more detailed break down.
int main(int argc, char**argv)
{
std::ifstream file("employees.txt");
// Read data into vector
std::vector<EmployeeInformation> employees(std::istream_iterator<EmployeeInformation>(file),
(std::istream_iterator<EmployeeInformation>()));
// Sort container.
std::sort(std::begin(employees), std::end(employees),
[](EmployeeInformation const& lhs, EmployeeInformation const& rhs) { return lhs.lastName < rhs.lastName;});
// Copy container to stream.
std::copy(std::begin(employees), std::end(employees),
std::ostream_iterator<EmployeeInformation>(std::cout));
}
Your class is not declared correctly. For instance, you cannot omit data types from the constructor's parameters.
You are also not reading the file correctly.
Try something more like this:
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
class EmployeeInformation
{
public:
string firstName;
string lastName;
string title;
string phoneNumber;
EmployeeInformation(const string &_firstName, const string &_lastName, const string &_title, const string &_phonenumber) :
firstName(_firstName),
lastName(_lastName),
title(_title),
phoneNumber(_phoneNumber)
{
}
};
int main(int argc, char**argv)
{
ifstream file;
file.open("employees.txt");
if (!file.is_open())
{
cout << "Error opening file";
return 0;
}
vector<EmployeeInformation> employees;
string line;
while (getline(file, line))
{
string firstName, lastName, title, phonenumber;
istringstream iss(line);
iss >> firstName >> lastName >> title >> phonenumber;
EmployeeInformation person(firstName, lastName, title, phonenumber);
employees.push_back(person);
}
sort(employees.begin(), employees.end(), [](const EmployeeInformation &a, const EmployeeInformation &b) { return a.lastName < b.lastName; });
for (EmployeeInformation i : employees)
{
// print person info in sorted order
cout << i.lastName << " " << i.firstName << " " << i.title << " " i.phoneNumber << endl;
}
return 0;
}
Your syntax for access modifiers is incorrect (you're using Java style rather than C++). In C++ you would declare this fully public class like this:
class EmployeeInformation {
public:
string firstName;
string lastName;
string title;
string phonenumber;
//...
};
If you really do want to make all the data public, you could also use a struct instead of a class, since the members of structs are public by default.
First of all your variable declaration in class is wrong.Then in the constructor,you are not telling the data types.See this
class EmployeeInformation {
public:
string firstName;
string lastName;
string title;
string phonenumber;
EmployeeInformation(string &firstName,string &lastName,string &title,string &phonenumber)
{
this->firstName = firstName;
this->lastName = lastName;
this->title = title; //there is no variable named initial in class
this->phonenumber = phonenumber; //there is no variable named id in class
}
};
Then you are missing a semicolon.
sort(employees.begin(), employees.end(), [](EmployeeInformation a, EmployeeInformation b) { return a.lastName < b.lastName; }); //there should be a semicolon after return statement
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.