开发者

C++ STL set of classes - compiler error error C2664

Newbie programmer here trying to work out his homework. I'm trying to use a STL set of classes, but the compiler complains about my code.

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(cu开发者_StackOverflowrrentCar);
}
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;
}
}

The error I get from the compiler is this: xmemory(208): error C2664: 'Car::Car(const Car &)' : cannot convert parameter 1 from 'Car *' to 'const Car &' Reason: cannot convert from 'Car *' to 'const Car' No constructor could take the source type, or constructor overload resolution was ambiguous

I'm not sure where I'm going wrong, would some please point out how my overload is incorrect?

Thanks


The error is likely the g.insert(currentCar) line in the carEnters method, as g is a std::set<Car>, not a std::set<Car*>. Either pass in a reference to the current car (*currentCar) or make the garage contain pointers to cars.

In addition, you may wish to pass in g as a reference, in the form of...

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

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

Otherwise the set is being copied and you might not get the results you want.

If you need explanation as to the why for any of these, add a comment. I used to do some TAing back in the day. :)


I believe @James is on the right track, but passing *CurrentCar isn't really the right answer (at least IMO). Instead, you should back up a bit:

Car *currentCar = new Car ;             

Perhaps you have prior experience with Java (or something similar) where this is a routine, normal type of code to write. In C++, however, using new directly is (or at least should be) fairly unusual. What you almost certainly want instead is:

Car currentCar;

and then you'll fill in the fields like:

currentCar.whatever = x;

Then, when you put your currentCar into the std::set (or whatever) you won't have to dereference anything, because you'll be starting with a Car object, which is what's expected. As an aside, I'd note that when you look up the car, you're also creating a Car object dynamically -- but you never seem to delete either one, so you're code is leaking memory.

Edit: I should add that there are alternatives that may be preferable. Right now, you're basically treating a Car as "dumb data", with outside code to operate on that data. If you want your code to be "object oriented", it would almost certainly be better to move the code for reading a Car's data into the class itself, so outside code would just invoke that member function.

Another possibility would be to make a Car an immutable object. Instead of creating an unitialized car, and then setting the appropriate values in that object, I'd pass the correct values to Car's constructor, and eliminate the member functions you currently have for changing those values. At least for your purposes, it doesn't appear that you need to actually change a car's plate number -- it should apparently only ever have one plate number, in which case it would be better for your code to reflect (and enforce) that directly.


Your problem is that your set takes elements of type Car but you are inserting elements of type Car*:

void carEnters( std::set<Car> g)
{
...
Car *currentCar = new Car;
...
g.insert(currentCar);

In this case, currentCar is a pointer to a Car and g.insert expects a Car. There are multiple ways of fixing this - you can change your set to use Car* although your overloaded operator< will no longer work (you'll have to create a functor that is passed to the set and takes two Car*s). You can change currentCar to be of type Car. This results in a bunch of copying however. Or you can ditch currentCar entirely and make a constructor that will set all the variables you need set:

Car(const std::string &plate, const std::string &desc)    
{
    plateNumber = plate;
    description = desc;
    setTimeDateIn();
};

then you can just do this:

g.insert(Car(desc, plate));

Which is actually preferable to what you are doing now, as someone might forget to call setTimeDateIn. It makes more sense for that to be called when the Car is constructed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜