簡體   English   中英

C ++類的問題

[英]Trouble with C++ Class

我是一所獨立寄宿學校的老師,我正在嘗試用C ++編寫一個程序,該程序會隨機將學生安排在餐廳的桌子旁,這樣他們每周都會與不同的學生和不同的工作人員坐在一起。 理想情況下,在一定時期內,他們不會兩次坐在同一張桌子上,並且盡可能多的不同學生。 我用Python創建了這個程序,它運行得很好(非常好)。 出於各種原因,我試圖將其移植到C ++(我根本不熟悉),所以我可以把它交給寄宿人員。 學生和教職員工(以及表格容量)都是從文本文件中讀取的。 我創建了兩個自定義類,一個用於學生,一個用於表,用於處理數據。 以下是兩個類頭文件:

Table.h

#pragma once
#include <iostream>
#include "Student.h"
#include <vector>

using namespace std;

class Table
{
    // Private class variables
    string staff;
    int numSeats;
    vector<Student> seating;
public:
    Table(); // Default constructor
    Table(string s, int n);
    Table(const Table& that) : staff(that.staff), numSeats(that.numSeats)
    {
    }
    // Copy Constructor
    Table& operator=(const Table& that)
    {
        staff = that.staff;
        numSeats = that.numSeats;
        return *this;
    }
    int getNumSeats();
    string getStaffName();
    void addStudent(Student student);
    void removeStudent(Student student);
    void clearStudents();
    vector<Student> getTableSeating();
    int getRemainingSeats();
    ~Table(void);
};

這是學生班級文件:

#pragma once
#include <iostream>
#include <vector>

using namespace std;

class Student
{
    string name;
    string country;
    vector<int> tablesSatAt;
public:
    Student(string n, string c);
    Student();
    Student(const Student& that) : name(that.name), country(that.country)
    {
    }
    Student& operator=(const Student& that)
    {
        name = that.name;
        country = that.country;
        return *this;
    }
    string getName();
    string getCountry();
    void addTable(int tableNumber);
    void removeTable(int tableNumber);
    bool satAtTable(int tableNumber);
    friend bool operator==(Student s1, Student s2);
    friend bool operator!=(Student s1, Student s2);
    ~Student(void);
};

bool operator==(Student s1, Student s2);
bool operator!=(Student s1, Student s2);

這是執行繁重工作的遞歸函數:

bool seatRecursive(vector<Student> &tempStudents, vector<Table> &tempTables)
{
    if (tempStudents.size() == 0) return true; //base case

    Student nextStudent = randomSelect(tempStudents);
    for (vector<int>::size_type i=0; i<tempTables.size(); i++)
    {
        if (tempTables[i].getRemainingSeats() > 0 && !nextStudent.satAtTable(i))
        {
            addStudentToTable(nextStudent, tempTables, i);
            if (seatRecursive(tempStudents, tempTables)) return true;
            else 
            {
                removeStudentFromTable(nextStudent, tempTables, i);
                tempStudents.push_back(nextStudent);
            }
        }
    }
    return false;
}

大部分都有效。 當我運行該程序時,我得到一個有10周座位的文本文件,但所有桌面座位都是相同的。 即如果我是一名特定的工作人員,我有同樣的孩子在我的餐桌上待了整整10個星期。 我有一個int的向量,應該存儲學生隨時間坐着的表號。 在調試時,我注意到那些表號不存儲在該向量中,它總是空的。 我的問題是,我無法弄清楚為什么會這樣。 是因為我通過引用傳遞向量嗎? 是否與指針有關,即使我沒有明確聲明指針?

非常感謝任何建議,如有必要,我可以粘貼其余的代碼。

布賴恩

為什么這么難?

只需將所有學生放在一個向量中,然后使用STL random_shuffle算法,最后將生成的學生矢量線性地放在所有可用的表中。

你甚至不需要為學生或桌子定制課程

我看到StudentTable類的復制構造函數(和賦值構造函數)都忘記了復制包含的STL向量( vector<Student> seating vector<int> tablesSatAtvector<int> tablesSatAt )。 由於重載這些構造函數,您應該通過vector復制構造函數復制它們,因為它不是隱式完成的。

在不復制它們的情況下,每當StudentTable圍繞向量移動(或分配給臨時)時,內部向量將被丟棄以用於新對象。

正如Useless在這種情況下的評論所指出的,你不需要聲明它們,因為三條規則不適用:你不需要析構函數,所以你可能不需要兩個拷貝構造函數。

注意:您在注釋中稱為復制構造函數的是復制賦值構造函數。 之前的一個是真正的拷貝構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM