Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multiple inheritance problem

Posted on 2001-06-12
14
Medium Priority
?
261 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 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

879 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