我正在尝试制作一个有关地区的程序,我想存储该名称。我得到:“没有匹配的构造函数来初始化‘ElectionDistrict[]’”

问题描述 投票:0回答:1
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <limits>

const int MAX_PARTIES = 10; /* Maximum number of parties*/

class ElectionDistrict {
private:
    char* name;
    int votersCount;
    int partiesCount;
    int* partyVotes;
    std::string* partyNames;

public:
    ElectionDistrict(const char* name, int voters, int parties) /* Constructor*/
            : votersCount(voters), partiesCount(parties) {
        this->name = new char[strlen(name) + 1];
        strcpy(this->name, name);

        partyVotes = new int[partiesCount];
        partyNames = new std::string[partiesCount];
    }

    ~ElectionDistrict() {  /*Destructor*/
        delete[] name;
        delete[] partyVotes;
        delete[] partyNames;
    }

    const char* getName() const { return name; }
    int getVotersCount() const { return votersCount; }
    int getPartiesCount() const { return partiesCount; }
    const int* getPartyVotes() const { return partyVotes; }
    const std::string* getPartyNames() const { return partyNames; }

    void setPartyVotes(int partyIndex, int votes) {
        partyVotes[partyIndex] = votes;
    }

    void setPartyName(int partyIndex, const std::string& partyName) {
        partyNames[partyIndex] = partyName;
    }

};

    // Function to compare ElectionDistrict objects based on party votes
bool compareByPartyVotes(const ElectionDistrict& a, const ElectionDistrict& b) {
    return a.getPartyVotes()[0] > b.getPartyVotes()[0];
}

int main() {
    std::ofstream outFile("districts.txt");
    ElectionDistrict* districts;

    int numDistricts;

    std::cout << "Enter the number of districts: ";
    std::cin >> numDistricts;

    districts = new ElectionDistrict[numDistricts]; /* right here is the problem*/

    for (int i = 0; i < numDistricts; ++i) {
        char name[100];
        int voters, numParties;

        std::cout << "Enter the name of district " << i + 1 << ": ";
        std::cin >> name;

        std::cout << "Enter the number of voters in " << name << ": ";
        std::cin >> voters;

        std::cout << "Enter the number of parties in " << name << ": ";
        std::cin >> numParties;

        districts[i] = ElectionDistrict(name, voters, numParties);

        for (int j = 0; j < numParties; ++j) {
            std::string partyName;
            int votes;

            std::cout << "Enter the name of party " << j + 1 << " in " << name << ": ";
            std::cin >> partyName;

            std::cout << "Enter the number of votes for " << partyName << " in " << name << ": ";
            std::cin >> votes;

            districts[i].setPartyVotes(j, votes);
            districts[i].setPartyName(j, partyName);
        }
    }
    /*writing the information about each election district and its associated parties to output file (districts.txt)*/
    for (int i = 0; i < numDistricts; ++i) { 
        outFile << districts[i].getName() << " " << districts[i].getVotersCount() << " " << districts[i].getPartiesCount();
        for (int j = 0; j < districts[i].getPartiesCount(); ++j) {
            outFile << " " << districts[i].getPartyVotes()[j] << " " << districts[i].getPartyNames()[j];
        }
        outFile << std::endl;
    }
    outFile.close();

      // Rest of the code remains the same

    delete[] districts; // Release memory

    return 0;
}

此代码正在创建ElectionDistrict,并尝试模仿一个检查这些地区政党结果的程序,然后它必须打印出各地区的获胜者,然后将其保存到一个单独的文件中。没有获奖者的地区会被打印出来,并且还要将有关地区的信息保存在文件中。

另一方面,使用向量会有所帮助,但我想让它与动态数组一起使用。

c++ arrays oop clion dynamic-arrays
1个回答
0
投票

问题是,由于您为类提供了参数化构造函数,默认构造函数会根据 C++ 标准规则被编译器隐式删除

要解决此问题,您可以显式添加为您的类提供一个 default ctor,如下所示:

class ElectionDistrict {
    //other code as before 

    //EXPLICITLY PROVIDE A DEFAULT CTOR
    ElectionDistrict() = default;
    
   //other code as before
};
© www.soinside.com 2019 - 2024. All rights reserved.