Hello, have problem with class and function calls

Hello,
I am trying to make a code that reads car model from user and saves in class files and also print out after that.
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
  #include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class CVehicle {
private:
	string m_make;
	int m_model;
public:
	CVehicle::CVehicle(int s) {
		SetModel(s);
		SetMake(y);
	}
	void SetModel(int s) {
		s = m_model;
	}
	void SetMake(string y) {
		y = m_make;
	}
	string ReadVehicle() {
		cout << "enter model";
		cin >> m_make;
		return m_make;
	}
}

int main() // error saying main should have int type instead of 'CVehicle' 
	{
		CVehicle CarThree();/* i am trying to read car model form user and save in carthree and than print out carthree */
		CarThree().ReadVehicle();
		cout << CarThree << endl;
		return 0;
}


thank you.
You forgot a semi-colon at the end of your class definition.
1
2
3
4
class Foo {


}; // semi-colon! 


CVehicle CarThree();
Due to pesky C++ syntax rules, this is interpreted as declaring a function that returns a CVehicle object.

To actually make an object of type CVehicle in main, you must drop the parentheses for 0-arg constructors.
 
CVehicle CarThree; // CarThree is an object of type CVehicle 

Compare to built-in and standard-library types:
1
2
3
4
int a;
string b; // calls 0-arg string ctor
string c("hello"); // calls 1-arg string ctor
double d;
Last edited on
Thank you Ganado,
I dropped a parentheses,
than it says no default constructor exist for class "CVehicle"
also,
 
cout << CarThree << endl; //no operator "<<" matches these operands  

thanks
Last edited on
than it says no default constructor exist for class "CVehicle"
Oh, I didn't even notice that you didn't have one.

You only defined a 1-arg constructor. When another constructor besides the default constructor is defined, a default (0-arg) constructor is not created.
Either call your 1-arg constructor, or make a default constructor yourself
1
2
3
4
5
CVehicle()
{
    SetModel(0);
    SetMake("0");
}


1
2
3
4
	CVehicle::CVehicle(int s) {
		SetModel(s);
		SetMake(y);
	}
What is y? Where do you define it?

1
2
3
	void SetMake(string y) {
		y = m_make;
	}
Assignment is always right to left. You're only assigning your local variable, y, here.
You meant to do
m_make = y;

Similar problem here:
1
2
3
	void SetModel(int s) {
		s = m_model;
	}
Last edited on
Hello, Thank you again,

Here is my new 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
#include "stdafx.h"
#include <iostream>
#include <string>


using namespace std;

class CVehicle {
private:
	string m_make;
	int m_model;
public:
	CVehicle() {
		SetModel(0);
		SetMake("0");
	}
	void SetModel(int s) {
		m_model =s ;
	}
	void SetMake(string y) {
		m_make =y;
	}
	string ReadVehicle() {
		cout << "enter model";
		cin >> m_make;
		return m_make;
	}
};

int main()
	{
		CVehicle CarThree;
		CarThree.ReadVehicle(); 
		cout << CarThree << endl; /*here it says No operator "<<"matches these operands but i used using namespace std; ...*/
		return 0;
}


So I am trying to save a car make from a user and print it out..
this code would work? if I set CarThree.ReadVehicle and CarFIve.ReadVehicle?

thank you.
Topic archived. No new replies allowed.