Quick basic classes error

I'm understanding classes alot better than my last post but I can't seem to find where this error is originating from: /home/rej3kt/Desktop/car_class/car project/main.cpp|36|error: request for member ‘getModel’ in ‘c1’, which is of non-class type ‘Car(char*, char*, int, int, float)’|

My main looks like this:

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
39
40
41
42
43

#include <iostream>
#include <string.h>
#include "car.h"
#include <cstdlib>
using namespace std;

int main()
{
    char car_model[50];
    char car_colour[50];
    int car_age;
    int car_value;
    float car_mpg;

    Car c1(char model[], char colour[], int age, int value, float mpg);

    cout << "Enter your car model: " << endl;
    cin >> car_model;

    cout << "Enter your car colour: " << endl;
    cin >> car_colour;

    cout << "Enter the age of your car: " << endl;
    cin >> car_age;

    cout << "Enter the value of your car in GBP: " << endl;
    cin >> car_value;

    cout << "Enter how many miles per gallon your car does: " << endl;
    cin >> car_mpg;


    cout << "\n\n\n\n\n\n\n\n\n" ;


    cout << "Your car model is: " << c1.getModel() <<endl;
    cout << "Your car colour is: " <<endl;
    cout << "Your car is " " years old" <<endl;
    cout << "Your car is worth £" <<endl;
    cout << "Your car does " "mpg" <<endl;
    return 0;
}


and my car.cpp file looks like this:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "car.h"
#include <string.h>

Car::Car()
{
}

Car::Car(char model[], char colour[], int age, int value, float mpg)
{
    strcpy(car_model, model);
    strcpy(car_colour, colour);
    car_age = age;
    car_value = value;
    car_mpg = mpg;


}

void Car::setModel(char model[])
{
	strcpy(car_model, model);
}
char* Car::getModel()
{
	return car_model;
}



void Car::setColour(char colour[])
{
    strcpy(car_colour, colour);
}
char* Car::getColour()
{
    return car_colour;
}


void Car::setAge(int age)
{
    car_age = age;
}
int Car::getAge()
{
    return car_age;
}


void Car::setValue(int value)
{
    car_value = value;
}
int Car::getValue()
{
    return car_value;
}

void Car::setMPG(float mpg)
{
    car_mpg = mpg;
}
float Car::getMPG()
{
    return car_mpg;
}

Give me a shout if you need my header file, thanks for any help guys!
Last edited on
I suppose line 16 in main.cpp is understood as a function declaration. Surely you meant Car c1(car_model, car_colour, car_age, car_value, car_mpg); But that wont work since you ser those variables after setting 'c1'. Move line 16 to line 33 (or use references).
I don't think you're grasping how classes work. Let me give you an example:

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
#include <iostream>

class Foo {
public:
    Foo()
    :bar(0) { }

    Foo(int n)
    :bar(n) { }

    int getBar() const { return bar; }
    void setBar(int n) { bar = n; }

private:
    int bar;
};

int main()
{
    Foo a;  // constructs a variable named a, of type Foo, by calling the default constructor
    Foo b(4);  // notice you just pass the parameters; you don't repeat their names or types
    int bar = 3;  // not a's or b's bar; won't be used or printed from now on

    std::cout << "a's bar: " << a.getBar() << std::endl;  // prints the default value, 0
    std::cout << "b's bar: " << b.getBar() << std::endl;  // prints 4

    a.setBar(8);
    b.setBar(-2);

    std::cout << "a's bar: " << a.getBar() << std::endl;  // prints 8
    std::cout << "b's bar: " << b.getBar() << std::endl;  // prints -2

    return 0;
}


EDIT: added a variable named bar to main() to make it clearer
Last edited on
lolwut, don't quite get your example Fillipe, I'm pretty sure my class is constructed properly, I'm just having problems saving information to the Car classe, I've defined its parameters (colour, age, value etc) and I've written functions to write to those but I'm having problems with my main trying to let people write to those variables
When you say this:

1
2
    cout << "Enter your car model: " << endl;
    cin >> car_model;

You're just modifying a variable of type char[] named car_model, not c1's car_model. I changed my example a bit to make it clearer.
Last edited on
but in my car.cpp file I'm copying the string from car_model to model in the Car class?
1
2
3
4
5
6
7
8
9
10
Car::Car(char model[], char colour[], int age, int value, float mpg)
{
    strcpy(car_model, model);
    strcpy(car_colour, colour);
    car_age = age;
    car_value = value;
    car_mpg = mpg;


}
But you're calling the constructor before you assign any value to those variable. The constructor will just copy whatever is in the variables at that point, it won't permanently bind the variables.
So what should I do?
I've moved this to line 33 so the variables have been defined before "c1" has been created
Car c1(char model[], char colour[], int age, int value, float mpg);
That looks like a function declaration. This is what you should say:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    char car_model[50];
    char car_colour[50];
    int car_age;
    int car_value;
    float car_mpg;

    // fill in the variables

    Car c1(car_model, car_colour, car_age, car_value, car_mpg);

    // go on; c1 was constructed with the values provided by the user
}
That's fantastic fillipe! Thanks mate just changed the c1 names to car_model etc and now it's working.

Trying to work back through the code logically does it go like this? :

car model entered gets saved to : "car_model", that goes to car.cpp and gets copied to "model" which is a property of my class, it is a private variable along with age, colour, value and mpg.

The function in car.cpp is strcpy which copies the string from car_model to model in the class property, the second part of this defines a function called getModel which is a char array and returns a result to the variable car_model. God I'm confused
1
2
3
4
5
6
7
8
void Car::setModel(char model[])
{
	strcpy(car_model, model);
}
char* Car::getModel()
{
	return car_model;
}
Can you post your class declaration?
This is my 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
39
40
41
42
43
44
#ifndef CAR_H_INCLUDED
#define CAR_H_INCLUDED



#endif // CAR_H_INCLUDED

class Car
{
    public:

    Car();

    Car(char model[], char colour[], int age, int value, float mpg);

    void setModel(char model[]);
    char* getModel();

    void setColour(char colour[]);
    char* getColour();

    void setAge(int age);
    int getAge();

    void setValue(int value);
    int getValue();

    void setMPG(float mpg);
    float getMPG();






    private:
    char car_model[50];
    char car_colour[50];
    int car_age;
    int car_value;
    float car_mpg;


};
Ok. The class doesn't have any member named model, like you said. The member is called car_model. model is just a temporay variable that only exists when the constructor (or setModel()) is being executed.

On a separate note, why don't you use std::string instead of C-style strings?
What are std::strings? We've only learnt the way i'm doing it here, I've had to start using "using namespace std" aswel as iostream.h doesn't work in my code::blocks.

Why does the class need temporary members/variables that only exsist when the constructor or set model is being executed?
In my opinion, if you're learning C++, you should begin by using the standard library and only later get into raw arrays. Notice that standard C++ headers have no trailing ".h". That's for pre-standard headers. Here's an example of how to use std::string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>

int main()
{
    std::string a = "A string";
    std::string b = "A different string";

    std::cout << a << std::endl << b << std::endl;

    a = b;
    b += " that's been appended";

    std::cout << a << std::endl << b << std::endl;

    return 0;
}

I avoid using namespace std; because it can cause name clashes.

About class members, you need temporary variables inside a function because otherwise you couldn't refer to the parameters that were passed to it. It's just a name for the value that was passed and, in your case, that you want to store in the member variables.
Thank you for that I appreciate all the help you've given me.

Also: No offence but I don't understand at all what difference using .h and using namespace std; does but my programs work, at college we get taught it with a .h anyways I just can't use it at home so I've changed it :). Thanks again!
You're welcome. The difference is that headers such as iostream.h are old and outdated.
Topic archived. No new replies allowed.