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

problems with inheritance in g++ compiler

P: 22
I'm trying to compile the following code but it won't work.

the compiler always tells me:

michael@michael:~/forum$ g++ *.cpp
ClassB.cpp: In member function `virtual void ClassB::abstractMethod(int) const
':
ClassB.cpp:6: error: passing `const ClassB' as `this' argument of `void
ClassA::setAttribute(int)' discards qualifiers
michael@michael:~/forum$

I've tried to compile the same code with Borland C++ BuilderX and it works.

this is the code I'm trying to compile:
//file ClassA.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef CLASSA_H
  2. #define CLASSA_H
  3. class ClassA{
  4.     private:
  5.         int attribute;
  6.     public:
  7.         ClassA(int attribute);
  8.         void setAttribute(int attribute);
  9.         virtual void abstractMethod(int attribute)const=0;
  10. };
  11. #endif
  12.  
//file ClassB.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef CLASSB_H
  2. #define CLASSB_H
  3. #include "ClassA.h"
  4. class ClassB : public ClassA{
  5.     private:
  6.     public:
  7.         ClassB(int attribute);
  8.         void abstractMethod(int attribute) const;
  9. };
  10. #endif
  11.  
//file ClassA.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "ClassA.h"
  2.  
  3. ClassA::ClassA(int attribute){
  4.     this->attribute = attribute;
  5. }
  6.  
//file ClassB.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "ClassB.h"
  2.  
  3. ClassB::ClassB(int attribute):
  4. ClassA(attribute){}
  5. void ClassB::abstractMethod(int attribute) const{
  6.     setAttribute(attribute);
  7. }
  8.  
Sep 21 '06 #1
Share this Question
Share on Google+
4 Replies


Banfa
Expert Mod 5K+
P: 8,916
It is because g++ is a more rigerous compiler than borland and has detected a fault that borland hasn't.

The problem is that

void abstractMethod(int attribute) const;

is declared const this means that it is saying that it will not be changing any class member variables. This is important because it means you can call the function on a const object.

However this function calls

void setAttribute(int attribute);

which is not declared const and in fact does change the class member variable attribute.

In calling setAttribute from abstractMethod you are loosing the const qualifer on the call.

Fix all these errors byt changing

void abstractMethod(int attribute) const;

to

void abstractMethod(int attribute);

in all cases.
Sep 21 '06 #2

P: 22
I get the idea now, a virtual method in c++ is not the same as an abstract method in java. But isn't there a way to declare a class abstract, that is that there can be no instances of this class?
Sep 21 '06 #3

100+
P: 144
I get the idea now, a virtual method in c++ is not the same as an abstract method in java. But isn't there a way to declare a class abstract, that is that there can be no instances of this class?
The existance of a pure virtual member function (ie. a virtual function followed by the pure specifier "= 0") implies that the class as a whole is abstract. In your example code ClassA is abstract, and the compiler will give an error if you try and instantiate an instance of ClassA.
Sep 21 '06 #4

P: 22
thank you both very much. You where of great help.
Sep 22 '06 #5

Post your reply

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