gpt4 book ai didi

C++ STL 类集 - 编译器错误错误 C2664

转载 作者:行者123 更新时间:2023-11-27 22:29:21 24 4
gpt4 key购买 nike

这里的新手程序员正在努力完成他的作业。我正在尝试使用一组 STL 类,但编译器提示我的代码。

car.h
#include <string>
#include <iostream>
#include <time.h>
#include <set>

class Car
{
private:

std::string plateNumber;
std::string description;
std::string dateIn;
std::string timeIn;

public:
Car() {};
~Car() {};
Car(std::string plate, std::string desc)
{
plateNumber = plate;
description = desc;
};

void setPlateNumber(std::string plate) ;
std::string getPlateNumber() const;

void setDesc(std::string desc);

void setTimeDateIn() ;
std::string getTimeIn() const;
std::string getDateIn() const;
std::string getDesc() const;

friend std::ostream & operator<<(std::ostream & os, Car &c);

};
std::ostream & operator<<(std::ostream & os, Car& c)
{
os << "Plate Number: " << c.plateNumber << ", Date In: " << c.dateIn << ", " <<
`"Time in: " << c.timeIn << "Description: " << c.description << std::endl;
return os;
}
bool operator< ( const Car& lhs, const Car& rhs)
{
return ( lhs.getPlateNumber() < rhs.getPlateNumber() );
};


main.cpp

#include "stdafx.h"
#include <iostream>
#include <set>
#include <string>
#include "car.h"

void carEnters(std::set<Car> g);
void carLeaves(std::set<Car> g);
void displayContents(std::set<Car> g);

int main ()
{
char choice [80];

// initialize the sets and iterators
std::set<Car> garage;

do // Loop until user quits
{
std::cout <<
std::endl;
std::cout << "Menu:" << std::endl;
std::cout << "-----" << std::endl;
std::cout << "'1' to enter a new car, or " << std::endl;
std::cout << "'2' to exit the front car, or " << std::endl;
std::cout << "'3' to to list all the cars or." << std::endl;
std::cout << "'0' to close the garage: " << std::endl;
std::cin.getline( choice, 1, '\n');

switch ( choice[0] )
{
case '0' :

std::cout << std::endl << "Thanks for playing...\n";
break;

case '1' :

carEnters(garage);
break;

case '2' :

carLeaves(garage);

case '3' :

displayContents(garage);
break;

default:
std::cout << "I'm sorry, I didn't understand that.\n";
break;
}
} while ( choice[0] != '0' ); // Loop again if the user hasn't quit.

return 0;
}

void carEnters( std::set<Car> g)

{
// Car enters garage
std::cout << "Please enter the plate number:" << std::endl;
std::string plate;
std::cin >> plate;
std::cin.ignore();

std::set<Car>::iterator findPlate;
Car* lookup = new Car;
lookup->setPlateNumber(plate);

findPlate = g.find(*lookup);
if (findPlate != g.end()) // Add car to garage
{
Car *currentCar = new Car ;
// Set car parameters
std::cout << "Please type the entering car's description <Model, Color...
> : " << std::endl;
char desc[80];
std::cin.get(desc, 80 );
std::cin.ignore();
currentCar->setDesc(desc);
currentCar->setTimeDateIn();
currentCar->setPlateNumber(plate);

g.insert(currentCar);
}
else // Plate is already in garage set
{
std::cout << "Sorry, this car is already in the garage!" <<
std::endl;
}
}

void carLeaves( std::set<Car> g)
{
std::string plate;
std::cout << "Which plate is leaving?" << std::endl;
std::cin >> plate;
std::cin.ignore();

// Find car's plate number in the garage set
// for (findPlate=garageSet.begin(); findPlate !=garageSet.end(); findPlate++)
std::set<Car>::iterator findPlate;
Car lookup(plate,"");

findPlate = g.find(lookup);
if (findPlate != g.end())
{
// Display time in and then remove car from set of cars
std::cout << "Car out at " << (*findPlate).getDateIn() << ", " <<
(*findPlate).getTimeIn() << std::endl;
g.erase(findPlate);
}
else
{
std::cout << "Car was not found in set of Cars!" << std::endl;
}
}

// Car class function implementation
void Car::setPlateNumber(std::string p)
{
plateNumber = p;
}
std::string Car::getPlateNumber() const
{
return plateNumber;
}
void Car::setDesc(std::string d)
{
description = d;
}
void Car::setTimeDateIn()
{
char dat[9];
char tim[9];

_strdate_s(dat);
_strtime_s(tim);

dateIn=dat;
timeIn=tim;
}
std::string Car::getTimeIn() const
{
return timeIn;
}
std::string Car::getDateIn() const
{
return dateIn;
}
std::string Car::getDesc() const
{
return description;
}
// Display the car set
void displayContents(std::set <Car> garage)
{
// function displays current contents of the parking garage.
std::set <Car>::iterator carIndex;

std::cout << std::endl << "Here are all the cars parked: " << std::endl;
for (carIndex = garage.begin();
carIndex != garage.end();
++carIndex )
{
std::cout << " " << carIndex->getPlateNumber() << ", Date In: " <<
carIndex->getDateIn() << ", " << "Time In: " << carIndex->getTimeIn() << "Description:
" << carIndex->getDesc() << std::endl;
}
}

我从编译器得到的错误是这样的:xmemory(208):错误 C2664:“Car::Car(const Car &)”:无法将参数 1 从“Car *”转换为“const Car &” 原因:无法从“Car *”转换为“const Car” 没有构造函数可以采用源类型,或者构造函数重载决策不明确

我不确定我哪里出错了,请有人指出我的重载是如何不正确的?

谢谢

最佳答案

错误可能是 g.insert(currentCar) carEnters 中的行方法,如gstd::set<Car> , 不是 std::set<Car*> .传递对当前汽车的引用 ( *currentCar ) 或使车库包含指向汽车的指针。

另外,不妨传入g作为引用,形式为...

void carEnters(std::set<Car>& g) { }

void carLeaves(std::set<Car>& g) { }

否则正在复制集合,您可能得不到想要的结果。

如果您需要解释其中任何一个的原因,请添加评论。我以前做过一些 TAing。 :)

关于C++ STL 类集 - 编译器错误错误 C2664,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4785583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com