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
	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; }
	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;
9 Years
Discussion Span
Last Post by ~s.o.s~

Your overloaded + operator takes another car object's reference as an input, while the way you have used it in your program, it would require an overloaded operator which takes in an integer as input.

Your first two constructors are ambiguous since for a particular condition (when only cost is passed) both are eligible of being called.

Also, you should consider allocating memory to your name variable and using strcpy for copying them, rather than using a simple assignment operator which doesn't seem to cut ice in the case of C style strings.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.