Solved

Multiple inheritance problem

Posted on 2001-06-12
14
211 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
  • 9
  • 3
  • 2
14 Comments
 
LVL 3

Accepted Solution

by:
elcapitan earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Expert Comment

by:NickAtBOM
Comment Utility
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
Comment Utility
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
Comment Utility

              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
Comment Utility
Now you do not have access vaiolations.
what is the question?

--EC--
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:glebspy
Comment Utility

              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
Comment Utility

              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
Comment Utility
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
Comment Utility
Bear in mind that the real code compiles correctly!
0
 

Expert Comment

by:NickAtBOM
Comment Utility
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
Comment Utility
yes and yes!
0
 
LVL 1

Author Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now