Your project doesn't include project.cpp.
You can do this
- //////////////////////1.program.h//////////////////////////////////////////////////////////////
-
-
-
#ifndef _PROGRAM_H_
-
#define _PROGRAM_H
-
-
class hello
-
{
-
int x;
-
public:
-
void set_value(int);
-
int get_value();
-
};
-
-
//Include implementation file automatically
-
#ifndef _PROGRAM_CPP
-
#include "Program.cpp"
-
#endif//_PROGRAM_CPP
-
-
#endif//_PROGRAM_H
-
-
////////////////////////////////2.program.cpp///////////////////////////////////////////////////////////
-
-
//Include only once
-
#ifndef _PROGRAM_CPP
-
#define _PROGRAM_CPP
-
-
#include "program.h"
-
#include <iostream>
-
void hello::set_value(int y)
-
{
-
x=y;
-
}
-
int hello::get_value()
-
{
-
return x;
-
}
-
-
#endif
-
-
////////////////////////////////////////////////3.main.cpp//////////////////////////////////
-
#include <iostream>
-
using std::cout;
-
using std::endl;
-
#include "program.h"
-
int main()
-
{
-
hello h;
-
h.set_value(4);
-
std::cout<<h.get_value()<<endl;
-
return 0;
-
}
This way header file Program.h will include it's implementation file automatically into your project.
PS:Please use code tags,they make your code much,much more readable.To use them just select your code and click on # button.You can also select a language to mark various keywords with color.For e.g [code=c] will format it using c keywords and coloring scheme or [code=cpp] for c++ code.
Thnaks sir ,
But sir when i do ,
the same it is giving the error
1. program.h:8: error: redefinition of ‘class hello’
2.program.h:10: error: previous definition of ‘class hello’
But when i put comment on the //#include "program.h" in program.cpp it is working fine then i should not include the header file in program.cpp ?
But in Deitel and Deitel it is shown the same ,as you explained ,so what to do?
I am also trying to putting the code interface in different folder(interface) and implementation in different folder (implementation) and main in the same directory in which the folders implementation and interface are present.
But they are giving error
///////////////////////////////program.h////////////////////////////////////////
#ifndef _PROGRAM_H_
#define _PROGRAM_H
class hello
{
int x;
public:
void set_value(int);
int get_value();
};
#ifndef _PROGRAM_CPP
#include <implementation/Program.cpp>
#endif//_PROGRAM_CPP
#endif//_PROGRAM_H
/////////////////////////Program.cpp/////////////////////////////////////////////////////
#ifndef _PROGRAM_CPP
#define _PROGRAM_CPP
//#include "program.h"
#include <iostream>
void hello::set_value(int y)
{
x=y;
}
int hello::get_value()
{
return x;
}
#endif
///////////////////////////////////////main.cpp/////////////////////////////////////////
using std::cout;
using std::endl;
#include <program.h>
int main()
{
hello h;
h.set_value(4);
std::cout<<h.get_value()<<endl;
return 0;
}
it is giving error
main.cpp:10:21: error: program.h: No such file or directory
main.cpp:16: error: ‘hello’ was not declared in this scope
main.cpp:16: error: expected `;' before ‘h’
main.cpp:18: error: ‘h’ was not declared in this scope
It is not including program .h,if i give full path home/manish/Desktop/folder/program.h then it is working fine ,but this is not good practice to keep full path , how can i solve this pblm , Do I need makefile if yes then how can i make it .Please help me out.