473,763 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handle Class

55 New Member
Hello all C++ expert programmer,

I have a handle class which point to another class and use the pointer as object.

I follow the code from C++ articles submited by someone in this forum. Unfortunately, my compilation is failed.

Below is my code :

Expand|Select|Wrap|Line Numbers
  1. // Main.cpp
  2.  
  3. #include<iostream>
  4. #include "myclass.h"
  5. #include "HandleClass.h"
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     Handle aHandler(3, 4);
  13.  
  14.     aHandler->getx();
  15.     aHandler->gety();
  16.  
  17.     return 0;
  18. }
  19.  
  20.  
Heade File :
Expand|Select|Wrap|Line Numbers
  1. // Handle Class
  2.  
  3. #ifndef _myclass_
  4. #define _myclass_
  5.  
  6. class myclass
  7. {
  8.     int x, y;
  9.  
  10. public:
  11.     myclass() : x(0), y(0){}
  12.     myclass(int user1, int user2) : x(user1), y(user2){}
  13.  
  14.     ~myclass(){}
  15.  
  16.     void getx(){std::cout << "X is " << x;}
  17.     void gety(){std::cout << "Y is " << y;}
  18. };
  19.  
  20. #endif
  21.  
  22.  
Header File :
Expand|Select|Wrap|Line Numbers
  1. // Handle to class
  2.  
  3. #ifndef _Handle_
  4. #define _Handle_
  5.  
  6. class Handle
  7. {
  8.     Handle *handler;
  9. public:
  10.     Handle(int user1, int user2) : handler(new myclass(user1, user2)){}
  11.     Handle *operator->(){return handler;}
  12.  
  13.     ~Handle(){delete handler;}
  14.  
  15. };
  16.  
  17. #endif
  18.  
  19.  
Error Message is :
1. c:\documents and settings\nichol as tse\my documents\c++\h andleclass.h(10 ) : error C2440: 'initializing' : cannot convert from 'class myclass *' to 'class Handle *'
Types pointed to are unrelated; conversion requires reinterpret_cas t, C-style cast or function-style cast

2. c:\documents and settings\nichol as tse\my documents\c++\h andleclass.h(10 ) : error C2439: 'handler' : member could not be initialized
c:\documents and settings\nichol as tse\my documents\c++\h andleclass.h(8) : see declaration of 'handler'


3. c:\documents and settings\nichol as tse\my documents\c++\h andleclass.cpp( 14) : error C2039: 'getx' : is not a member of 'Handle'
c:\documents and settings\nichol as tse\my documents\c++\h andleclass.h(7) : see declaration of 'Handle'

4. c:\documents and settings\nichol as tse\my documents\c++\h andleclass.cpp( 15) : error C2039: 'gety' : is not a member of 'Handle'
c:\documents and settings\nichol as tse\my documents\c++\h andleclass.h(7) : see declaration of 'Handle'

These are all the errors i got from the compiler.

Thaks for your help.

Your help is greatly appreciated by me and others.
Jun 2 '07 #1
3 3152
AdrianH
1,251 Recognized Expert Top Contributor
Hello all C++ expert programmer,

I have a handle class which point to another class and use the pointer as object.

I follow the code from C++ articles submited by someone in this forum. Unfortunately, my compilation is failed.

Below is my code :

Expand|Select|Wrap|Line Numbers
  1. // Main.cpp
  2.  
  3. #include<iostream>
  4. #include "myclass.h"
  5. #include "HandleClass.h"
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     Handle aHandler(3, 4);
  13.  
  14.     aHandler->getx();
  15.     aHandler->gety();
  16.  
  17.     return 0;
  18. }
  19.  
  20.  
Heade File :
Expand|Select|Wrap|Line Numbers
  1. // Handle Class
  2.  
  3. #ifndef _myclass_
  4. #define _myclass_
  5.  
  6. class myclass
  7. {
  8.     int x, y;
  9.  
  10. public:
  11.     myclass() : x(0), y(0){}
  12.     myclass(int user1, int user2) : x(user1), y(user2){}
  13.  
  14.     ~myclass(){}
  15.  
  16.     void getx(){std::cout << "X is " << x;}
  17.     void gety(){std::cout << "Y is " << y;}
  18. };
  19.  
  20. #endif
  21.  
  22.  
Header File :
Expand|Select|Wrap|Line Numbers
  1. // Handle to class
  2.  
  3. #ifndef _Handle_
  4. #define _Handle_
  5.  
  6. class Handle
  7. {
  8.     Handle *handler;
  9. public:
  10.     Handle(int user1, int user2) : handler(new myclass(user1, user2)){}
  11.     Handle *operator->(){return handler;}
  12.  
  13.     ~Handle(){delete handler;}
  14.  
  15. };
  16.  
  17. #endif
  18.  
  19.  
Error Message is :


These are all the errors i got from the compiler.

Thaks for your help.

Your help is greatly appreciated by me and others.
It is quite simple, look at line 10 in file handleclass.h, then look at the message. myclass is not a Handle so you cannot assign a myclass pointer to it.


Adrian
Jun 2 '07 #2
Peterwkc
55 New Member
Then how to solve it. I have no idea what is handle class.

Thanks for your help.

Your help is greatly appreciated by me and others.
Jun 3 '07 #3
JosAH
11,448 Recognized Expert MVP
Hello all C++ expert programmer,

I have a handle class which point to another class and use the pointer as object.

I follow the code from C++ articles submited by someone in this forum. Unfortunately, my compilation is failed.
Have a look at the C/C++ Articles section; it has a nice article about just this:
handle classes; the article even explains reference counting handle classes or
'fat pointers' or 'smart pointers'.

kind regards,

Jos
Jun 3 '07 #4

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

Similar topics

7
3256
by: Tony Johansson | last post by:
Hello!! Assume I have a handle body pattern with classes called Handle and Body. In the Body class I store one int value for example 7 or some other integer value. In the Handle class I have a pointer to the Body class. If a want to create a STL container of List with the following declaration List <Handle <Body> > list
0
2172
by: Tony Johansson | last post by:
Hello! Here I have two classes these are called Handle and Body and a main. You have the class definition below. Some basic information. In the Handle class is there a pointer to the Body. Each Body object contains one primitive datatype int. The Body instance is created in the constructor for Handle. In main I instansiate some Handle object and use the STL function push_front to push these object into the list.
2
3053
by: Indiana Epilepsy and Child Neurology | last post by:
Before asking this questions I've spent literally _years_ reading (Meyer, Stroustrup, Holub), googling, asking more general design questions, and just plain thinking about it. I am truly unable to figure out what would be a "proper" OO design (in C++) for this. There may be alternatives to writing my own ODBC handle classes, and I may be interested in them, but I'd like to pursue this particular problem, if for no other reason than to...
6
2873
by: Leandro Berti via DotNetMonster.com | last post by:
Hi All, I wrote a code to do serial communication with an equipament. When i use the code outside of threaded class it seens work properly, but when i put inside a class and execute a thread in the first seconds the communication is ok, later i receive read/write error. I?ve been in MSDN site and there i discover that the read/write error is a INVALID_HANDLE problem. But why??? I just create the serial communication file and use it....
4
1190
by: SamSpade | last post by:
If I have a class that inherits from, say RichTextBox and in the derived class I refer to Handle, does it refer to the RichTextBox class or the derived class? I'm wondering if the derived class gets it's own handle or is there only one for both classes (after all there is only one window)? Thanks
2
3062
by: Gary Wessle | last post by:
Hi I need help organizing this program in the right way. I included the code below which compiles and runs and gives the desired effect to a certain point, but I don't know what the next step is to finish this program. the program presents Main menu to the user and gives a prompt, the user types the number corresponding to an item from the menu, the program then
6
2052
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in your business layer and/or data access layer? suppose in my data access layer, I provide try and catch, log the exception and re-throw it back to business layer, then in yoru business layer, what do you do? throw it back to the code behind or...
2
35619
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
4
4310
by: Donos | last post by:
Hi I have a HANDLE to an Event, like this.. HANDLE h = ::CreateEvent(NULL, FALSE, FALSE, NULL); This is running in one thread in one class. For example we will call that class as "Class A" Now i want to use this HANDLE in another thread in another class to
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.