I want to write two program(.h and .cpp) with below code and use .h file in .cpp but when i run it in TC occur below error
.h File
#ifndef ADD_H
#define ADD_H
int add(int x, int y)
{
return x + y;
}
#endif
.cpp file
#include <iostream.h>
#include <conio.h>
#include "Add.h"
void main()
{
clrscr();
cout << "Sum of 3 and 4 :" << add(3, 4);
getch();
}
Error
Unable to open include file "Add.h"
There's a few things you should look into:
the location (search path) of header files is implementation dependent, both for the <> and "" variants - make sure your header file is in that path somewhere.
you may find that you need to use add.h (all lowercase).
you shouldn't generally include code in header files (you should put it in a separate C file and just use the header file to list declarations (or the prototype in your case).
if that's Turbo C you're using (and it probably is, given the clrscr and getch), there's really no excuse not to upgrade to a more modern environment.
You probably just need to add -I. flag to you compile line.
Add.h is not in the includes path of your compiler.
By the way, iostream.h is deprecated, you should include iostream. Also, cout is in the std namespace, so you need a using namespace std; in your .cpp file or, alternatively, use std::cout instead of cout.
Related
I have the following question. Supposing I have an header file header.hpp which is include in a test.cpp file. Is it possible to add instructions to the header.hpp file in order to check (maybe at compile time) if some C stdio functions are used in the test.cpp file and in positive case do something specific? For example something like:
header.hpp:
#ifndef HEAD
#define HEAD
#include <iostream>
if ( C stdio functions are used in test.cpp ){
std::cout << "Hey" << "\n";
}
#endif
test.cpp
#include "header.hpp"
#include <cstdio>
int main(){
printf( "Hello\n" ); // C stdio function has been used.
}
Output:
Hello
Hey
No, this is not possible. Neither C++, nor C, work like this, on a fundamental level.
An #include is logically equivalent to physically inserting the contents of the included file into the including file. Doing a cut and paste of your header.hpp into the beginning of your test.cpp replacing the #include accomplishes exactly the same thing.
The resulting code gets compiled from beginning to the end, in order.
When reading the header file, the C++ compiler has no knowledge, whatsoever, of something it hasn't read yet. An #include stops reading the file that it's in, and the included file gets read and compiled, before proceeding.
So i'm doing some basic OpenGL stuff and for my math functions like vectors, matrices i use the GLM library. I created a header file which is supposed to work with the said library and i noticed it compiles and works as intended without even including the needed headerfiles of the GLM-library.
My simplified example program:
Main.cpp
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
#include "Header.h"
int main(int argc, char* args[]){
Header::test();
return 0;
}
Header.h
#pragma once
#include <iostream>
namespace Header {
void test() {
glm::vec3 vec = glm::vec3(1.0f, 5.0f, 1.0f);
std::cout << vec.x << std::endl;
std::cout << vec.y << std::endl;
}
};
My output:
1
5
How is it possible that I don't need to include the GLM-headers in my "Header.h" file?
For C++ programs, the compiler only compiles the .cpp files as a unit. The header files are "included" into the .cpp files.
So the compiler compiles Main.cpp and when the compiler sees #include "Header.h", then it replaces the #include line with the contents of that file.
Because of how this works, the header file does not need to include everything that's needed. This is because it was included by the cpp file before your header file.
Some quirks about this:
If you do #include "Header.h" in another .cpp file that does not have the GLM header files before it, then it will not compile.
If you took the Header.h file and renamed it to a .cpp file, it won't work because then the compiler will attempt to compile it as it's own unit (which will fail because the GLM files are not there).
.h Files are not compiled. Since you include the glm headers first, when the header.h file is included to the cpp, glm is already included. If you tried to include the header in a separate cpp that didn't have #include <glm.hpp>, it would fail.
You have included the GLM headers already, before you include Header.h.
The compilation unit is your Main.cpp file; header files are not compiled separately.
The C-Preprocessor cpp processes all the # statements before compilation is attempted. Try to run cpp Main.cpp: that will show you the source file that g++ will actually compile.
I'm a bit confused at the moment because I'm planning to include multiple source and header files for the first time in one of my projects.
So I'm wondering if this would be the right approach?
Do I have to include the string header in every source file that uses it directly?
And what about the "stdafx.hpp" header that Visual C++ wants me to include?
Would that be the way to go?
main.cpp
#include "stdafx.hpp"
#include <string> //?
#include <stringLib1.h>
#include <stringLib2.h>
using std::string;
//use a windows.h function here
//use a stringLib1 function here
//use a stringLib2 function here
stringLib1.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass1
{
public:
string GetStringBack1(string myString);
};
stringLib1.cpp
#include "stdafx.hpp"
string uselessClass1::GetStringBack1(string myString) {
return myString;
}
stringLib2.h
#include "stdafx.hpp"
#include <string>
using std::string;
class uselessClass2
{
public:
string GetStringBack2(string myString);
};
stringLib2.cpp
#include "stdafx.hpp"
string uselessClass2::GetStringBack2(string myString) {
return myString;
}
A good practice is usually to include only what your code uses in every file. That reduces dependencies on other headers and, on large projects, reduce compilation times (and also helps finding out what depends on what)
Use include guards in your header files
Don't import everything by polluting the global namespace, e.g.
using namespace std;
but rather qualify what you intend to use when you need it
You don't need stdafx.h in your project unless you're using precompiled headers. You can control this behavior in the VS project properties (C/C++ -> Precompiled Headers -> Precompiled Header)
The stdafx.h header is needed if precompiled header is enabled in VS. (Read this one)
You only need to include the stdafx.h in your .cpp files as the first include.
Regarding the header and cpp files (which come in pairs), include things necessary for the declaration in the header, and include everything else (necessary for the definition) in the cpp. Also include the corresponding header in its cpp pair too. And use include guards.
myclass.h
#ifndef MYCLASS_H // This is the include guard macro
#define MYCLASS_H
#include <string>
using namespace std;
class MyClass {
private:
string myString;
public:
MyClass(string s) {myString = s;}
string getString(void) {return myString;}
void generate();
}
myclass.cpp
#include <stdafx.h> // VS: Precompiled Header
// Include the header pair
#include "myclass.h" // With this one <string> gets included too
// Other stuff used internally
#include <vector>
#include <iostream>
void MyClass::generate() {
vector<string> myRandomStrings;
...
cout << "Done\n";
}
#endif
Then in main(...), you can just include myclass.h and call generate() function.
The stdafx include should be at the top of every .cpp file and it should NOT be in .h files.
You could put #include < string > in stdafx.h if you don't want to put it in every other file.
I suppose that you must be having your own header files also which might be requiring in other cpp files and header files. Like the one you gave
#include <stringLib1.h>
#include <stringLib2.h>
In my opinion, its better to create one common header file in which you include all the common library header files and your project header file. This file then you can include in all the other cpp files and header file. And it will be better to use header guards also.
So, considering a common header file "includes.h".
#ifndef INCLUDES_H
#define INCLUDES_H
#include <string>
#include <stringLib1.h>
#include <stringLib2.h>
/***Header files***/
#endif //INCLUDES_H
This is now your common header file. This you can include in all your project files.
Add.cpp
int add(int x, int y)
{
return x + y;
}
Main.cpp
#include <iostream>
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
return 0;
}
When I try to compile this program I get an error message for line 6 of main.cpp that states: "error: 'add' was not declared in this scope".
Create a header file
Contents:
int add(int x, y);
Include that file main.cpp
i.e. #include "headerfile.h"
Then the rest is up to the compiler environment. Basically need to compile each .cpp to object code and then link them. You need to read up about this as this differs between environment. Also read up on header guards and also stuff like graadle, SCONS, Makefiles. Also good to learn about version control systes e.g. mercurial.
Guess you going to have a busy day
You need Add.h file and include it in your Main.cpp
Add.h
int add(int x, int y);
Main.cpp
#include <iostream>
#include "Add.h"
...
In c++ the scope is all visible functions/methods and variables. In order for it to be seen in the scope in this instance you would have to create a header file that contains your method "add". One way to do this would be to instead of having it in a .cpp file have it in a .h file, then include that .h file in your main.cpp file like this
#include "Add.h"
At: http://www.learncpp.com/cpp-tutorial/110-a-first-look-at-the-preprocessor/
Under Header guards, there are those code snippets:
add.h:
#include "mymath.h"
int add(int x, int y);
subtract.h:
#include "mymath.h"
int subtract(int x, int y);
main.cpp:
#include "add.h"
#include "subtract.h"
How can I avoid #include "mymath.h" from appearing twice in main.cpp?
Thanks.
The lines right below that example explain it. Your mymath.h file should look like this:
#ifndef MYMATH_H
#define MYMATH_H
// your declarations here
#endif
Every header file should follow this basic format. This allows a header file to be included by any file that needs it (both header and source files), but the actual declarations will only be included at most once in each source file.
use #pragma once if you using MS VC++ or the standard way
inside mymath.h
#ifndef MYMATH_H
#define MYMATH_H
[code here]
#endif // MYMATH_H
That's ok if they included twice if all the headers files have header guards. The second and all subsequent inclusions would just add empty lines and there won't be any code duplication. Just make sure that mymath.h has header guards as well.
You should put your header guards in any header, also in mymath.h.