Solved

Multiple inheritance problem

Posted on 2001-06-12
14
240 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

713 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