473,383 Members | 1,952 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,383 software developers and data experts.

Handle Class

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\nicholas tse\my documents\c++\handleclass.h(10) : error C2440: 'initializing' : cannot convert from 'class myclass *' to 'class Handle *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

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


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

4. c:\documents and settings\nicholas tse\my documents\c++\handleclass.cpp(15) : error C2039: 'gety' : is not a member of 'Handle'
c:\documents and settings\nicholas tse\my documents\c++\handleclass.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 3123
AdrianH
1,251 Expert 1GB
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
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 Expert 8TB
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
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...
0
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...
2
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...
6
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...
4
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...
2
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...
6
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...
2
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...
4
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"...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.