user409333
user409333

Reputation:

Problem in Include file

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"

Upvotes: 1

Views: 237

Answers (3)

Simone
Simone

Reputation: 11797

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.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882716

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.

Upvotes: 2

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

You probably just need to add -I. flag to you compile line.

Upvotes: 0

Related Questions