I'm trying to learn how to properly create separate classes in my c .
Every tutorial on classes have the custom class in the same file like this.
I found this question on combining different files but it doesn't deal with classes.
I've created 3 simple files to learn creating classes in different files.
car.h
#ifndef CAR_H
#define CAR_H
class Car
{
public:
Car();
Car(double wieght);
double get_wieght();
void set_wieght(double wieght);
~Car();
private:
double wieght;
};
#ENDIF //CAR_H
car.cpp
#include "car.h"
class Car
{
public:
Car(){
wieght = 10.0;
}
Car(double wieght){
this->wieght = wieght;
}
double get_wieght(){
return wieght;
}
void set_wieght(double wieght){
this->wieght = wieght;
}
~Car(){
}
private:
double wieght;
};
main.cpp
//Program to learn how to use multiple files
#include <iostream>
#include "car.h"
using namespace std;
int main(){
Car c;
c.set_wieght(100.0);
cout << "Car wiegth: " << c.get_wieght() << endl;
}
My output error when I try to compile it with g Main_Multiple_Files.cpp -o main.exe
:
Undefined symbols for architecture x86_64:
"Car::get_wieght()", referenced from:
_main in Main_Multiple_Files-fbf3f8.o
"Car::set_wieght(double)", referenced from:
_main in Main_Multiple_Files-fbf3f8.o
"Car::Car()", referenced from:
_main in Main_Multiple_Files-fbf3f8.o
"Car::~Car()", referenced from:
_main in Main_Multiple_Files-fbf3f8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Trying to link the files with g Main_Multiple_Files.cpp car.cpp -o main.exe
:
In file included from Main_Multiple_Files.cpp:4:
./car.h:41:2: error: invalid preprocessing directive
#ENDIF //CAR_H
^
./car.h:1:2: error: unterminated conditional directive
#ifndef CAR_H
^
2 errors generated.
In file included from car.cpp:1:
./car.h:41:2: error: invalid preprocessing directive
#ENDIF //CAR_H
^
./car.h:1:2: error: unterminated conditional directive
#ifndef CAR_H
^
car.cpp:3:7: error: redefinition of 'Car'
class Car
^
./car.h:4:7: note: previous definition is here
class Car
^
3 errors generated.
What do I need to do to get this class working? Feel free to obliterate my code I've been trying to do this for a while now.
CodePudding user response:
You're declaring two separate Car
classes. The class Car { ... }
part should only occur in the header. Then your .cpp
file should contain the function bodies, using the fully qualified name of the function as the prototype. So, for the first constructor, the declaration in the header should read
class Car {
Car();
...
}
whereas the version in .cpp
should read
// Note: *Not* inside of 'class Car {...}'
Car::Car(){
wieght = 10.0;
}
Then, similarly, for member functions that have bodies,
void Car::set_weight(double weight) {
this->weight = weight;
}
CodePudding user response:
If you really want the bodies separate, your Car.cpp should look like this:
#include "car.h"
Car::Car(){
wieght = 10.0;
}
Car::Car(double wieght){
this->wieght = wieght;
}
double Car::get_wieght(){
return wieght;
}
void Car::set_wieght(double wieght){
this->wieght = wieght;
}
~Car::Car(){
}
(Side note: you have misspelled "weight".)