Linked Questions
109 questions linked to/from Why have header files and .cpp files?
54 votes
4 answers
40k views
What is the point of header files in C? [duplicate]
Possible Duplicates: [C] Header per source file. In C++ why have header files and cpp files? C++ - What should go into an .h file? Is the only reason header files exist in C so a developer can quickly ...
21 votes
4 answers
28k views
Confused about the actual purpose of header files in C++ [duplicate]
Possible Duplicate: In C++ why have header files and cpp files? I don't quite get C++ header files, for two conflicting reasons: I thought the purpose of header files was in general to separate ...
12 votes
3 answers
4k views
Why do we declare and define functions separately in C++? [duplicate]
I have only just started learning C++, and I see that functions are usually declared and defined separately, for example: // Declaration void sayhi(std::string name); // Definition void sayhi(std::...
3 votes
1 answer
9k views
Where/how to put functions in a header vs C++ source file [duplicate]
Someone asked a question similar to mine here: Function declaration inside or outside the class? Why do you just declare a function inside a .h file but not the definition, and then write the whole ...
4 votes
3 answers
6k views
Are header files necessary? [duplicate]
Possible Duplicate: In C++ why have header files and cpp files? Coming from a C# background, I find header files really annoying. Are they necessary even with C++11?
2 votes
3 answers
4k views
Class implementation in a header file == bad style? [duplicate]
I was wondering if it is advisable to write a the whole class c++ in a header file ? and use include to include the class, in a similar way that java does its oop programming. Is it a bad style ? can ...
0 votes
3 answers
2k views
Why create abc.h and abc.cpp file in a c++ program? [duplicate]
Possible Duplicate: In C++ why have header files and cpp files? I was reading some KDE tutorials for creating basic plasma widgets and other QT stuff. One thing that I noticed is almost all the ...
2 votes
1 answer
4k views
Why #include<.hpp> in .cpp, not <.cpp> in .hpp? [duplicate]
I wonder why recommended way is to #include<example.hpp> in example.cpp; I don't understand, how preprocessor (which seems to be quite simple program) knows that definition of methods are in ...
0 votes
3 answers
578 views
what to put in cpp and what to h files [duplicate]
Possible Duplicate: In C++ why have header files and cpp files? C++ - What should go into an .h file? All the functions/methods which are usually defined in .cpp file can be defined inline in .h ...
0 votes
2 answers
1k views
Creating a Derived Class in C++ [duplicate]
So im trying to make a Die class and a LoadedDie class where the Die class is the base and the LoadedDie class is derived. The only difference between the 2 classes is that the roll function in the ...
1 vote
2 answers
484 views
C Head files and Source files [duplicate]
I am a student programmer learning my first language with the book "Accelerated C++." I am at at a point where the author is explaining header files and source files containing functions. In the ...
0 votes
1 answer
415 views
Include declaration but not definition in main file C++ [duplicate]
When using multiple files in C++, for example: main.cpp, definition.cpp, declaration.h // declaration.h int Myfunc(); //definition.cpp #include "declaration.h" int MyFunc() {return 5;}; //...
-1 votes
3 answers
237 views
C++ header file [duplicate]
Possible Duplicate: In C++ why have header files and cpp files? Why in C++ is there a .h and .cpp and not only one file like c# and Java ?
0 votes
1 answer
173 views
Do c++ compilers skip parts of files when compiling? [duplicate]
In C++, I've heard that you need to separate the implementation from the interface in order to reduce compile times. In this question, the answerer basically says that if you change the header then ...
0 votes
1 answer
253 views
What is the point of .cpp files if I can use header files with the same use? [duplicate]
I may be thinking/using header files the wrong way, but currently I'm using 1 .cpp file and the rest are all in header files. I have things like a screen which is an array of characters in a header ...
0 votes
1 answer
101 views
Why I have to use header file in separate file except main file? [duplicate]
The header's file content is put where .h file is used. Why use add.h in add.cpp? In case without using header file add.cpp // There is not declaration int add(int x, int y); int add(int x, int y) { ...
-1 votes
1 answer
110 views
Cpp linking/header, why is it done this way? [duplicate]
I'm new to c++ and I have 'actually' learned a lot these past days. I just learned how to link multiple files today or what I think is called, using "headers". My question is related to headers and ...
0 votes
1 answer
122 views
Why use a C++ header file? [duplicate]
I understand the header file contains all the prototypes of every function contained in the implementation file; but what is the real purpose? Is it because when another program calls on the class, it ...
1 vote
1 answer
89 views
Why make new header files when you can just use C files? [duplicate]
I was working with sockets , and i noted that a lot of the code i wrote repeated in different programs , so i wrote functions for most of the stuff that i did and saved them in a ".c" file without a ...
0 votes
3 answers
110 views
Resolve circular dependancy when using a Singleton in C++ [duplicate]
So I have a problem when using a singleton Game class and the Story class. Game.hpp #ifndef GAME_HPP #define GAME_HPP #include <memory> class Phase; // Forward declaration class Game { ...
1 vote
0 answers
98 views
Are header files only for classes? [duplicate]
I'm learning C ++ and I have a question about header files. Are they only for classes? If I have some functions and want to use them in several files, should I write the declaration in the h file and ...
2 votes
0 answers
82 views
Why Should I User Header files in c++? [duplicate]
my first project in c++ (I use g++ on centos) from Python and c#, I use to split my program into few files, each class in different file. The most common reason i find, why use .h file, is ...
-4 votes
1 answer
79 views
Why use .h files when you can just put all in the .cpp fil? [duplicate]
I been searching a lot lately about header files and stuff and i always see the same things, but i dont get something, why use the .h header file when you can just throw the code on the .cpp file? ...
0 votes
0 answers
73 views
Purpose of header files in cpp [duplicate]
Why is there a need to include the header file of a class into the implementation file of the same class?? Does it serve a purpose?? Since i thought it is needed only where we need to use that class.
0 votes
1 answer
65 views
why create .h and .cpp headers [duplicate]
I came across this implementation of some code which requires us to create a header and then #include it in our source code. So my question is why we need to create a .cpp file along with the .h and ...
0 votes
0 answers
60 views
Is their a better way to implement class in c++? [duplicate]
When we write class in c++ we divide class into two part that header file and cpp file. In header file we define class and prototype Class.h class A{ public: void functionName(); }; Then we define ...
0 votes
1 answer
53 views
Pracitcal design difference between header and cpp files [duplicate]
Comming from a mostly Java background I recently started learning a bit of C++. I'm trying to create a class that describes a LinkedList. Since I want this class to be something like an external ...
0 votes
0 answers
48 views
(C++) Dont use header files [duplicate]
I'm having a general question about header files: In my project, which is not a library or plugin, but a standalone program, I don't use any header files - I am just using cpp files that I include in ...
0 votes
0 answers
41 views
Does every C++ implementation file need its own headers? [duplicate]
I'm a beginner to C++ and I am getting extremely confused trying to understand the benefits of the recommended practices regarding headers and implementation files. Say I have two implementation ...
0 votes
1 answer
42 views
use header file for sources instead using cpp file and .lib [duplicate]
Hello i have a code like this in c++: #include <iostream> using namespace std; namespace exercises { class String; ostream& operator<<(ostream&, const String&); istream&...
1 vote
0 answers
40 views
Language design question: why do C and C++ separate the declaration of types and functions from the definitions? [duplicate]
I've been making websites and odd projects with Python and Javascript for about 4 years, and now I'm making the leap down to C/C++ for a new endeavor. Forgive me if my terminology is off, still ...
0 votes
0 answers
30 views
why we need header files in our C++ project? [duplicate]
why we need header files instead of just define things in library files ? I'm asking this question because I recently read an article like this: While it is true that all definitions are ...
0 votes
0 answers
25 views
Confusion in linking concept in compiler [duplicate]
My question is regarding preprocessor and linking phase confusion in compilers (typically c++ compilers). From the online sources, its said that the compiler doesnt look at other source codes until ...
0 votes
0 answers
21 views
Why declaration of a pre defined function and code of that pre defined function are in different files? [duplicate]
Suppose we consider math library functions in c language . declaration's are in header file #include whereas code is in math library (which we link during compilation by -lm) . Why both declaration ...
224 votes
13 answers
91k views
Why does C++ need a separate header file?
I've never really understood why C++ needs a separate header file with the same functions as in the .cpp file. It makes creating classes and refactoring them very difficult, and it adds unnecessary ...
129 votes
12 answers
152k views
What should go into an .h file?
When dividing your code up into multiple files, what exactly should go into an .h file and what should go into a .cpp file?
110 votes
8 answers
126k views
Considerations for defining a member function inside or outside the class
What considerations are there when deciding between defining a member function inside or outside the body of a class? In the class: class Clazz { public: void Fun1() { //do something ...
46 votes
3 answers
169k views
Already defined in .obj - no double inclusions
I happened to get that already defined in .obj error. This is structure of my project: main.cpp #include "main.h"; main.h #include <iostream> #include <string> #include <...
3 votes
7 answers
30k views
Why can I declare constructor out of the class
Consider this link. See this code: class MyClass { public: MyClass(); ~MyClass(); private: int _a; }; MyClass::MyClass() { } ...
25 votes
4 answers
6k views
When using C++ modules, is the any reason to separate function declarations (.hpp files) from their definitions (.cpp files)?
I'm used to writing code without modules where the header files contain the function declarations like: // foo.h class Foo { void bar(); }; and the corresponding .cpp file contains the ...
8 votes
4 answers
23k views
Arduino library: multiple definitions of a function
Today I encountered a weird problem when trying to use IRremote library, and I managed to strip down the problem as following. If you have a folder in libraries, with Foo.h and Foo.cpp inside, and ...
7 votes
4 answers
27k views
Separate classes into headers and source files in C++
How do I separate classes into multiple files? Here is my understanding so far: Create new class, and a ".h" and a ".cpp" file for it. You use #include classname.h in your main ...
9 votes
8 answers
912 views
Ways not to write function headers twice?
I've got a C/C++ question, can I reuse functions across different object files or projects without writing the function headers twice? (one for defining the function and one for declaring it) I don't ...
5 votes
2 answers
28k views
Compiling with g++ - including header files
I just have a quick question as I'm trying to understand how to compile (in ubuntu 12.04) a main in c++ that includes a simple header file. The command: g++ -o main main.cpp add.cpp -Wall Works ...
6 votes
4 answers
2k views
Why create a .cpp file when I could declare and define it all in the header file?
I understand and very much appreciate the practice of separating header (.h, .hpp) and source files (.cpp). However, many simple classes can be expressed entirely in the header file itself. I could ...
2 votes
10 answers
2k views
How should I write classes? C++
Hey.. I don't really get them. I read a tutorial about classes in C++, and I don't get a few things: In every example and tutorial that I've seen, functions are never written inside the class! For ...
3 votes
2 answers
10k views
Visual C++/CLI - CLR Class - LNK2020 Error - how to fix?
I have a project, where I must connect to a library that is C based, and I was not able to get at the functions from C# using the DLLImport. I have to use a project that is C++/CLI based to surface ...
2 votes
5 answers
1k views
When should I use a header file?
After tutoring a few students at a local college, I was asked "when is best to use a header file in C++ compared to a regular .cpp file?". I kind of struggled with the answer and was seeing if there ...
3 votes
3 answers
9k views
what is the difference between header and source files C++?
I would like to know the difference between header (h) and source (cpp) files. We usually use headers for declaration and cpp s for definition of non-template stuff, and 2 different headers for ...
3 votes
2 answers
2k views
Definition of C++ methods inside the class like Java
I'm Java programmer and I'm new to C++ Programming. In java we have to write all classes in separate files and all method's definitions are right inside the class. But now in C++ I'm wonder that why C+...