按姓氏排序列表

时间:2015-05-13 20:11:52

标签: c++ sorting

Jason Chimera SE 7039990101

Mike Knuble SSE 7039990102

Karl Alzner JSE 7039990202

Tom Poti SE 7039990203

Alex Ovechkin EGM 7039990001

我正在尝试从文件中读取上述信息并按姓氏对其进行排序。我该如何解决这个错误:

错误:预期为':'

我在EmployeeInformation类中声明的字符串?

 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;

}

4 个答案:

答案 0 :(得分:7)

很多小错误。

公共部门。

在C ++中,public后跟':'来标记一个部分。您还需要包含string定义。它位于std::命名空间中。

  public: std::string firstName;
  public: std::string lastName;
  public: std::string title;
  public: std::string phonenumber;

C ++在类型上非常迂腐。

您需要指定所有参数的类型:

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.

一旦你修好它就会编译。但我会将固定版本带到code review以获得更详细的细分。

当所有完整的main看起来像这样:

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));
}

答案 1 :(得分:4)

您的课程未正确宣布。例如,您不能从构造函数的参数中省略数据类型。

您也没有正确阅读文件。

尝试更像这样的事情:

#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;
}

答案 2 :(得分:3)

访问修饰符的语法不正确(您使用的是Java样式而不是C ++)。在C ++中,您可以像这样声明这个完全公共的类:

 class EmployeeInformation {
 public:
   string firstName;
   string lastName;
   string title;
   string phonenumber;
   //...
};

如果你确实想要公开所有数据,你也可以使用结构而不是类,因为结构的成员默认是公共的。

答案 3 :(得分:2)

首先,你的类中的变量声明是错误的。然后在构造函数中,你没有告诉数据类型。参见

 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
}
};

然后你错过了一个分号。

sort(employees.begin(), employees.end(), [](EmployeeInformation a, EmployeeInformation b) { return a.lastName < b.lastName; });  //there should be a semicolon after return statement