![]() |
|
#1
|
|||
|
|||
c++ inheritanceHello everyone,
I'm making some experiment on C++ inheritance and find some confusion. Say I have 3 classes, 1) class Cart, which has a method CPP / C++ / C Code:
2)a Good class, which is inherited by class called Book 3)Book class. CPP / C++ / C Code:
CPP / C++ / C Code:
And when I complied it I got an error saying "no match for 'operator*' in '*book' ". I think we I add an object, I should pass the real object which is the object the variable points to into the cart, instead of just put "book" value as a parameter. But neither way seemed to work. Could anyone give me any hint? Thanks! |
|||
|
#2
|
|||
|
|||
Re: c++ inheritanceThis link has some information,
http://www.parashift.com/c++-faq-lite/ Take a look at 19,20,21,22,23,24,25 If nothing there helps post back with more code. Because that is not really enough code to see what all you are doing. Are you using public, private or protected with inheritance(example of missing details)? Usually the smallest complete program that reproduces the error is best. |
|
#3
|
|||
|
|||
Re: c++ inheritanceThanks Sokar, I read the link pages and learned something new. However, problems are still not solved. Like you said, I will make a smallest possible program that demonstrate the error I encountered
This time say there are 4 classes: 1, Cart: CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
I hope I have demonstrated clearly enough. Sorry for putting such a lot of code, but I think this is the best I can address my problem. Thanks a lot!! |
|
#4
|
|||
|
|||
Re: c++ inheritanceIf you have do not have a reason for using char just switch it to,
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#5
|
||||
|
||||
Re: c++ inheritanceHi,
Please take a look at this: Pass by value and pass by reference: C++ always passes arguments `by value' by default: a copy of the value of each argument is passed to the function; the function cannot modify the actual argument passed to it: CPP / C++ / C Code:
If you do want a function to modify its argument you can obtain the desired effect using pointer arguments instead: CPP / C++ / C Code:
Your main program: CPP / C++ / C Code:
So you should use like this: CPP / C++ / C Code:
And you may switch to std::string instead of char* as sokar said. try this: CPP / C++ / C Code:
Note the difference between how a char* is passes and how a string is passed. Please post your full code at a stretch.(i.e dont split up your code) That may help more.. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#6
|
|||
|
|||
Re: c++ inheritancethanks a lot Sokar!
|
|
#7
|
|||
|
|||
Re: c++ inheritanceHi Paramesh,
Thank you for the excellent explanation. That's exactly where I was not clear, and am still but better. Could you explain in more detail about this example? CPP / C++ / C Code:
|
|
#8
|
||||
|
||||
Re: c++ inheritanceHi,
Where are your CPP / C++ / C Code:
Without knowing that, there is no way to understand your program. Please post them. About the errors: Undefined reference: When the linker tries to generate the application, it can't find the *definition* of your class or its methods. Note that definition is different from declaration. Now to the question you asked: CPP / C++ / C Code:
Passing by reference means that we'll pass the addresses of the variables instead of the values. CPP / C++ / C Code:
You need to know more about pointers You can find more about pointers in these links: http://www.gidforums.com/t-5393.html http://www.gidforums.com/t-5640.html Sorry. I have one more request. Please tell us what your program should do or what is your assignment. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#9
|
|||
|
|||
Re: c++ inheritanceHi Paramesh,
Thanks for your explanation and time. The question is to fill a basket of goods until the input file list is empty or the weight in basket excesses the allowed weight, using c++, provided w/ those classes necessary. so what i have to do is maily is to implement the fillBasket method, without touching other classes or headers. It's my first time using c++, so I have quite many questions. still not sure why the linker errors are there.. is it enough to just include the .h header files to make compile? Thanks again! |
|
#10
|
||||
|
||||
Re: c++ inheritanceHi,
Quote:
Else you need to define the class functions in your main program. Quote:
Quote:
Why do we get a linker undefined reference error? When the linker tries to generate the application, it can't find the *definition* of your class or its methods. So you get an error. Here is a sample code of mine that tells you how to to use header files along with passing arguments as you asked. header file main.h CPP / C++ / C Code:
C++ file main.cpp CPP / C++ / C Code:
Note that i havent included some advanced concepts. You can learn them as you proceed learning c++.(just forget these two lines) You can refer the links posted by Sokar to learn more. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| operator overloading | Krandygrl00 | C++ Forum | 5 | 06-Jul-2005 17:01 |
| Inheritance Project .. | Qatar | C++ Forum | 1 | 28-Apr-2005 17:04 |
| inheritance problem | ap6118 | C++ Forum | 8 | 11-Apr-2005 11:24 |
| Inheritance problem | CaptnB | C++ Forum | 2 | 27-Jan-2005 08:09 |
| Operator overloading & inheritance | Imajica | C++ Forum | 1 | 15-Apr-2004 20:15 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The