Hi, I can't get the following code to work. I have my converting constructor (second line) to change an int type to a car type but my compiler says there's something wrong with the overloaded + operator.
It says here in my book that if I want to do an implicit type conversion:
1) The class must have an accessible converting constructor
2) The overloaded function must be nonmember (with or without friendship)
Let's suppose that the code given in the class is correct (b/c my teacher went over it two days ago and it was supposed to be correct. Don't think she checked it on the compiler though).
#include <iostream>
class car
{
public:
car(int c = 0, char * name = "none") {}
car(int c) : cost(c) { name = 0; }
car(const car &);
~car() { delete [] name; }
car operator+ (const car &) { return *this; }
car * fA (const car &) { return this; }
car & operator= (const car &) { return *this; }
private:
char * name;
int cost;
};
car::car(const car & obj) : cost(obj.cost)
{
this->name = new char[strlen(obj.name) + 1];
strcpy( this->name, obj.name );
}
int main(void)
{
car ford, vw;
int a = 0;
vw = ford + a;
return 0;
}