Solved

Multiple inheritance problem

Posted on 2001-06-12
14
252 Views
Last Modified: 2010-04-02
class B{
  int member;
  int accessor(void){return member;}};

class C{
  virtual int accessor(void)=0;  
}


class A:public B,public C{
  int accessor(void){return B:accessor;}
};

void d(C&);

void main(void){
 A a(4);//sets up member as 4;
 d(A);
}

different file:

void d(C&){
  cout<<accessor();}

this programme messes up on the call to d, with
cannot access address ...

what is wrong and how can I make it better?
0
Comment
Question by:glebspy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
  • 2
14 Comments
 
LVL 3

Accepted Solution

by:
elcapitan earned 50 total points
ID: 6184673
You have so many mistakes in your code, I don't know if I'll be able to point them all. But I'll try:
1. syntax:
A. add ; at the end of class C decleration
B. >>return B:accessor; - should be return B::accessor();
C. >>d(A); - should be d(a);

2. class decleration:
You need to declare the function as public, or you will not be able to access them. The default access level is private. For example:
class B
{
 int member;
public:
 int accessor(void){return member;}};

class C
{
public:
 virtual int accessor(void)=0;  
};

class A:public B,public C
{
public:
     int accessor(void){return B::accessor();}
};

3. other:
>>A a(4);//sets up member as 4;
You cannot pass 4 as a constructor member, since you did not create a constructor that gets parameters.

--EC--
0
 
LVL 3

Expert Comment

by:elcapitan
ID: 6184680
I made a mitake
>>You cannot pass 4 as a constructor member
should be:
You cannot pass 4 as a constructor parameter

--EC--
0
 
LVL 1

Author Comment

by:glebspy
ID: 6185025
Please imagine that I posted the following code:

class B{public:
               int member;
               int accessor(void){return member;}};

              class C{public:
               virtual int accessor(void)=0;  
              };


              class A:public B,public C{public:
               int accessor(void){return B::accessor();}
              };

              void d(C&);

              void main(void){
              d(a);
              }

              different file:

              void d(C&){
               cout<<accessor();}


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:NickAtBOM
ID: 6185047
Still a couple of things to do:

I'm guessing that the code should read:

void main(void){
A a;
d(a);
}

then the d function should be

void d(C& objC){
cout << objC.accessor();
}
0
 
LVL 1

Author Comment

by:glebspy
ID: 6185065
Please imagine that I posted the following code:

class B{public:
                            int member;
                            int accessor(void){return member;}};

                           class C{public:
                            virtual int accessor(void)=0;  
                           };


                           class A:public B,public C{public:
                            int accessor(void){return B::accessor();}
                           };

                           void d(C&);

                           void main(void){
                           d(a);
                           }

                           different file:

                           void d(C&c){
                            cout<<c.accessor();}
0
 
LVL 1

Author Comment

by:glebspy
ID: 6185070

              Please imagine that I posted the following code:

              class B{public:
                                         int member;
                                         int accessor(void){return member;}};

                                        class C{public:
                                         virtual int accessor(void)=0;  
                                        };


                                        class A:public B,public C{public:
                                         int accessor(void){return B::accessor();}
                                        };

                                        void d(C&);

                                        void main(void){
                                        A a;d(a);
                                        }

                                        different file:

                                        void d(C&c){
                                         cout<<c.accessor();}
0
 
LVL 3

Expert Comment

by:elcapitan
ID: 6185082
Now you do not have access vaiolations.
what is the question?

--EC--
0
 
LVL 1

Author Comment

by:glebspy
ID: 6185085

              Please imagine that I posted the following code:

              class B{public:
                                         int member;
                                         int accessor(void){return member;}};

                                        class C{public:
                                         virtual int accessor(void)=0;  
                                        };


                                        class A:public B,public C{public:
                                         int accessor(void){return B::accessor();}
                                        };

                                        void d(C&);

                                        void main(void){
                                        A a;d(a);
                                        }

                                        different file:

                                        void d(C&c){
                                         cout<<c.accessor();}
0
 
LVL 1

Author Comment

by:glebspy
ID: 6185086

              Please imagine that I posted the following code:

              class B{public:
                                         int member;
                                         int accessor(void){return member;}};

                                        class C{public:
                                         virtual int accessor(void)=0;  
                                        };


                                        class A:public B,public C{public:
                                         int accessor(void){return B::accessor();}
                                        };

                                        void d(C&);

                                        void main(void){
                                        A a;d(a);
                                        }

                                        different file:

                                        void d(C&c){
                                         cout<<c.accessor();}
0
 
LVL 1

Author Comment

by:glebspy
ID: 6185100
Unfortunately I do still have access violations.
The above code is just a representation of the real code I'm running.

My real question is, of course, not what is wrong with this code, but what mistake am I likely to be making in my real code which results in the access violation.

I'm basically passing an A object which is fine in main just before the function call (checked with a debugger) but as soon as the control enters d function, the object's members are completely corrupted.

What could possibly cause such a mistake?
0
 
LVL 1

Author Comment

by:glebspy
ID: 6185103
Bear in mind that the real code compiles correctly!
0
 

Expert Comment

by:NickAtBOM
ID: 6185208
The last version of the code compiles and runs fine, using VC6.  The only extra thing I had to do to really confirm that things were working was to add a constructor to class A to initialise the 'member' variable.

Its a bit difficult to work out what 'access violations' you are getting in your real code, without seeing it.  Have you overridden the accessor method in the derived class, and does it specifically call the accessor method that it inherits from B?
0
 
LVL 1

Author Comment

by:glebspy
ID: 6185705
yes and yes!
0
 
LVL 1

Author Comment

by:glebspy
ID: 6186158
Thanks for taking the time. I've decided to post my real code to see if someone can spot the problem.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question