zm455 zm455 - 2 months ago 10
C++ Question

c++: Include Headers files into source files

When I try to build and run my project (called Newboston_tuto) codeBlocks displays this message in the console:

||=== Build: Debug in Newboston_tuto (compiler: GNU GCC Compiler) ===|
C:\Users\Admin\Documents\CodeBlocksProjects\Newboston_tuto\main.cpp|2|fatal error: ../Headers/Car.h: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


I just would like to display the message "hello world" when I execute this code. Thank you for the help.

This is the very simple arborescence of my c++ project.

Sources/
main.cpp
car.cpp
Headers/
car.h


Here is the content of this 3 simple files.

main.cpp:

#include <iostream>
#include "../Headers/Car.h"

using namespace std;

int main()
{
Car myCar;
return 0;
}


Car.cpp:

#include "Car.h"
#include <iostream>

using namespace std;

Car::Car()
{
//ctor
cout << "hello world";
}


Car.h:

#ifndef CAR_H
#define CAR_H


class Car
{
public:
Car();
virtual ~Car();

protected:

private:
};

#endif // CAR_H

Answer

The ../ takes it a folder above the current directory. If your Headers folder is in the project's directory, you should remove the ../!

I have created a project like this before! :)

Comments