Default Constructors and Header Files.

Hello all!

I'm working on trying to figure out constructors and header files. Can ya'll help me out with this? I'm sure my code looks like a mess as I tried to piece together different solutions I've found. There's also an attempted copy constructor and operator function. Basically my problem is my source file says there is no default constructor for my class type. Any guidance would be greatly appreciated. Here's my header code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#ifndef _car
#define _car

#endif
class Car
{
private:
	std::string Make;
	std::string Model;
	int Year;

public:

	Car(std::string mk, std::string mdl, int yr):
		Make(mk),
		Model(mdl),
		Year(yr)
	{}

	std::string getMake(){return Make;}
	std::string getModel(){return Model;}
	int getYear(){return Year;}

	Car(const Car& anotherCar):
		Make(anotherCar.Make),
		Model(anotherCar.Model),
		Year(anotherCar.Year)
	{}

friend std::ostream& operator<<(std::ostream& s, const Car& c)
	{
		s << c.getYear << " " << c.getMake() << " " << c.getModel();
		return s;
	}

};
You are creating your own constructor which means the blank default constructor can no longer be used so if you have anything like Car c in your source code it will not work. You could always add a blank constructor for that if you wanted by doing Car() {} in the class. Otherwise we would have to see your source code to see what exactly the problem is.

Just on another note you can not call any non-const functions from a const object, so either change const Car& c to Car& c or access the members directly.
Thanks! So I have my default and copy constructors working, no I just need to output the class members using a stream operator. I have incorporated it into the header file but it keeps telling me it can't convert const Car to Car & on line 38. Also, I have no idea how to call this stream operator in the main function.

Here's my updated header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstring>
#ifndef _car
#define _car

#endif
class Car
{
private:
	std::string Make;
	std::string Model;
	int Year;

public:

	Car(std::string mk, std::string mdl, int yr):
		Make(mk),
		Model(mdl),
		Year(yr)
	{}

	std::string getMake(){return Make;}
	std::string getModel(){return Model;}
	int getYear(){return Year;}
	
	Car(const Car& anotherCar):
		Make(anotherCar.Make),
		Model(anotherCar.Model),
		Year(anotherCar.Year)
	{}

friend std::ostream& operator<<(std::ostream& s, const Car& c);
};

inline std::ostream& operator<<(std::ostream& s, const Car& c)
{
	return s << c.getYear()<< " " << c.getMake() << " " << c.getModel();
}


and the source file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "car.h"
#include <string>

std::ostream& operator<<(std::ostream& s, const Car& c);

int main()
{
	Car car1("Pontiac", "Trans Am", 1999);
	std::cout << car1.getYear() << " " << car1.getMake() << " " << car1.getModel();
	std::cout << "\n";


	Car car2(car1);
	std::cout << car2.getYear() << " " << car2.getMake() << " " << car2.getModel();



	return 0;
}	
Last edited on
You are getting that error because of what I mentioned.

Just on another note you can not call any non-const functions from a const object, so either change const Car& c to Car& c or access the members directly.


If you wanted to call it in your source code you would just do
cout << car1 << endl;
I see. Thank you. well no I'm getting all kinds of crazy errors starting with:
 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
on line 37.
Make sure you #include <string> everywhere that's needed.
Nevermind that extra operator declaration in my .cpp was holding things up. Thanks again!
> Just on another note you can not call any non-const functions from a const object,
> so either change const Car& c to Car& c or access the members directly.
... or make the methods const correct
std::string Car::getMake() const{return Make;}
Ah this would be more secure yes? I had tried that in then debugging process and it wasn't working because I had that declaration in my source file.
Topic archived. No new replies allowed.