By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,890 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,890 IT Pros & Developers. It's quick & easy.

Handle Class

P: 55
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
Share this Question
Share on Google+
3 Replies


AdrianH
Expert 100+
P: 1,251
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

P: 55
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

Expert 10K+
P: 11,448
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

Post your reply

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