473,320 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

problem in seprating interface from implementation

151 100+
I am keeping three program
1.program.h
2.program.cpp
3. main.cpp
in three different files

and running the main.cpp files but still getting errors can anyone suggest me ,how to rectify this.I will very much thankful to you.
//////////////////////1.program.h//////////////////////////////////////////////////////////////

#ifndef _PROGRAM_H_
#define _PROGRAM_H_
class hello
{
int x;
public:
void set_value(int);
int get_value();
};
#endif

////////////////////////////////2.program.cpp///////////////////////////////////////////////////////////


#include "program.h"
#include <iostream>
void hello::set_value(int y)
{
x=y;
}
int hello::get_value()
{
return x;
}

////////////////////////////////////////////////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;
}

/////////////////////////////////////////////Error Message//////////////////////////////////
/tmp/ccwZPvpG.o: In function `main':
main.cpp:(.text+0x92): undefined reference to `hello::set_value(int)'
main.cpp:(.text+0x9d): undefined reference to `hello::get_value()'
collect2: ld returned 1 exit status
Compilation failed.
Apr 4 '08 #1
6 1625
gpraghuram
1,275 Expert 1GB
Did u compile the the program.cpp also?
I think u have missed that

Raghuram
Apr 4 '08 #2
Man4ish
151 100+
Did u compile the the program.cpp also?
I think u have missed that

Raghuram

I complied program.cpp also still i am facing the pblm .Actually i am working on linux .
while compiling the program.cpp i am getting the following error.

manish@manish:~/Desktop/interface$ g++ program.cpp
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
Apr 4 '08 #3
Savage
1,764 Expert 1GB
Your project doesn't include project.cpp.
You can do this

Expand|Select|Wrap|Line Numbers
  1. //////////////////////1.program.h//////////////////////////////////////////////////////////////
  2.  
  3.  
  4. #ifndef _PROGRAM_H_
  5. #define _PROGRAM_H
  6.  
  7. class hello
  8. {
  9. int x;
  10. public:
  11. void set_value(int);
  12. int get_value();
  13. };
  14.  
  15. //Include implementation file automatically
  16. #ifndef _PROGRAM_CPP
  17. #include "Program.cpp"
  18. #endif//_PROGRAM_CPP
  19.  
  20. #endif//_PROGRAM_H 
  21.  
  22. ////////////////////////////////2.program.cpp///////////////////////////////////////////////////////////
  23.  
  24. //Include only once
  25. #ifndef  _PROGRAM_CPP
  26. #define _PROGRAM_CPP  
  27.  
  28. #include "program.h"
  29. #include <iostream>
  30. void hello::set_value(int y)
  31. {
  32. x=y;
  33. }
  34. int hello::get_value()
  35. {
  36. return x;
  37. }
  38.  
  39. #endif
  40.  
  41. ////////////////////////////////////////////////3.main.cpp//////////////////////////////////
  42. #include <iostream>
  43. using std::cout;
  44. using std::endl;
  45. #include "program.h"
  46. int main()
  47. {
  48. hello h;
  49. h.set_value(4);
  50. std::cout<<h.get_value()<<endl;
  51. return 0;
  52. }


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.
Apr 5 '08 #4
Man4ish
151 100+
Your project doesn't include project.cpp.
You can do this

Expand|Select|Wrap|Line Numbers
  1. //////////////////////1.program.h//////////////////////////////////////////////////////////////
  2.  
  3.  
  4. #ifndef _PROGRAM_H_
  5. #define _PROGRAM_H
  6.  
  7. class hello
  8. {
  9. int x;
  10. public:
  11. void set_value(int);
  12. int get_value();
  13. };
  14.  
  15. //Include implementation file automatically
  16. #ifndef _PROGRAM_CPP
  17. #include "Program.cpp"
  18. #endif//_PROGRAM_CPP
  19.  
  20. #endif//_PROGRAM_H 
  21.  
  22. ////////////////////////////////2.program.cpp///////////////////////////////////////////////////////////
  23.  
  24. //Include only once
  25. #ifndef  _PROGRAM_CPP
  26. #define _PROGRAM_CPP  
  27.  
  28. #include "program.h"
  29. #include <iostream>
  30. void hello::set_value(int y)
  31. {
  32. x=y;
  33. }
  34. int hello::get_value()
  35. {
  36. return x;
  37. }
  38.  
  39. #endif
  40.  
  41. ////////////////////////////////////////////////3.main.cpp//////////////////////////////////
  42. #include <iostream>
  43. using std::cout;
  44. using std::endl;
  45. #include "program.h"
  46. int main()
  47. {
  48. hello h;
  49. h.set_value(4);
  50. std::cout<<h.get_value()<<endl;
  51. return 0;
  52. }


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.
Apr 7 '08 #5
Banfa
9,065 Expert Mod 8TB
This way header file Program.h will include it's implementation file automatically into your project.
This seems like a slightly strange suggestion to me, a normal rule of thumb is only include headers not source code files.

It appears to me that the OP is trying to compile and link program.cpp and main.cpp as though they are 2 separate program. Presumably he is calling cl (cc??) without a -c switch to compile only so the program is attempting the link.

Both files need compiling and then should be linked together so using the original source something like

cl program.cpp main.cpp
Apr 7 '08 #6
Savage
1,764 Expert 1GB
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.
Yes,you would not need program.h in program.cpp.Also,every IDE has option to set the multiple paths for include directories.Find and add there a full path to the directory where program.h is to be found.But as a rule of thumb angle brackets are only for library files(like iostream.h,math.h) and you should include your own with " ".

@Banfa

It is a bit strange but it works just like a header file would do.I suggested it to the OP so that he doesn't bother every time with linking the program.cpp's object file into the executable trough a make file.
Apr 7 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
3
by: Brad Quinn | last post by:
Friday, no brain power remains... I have three assemblies; Client, Interface and Implementation. The Client uses Implementation through remoting. Client has a reference to Interface, but not...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
0
by: Vince Zhang | last post by:
Hi, all I wanna make dso that implements OLEDBSimpleProvider interface to interact mshtml.dll. but meet problem in doing it. Could anybody help me? I do the following tasks: 1. use VS.NET IDE...
3
by: John Underwood | last post by:
Hi.. I was looking at interface, and I have a example in the docs i'll paste below.. I'm not grasping what you would gain by using a interface, does any one have a brief description of their...
2
by: COLIN JACK | last post by:
Hi All, I've got a situation where I'm implementing an interface (BaseInterface in example below) and I want to use explicity interface implementation of an event so that I can add type safety. ...
2
by: Wiktor Zychla [C# MVP] | last post by:
Hi, suppose there are two interfaces that contain methods with the same name but different signature: interface I1 { void F(); } interface I2 { int F(); } it is then easy to implement...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
3
by: Builder | last post by:
Hello, I created a DirectShow filter which detects faces from video. Filter exposes interface, which enables to set a callback, which is called when a face is detected. I made an application in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.