将字符串元素添加到动态数组

时间:2017-03-30 21:28:37

标签: c++

解析文件并需要使用数组将学生添加到结构向量中,以用于特定于该课程的学生名称。

在我的course.h文件中:

struct Course {
    std::string name;
    int enrollment;
    int maxEnrollment;

    std::string* students; ///< array of student names

    Course(std::string courseName, int maxEnrollmentPermitted);

    bool enroll(std::string studentName);

    void print(std::ostream& output);
};

在我的course.cpp文件中:

bool Course::enroll(std::string studentName) {
    this->students = new std::string[studentName];
    if (this->enrollment < this->maxEnrollment) {
        this->enrollment++;
        return true;
    }
    else {
        return false;

在我的源文件中:

void processEnrollmentRequests(istream& enrollmentRequestsFile, vector<Course>& courses) {
    // Read the requests, one at a time, serving each one
    string courseName;
    enrollmentRequestsFile >> courseName;

    while (enrollmentRequestsFile) {
        enrollmentRequestsFile >> ws;
        string studentName;
        getline(enrollmentRequestsFile, studentName);

        int pos = search(courses, courseName);
        if (pos >= 0) {
            // Found a matching course
            bool enrolled = courses[pos].enroll(studentName);
            if (enrolled) {
                cout << studentName << " has enrolled in " << courseName << endl;
            }
            else {
                // course is full
                cout << studentName << " cannot enroll in " << courseName << endl;
            }
        }
        else {
            // course does not exist
            cout << studentName << " cannot enroll in " << courseName << endl;
        }
        enrollmentRequestsFile >> courseName;
    }
}
        }
    }

我似乎无法使用this->students = new std::string[studentName]将收集的studentName添加到数组中。收到错误消息must have integral or enumeration type

2 个答案:

答案 0 :(得分:1)

new SomeThing[size]用于声明数组。使用字符串作为大小是没有意义的。

假设students的大小仅限于maxEnrollment,您可以使用此功能:

if (this->enrollment < this->maxEnrollment) {
    this->students[this->enrollment++] = studentName;
    return true;
}
else {
    return false;

答案 1 :(得分:0)

为了完整起见,students的分配不是唯一的问题。鉴于您发布的代码也使用std::vector<Course>Course不遵循3的规则,在std::vector中使用它很可能会导致内存损坏,泄漏等。< / p>

鉴于您声明students必须保留指针,完整的修复方法是以这种方式编写Course

#include <string>
#include <algorithm>

struct Course {
    std::string name;
    int enrollment;
    int maxEnrollment;
    std::string* students; ///< array of student names
    Course(std::string courseName, int maxEnrollmentPermitted);
    bool enroll(std::string studentName);
    void print(std::ostream& output);
    Course(const Course& rhs);
    Course& operator =(const Course& rhs);
    ~Course();
};

Course::Course(const Course& rhs) : name(rhs.name),  
                                    enrollment(rhs.enrollment),  
                                    maxEnrollment(rhs.maxEnrollment),
                                    students(new std::string[rhs.maxEnrollment])
{
   for (int i = 0; i < maxEnrollment; ++i)
      students[i] = rhs.students[i];
}

Course& Course::operator= (const Course& rhs)
{
   Course temp(rhs);
   std::swap(temp.students, students);
   std::swap(temp.maxEnrollment, maxEnrollment);
   std::swap(temp.enrollment, enrollment);
   std::swap(temp.name, name);
   return *this;
}

Course::~Course() { delete [] students; }

Course::Course(std::string courseName, int maxEnrollmentPermitted) : 
               name(courseName), 
               enrollment(0),
               maxEnrollment(maxEnrollmentPermitted), 
               students(new std::string[maxEnrollmentPermitted])    
{}

为什么所有这些代码?好吧,在您在问题中发布的代码中,您使用的是std::vector<Course>。由于Course具有不正确的复制语义,因此无法在向量中安全地使用所写的Course类。因此,您所犯的错误可能与您所声明的代码(vector<Course>)有很大关系。

现在对Course的调整使得Course可以安全地用在向量中,因为现在已经实现了复制语义(复制构造函数,赋值运算符和析构函数)来处理动态分配的{ {1}}成员。

请注意,如果students只是students而不是std::vector<std::string>,则绝对不需要此代码。

更多阅读:

What is the rule of 3?

What is the copy / swap idiom?