Помогите с кодом либо перепишите( здесь все работает кроме названия производителя ), выдает 2 ошибки: 1) отсутствуют экземпляры конструктора "Commutator::Commutator",соответствующий списку аргументов
2)"Commutator::Commutator(int,int,int,std::string,float)": невозможно преобразовать аргумент 5 из "std::string" в "float"
#include
#include
const int N = 10;
class Commutator
{
public:
int portCounter;
int portSpeed;
int matrixSpeedCommunication;
float price;
std::string manufacturer;
Commutator()
{
};
Commutator(int portCounter_, int portSpeed_, int matrixSpeedCommunication_,
std::string manufacturer_, float price_)
{
portCounter = portCounter_;
portSpeed = portSpeed_;
matrixSpeedCommunication = matrixSpeedCommunication_;
price = price_;
manufacturer = manufacturer_;
}
};
int main()
{
std::string manufacturers[] = { "name1","name2","name3" };
srand(time(nullptr));
Commutator commutatorArray[N];
for (int i = 0; i < N; ++i)
{
int manufacturerIndex = rand() % 3;
commutatorArray[i] = *new Commutator(rand() % 20 + 5, rand() % 4000 + 2000, rand() % 100 + 20, 0,
manufacturers [manufacturerIndex]);
commutatorArray[i].price = commutatorArray[i].portCounter * 2.0 + commutatorArray[i].matrixSpeedCommunication * 1.5 + commutatorArray[i].portSpeed * 1.2;
std::cout << "[" << i << "] " << commutatorArray[i].manufacturer <<";" << commutatorArray[i].portCounter << "; " << commutatorArray[i].matrixSpeedCommunication
<< "; " << commutatorArray[i].portSpeed << "; " << commutatorArray[i].price << "\n";
}
Commutator bestPriceArray[N];
Commutator bestPortCounter[N];
std::copy(commutatorArray, commutatorArray + N, bestPriceArray);
std::copy(commutatorArray, commutatorArray + N, bestPortCounter);
//Sort by price
std::sort(bestPriceArray, bestPriceArray + N, [](const Commutator& a, const Commutator& b) -> bool
{
return a.price > b.price;
});
//Sort by portNumber
std::sort(bestPortCounter, bestPortCounter + N, [](const Commutator& a, const Commutator& b) -> bool
{
return a.portCounter < b.portCounter;
});
std::cout << "Best price:\n";
for (int i = 1; i < 6; ++i)
{
std::cout << "[" << i << "] " << bestPriceArray[N - i].portCounter << "; " << bestPriceArray[N - i].matrixSpeedCommunication << "; " << bestPriceArray[N - i].portSpeed << "; " << bestPriceArray[N - i].price << "\n";
}
std::cout << "Best port counter:\n";
for (int i = 1; i < 6; ++i)
{
std::cout << "[" << i << "] " << bestPortCounter[N - i].portCounter << "; " << bestPortCounter[N - i].matrixSpeedCommunication << "; " << bestPortCounter[N - i].portSpeed << "; " << bestPortCounter[N - i].price << "\n";
}
}
даю 30 баллов
Ответы на вопрос
Ответ:
Объяснение:
Ошибки, которые вы описали, связаны с тем, что в конструкторе Commutator после параметра manufacturer_ не указан параметр price_. Поэтому в вызове конструктора в цикле for указан неправильный набор параметров.
Вот как должен выглядеть конструктор:
Commutator(int portCounter_, int portSpeed_, int matrixSpeedCommunication_, std::string manufacturer_, float price_)
{
portCounter = portCounter_;
portSpeed = portSpeed_;
matrixSpeedCommunication = matrixSpeedCommunication_;
price = price_;
manufacturer = manufacturer_;
}
И вот как должен выглядеть вызов конструктора в цикле:
commutatorArray[i] = *new Commutator(rand() % 20 + 5, rand() % 4000 + 2000, rand() % 100 + 20, manufacturers[manufacturerIndex], 0);
После этого ошибки должны исчезнуть.