I want to write a simple program to check class and inheritance in Code Blocks but i don't know how to write. I have make new project and add new class named "Car".

This is main class.

#include <iostream>
#include "Car.h"
using namespace std;

int main()
{

    Car c;
    return 0;
}

and this is include\Car.h file.

#ifndef CAR_H
#define CAR_H

class Car
{
    public:
        Car();

};

#endif // CAR_H

and this is Car.cpp file code.

#include <iostream>
#include "Car.h"
using namespace std;

Car::Car()
{
    cout << "Car program." <<endl;
}

But when i compile and run it, it gives me errors. Please tell me how to resolve this and how to use code blocks.

But when i compile and run it, it gives me errors.

What are the errors? Seriously, would you go to an auto mechanic and say "something's wrong" and expect them to find the problem you're thinking of?

C:\Users\Raheel\Desktop\test c++\prog\src\Car.cpp|2|error: Car.h: No such file or directory|
C:\Users\Raheel\Desktop\test c++\prog\src\Car.cpp|5|error: ISO C++ forbids declaration of 'Car' with no type|
C:\Users\Raheel\Desktop\test c++\prog\src\Car.cpp||In function 'int Car()':|
C:\Users\Raheel\Desktop\test c++\prog\src\Car.cpp|8|warning: no return statement in function returning non-void|
||=== Build finished: 2 errors, 1 warnings ===|

Is the include directory where Car.h lives in your search paths? What's the directory structure of your project?

C:\Users\Raheel\Desktop\test c++\prog\src\Car.cpp|2|error: Car.h: No such file or directory|

For some reason it doesn't include your files correctly. What is your directory tree? Are the includes in your search path?

Edited 4 Years Ago by Lucaci Andrew

You need to provide the absolute path to tell the compiler where to find the header file

 #include "C:\Users\Raheel\Desktop\test c++\prog\header\"

Solution

You have to configure the project build and add the directory where the header file is located. The steps are as below:-

Project > Build options > Search Directories tab
Choose the Policy dropdown list with the option: Prepend target options to project options”
Click the button ‘Add’ to search and add the directory (e.g. ‘include’ directory)
Another popup with the question “Keep this as a relative path?” and click ‘Yes’ to add the directory.

If you put car.h in the same folder as car.cpp the compiler should not have a problem finding it. If you put car.h in some otherr folder you will have to tell the compiler where it is, as stated by other members who have posted here. For small projects just put everything in the same folder unless you are instructed to do otherwise by your teacher.

This question has already been answered. Start a new discussion instead.