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)’|
#include <iostream>
#include <string.h>
#include "car.h"
#include <cstdlib>
usingnamespace 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;
}
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).
#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
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
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
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 usingnamespace 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!