Solved

Multiple inheritance problem

Posted on 2001-06-12
14
229 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

821 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