![]() |
|
#1
|
|||
|
|||
inheritance problemhello everyone. Sorry for being away for a while, the new job is a killer.
here i am now with a little bit of an inheritance problem. Actually it's more of a constructor problem. i am trying to intialize a private const object in the constructor, but no matter what format i use to try to do so, ms visual studio still rejects it. CPP / C++ / C Code:
in case someone needs to take a peek at the class itself, here it is. CPP / C++ / C Code:
what do u recommend? |
|||
|
#2
|
|||
|
|||
|
numberOfDoors, numberOfCylinders, and transmissionType are declared const. you can't make assignments to const variables, and this extends to class members. so either remove the const attribute from those variables, or use a member initialization list. they are the only way to initialize const members, but as a small tradeoff they make your constructor a little more cryptic. using an initialization list instead of assignments, your constructor would be rewritten like this:
CPP / C++ / C Code:
your other problem is that you were trying to assign the member variables as if they were static. (declaring a class member variable static means that there is only one instance of the variable for all objects of the class. this can be useful, but I am almost positive it is not what you want.) anyways, non-static class member variables are accessed in the class's member functions in one of two ways: (1) as if they were global variables. (i.e. just use them) (2) through the this pointer (i.e. this->myVar). the this pointer is a parameter that the compiler adds to every class member function. it is transparent to the programmer, but it is what allows classes to be compiled back into regular C. (in other words, the class is separated into a structure containing the variables and a set of functions with their names mangled like __class___m_func_whatever(), that all receive a pointer to the structure they are being called on as the this pointer). |
|
#3
|
|||
|
|||
thanks a bunchthat really helped, thank you. no it was not my desire to make those objects static, but it is the book's desire...
i really appreciate it. incidently, i created another class called "Truck" to inherit from class vehicle, but as i created the constructor for the class, as follows.... CPP / C++ / C Code:
truck.cpp(6) : error C2512: 'Vehicle' : no appropriate default constructor available if anyone can help and need to take a look at class vehicle it is posted before. |
|
#4
|
|||
|
|||
|
I would have probably have to see the Truck class source code, but my guess is you will have to add a
CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
class TruckQuote:
CPP / C++ / C Code:
that is the truck class, and i also tried adding Vehicle():\; to class Vehicle wich stopped the compiler from generating a syntax error, but when i try to build the program it shows this truck.obj : error LNK2001: unresolved external symbol "public: __thiscall Vehicle::Vehicle(void)" (??0Vehicle@@QAE@XZ) |
|
#6
|
||||
|
||||
|
Hi.
You'll have to implement your default constructor in the .cpp file. CPP / C++ / C Code:
When you build an object of class Vehicle, the default constructor will be called (unless you specify otherwise), and the compiler can't find it's definition. Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#7
|
|||
|
|||
|
LuciWiz is right, although if you don't want to take up that space in the .cpp file you can just define the def. constructor in the class definition (in the .h file):
CPP / C++ / C Code:
(1) defining them inside the class like that makes the class definition harder to read (2) they are implicitly inline functions when defined inside the class. that means that whenever they are called, the code is simply inserted into the calling function. so if the function is long and involved, this will slow down the program. but for short functions, it can actually add speed because branching to a function causes a small amount of overhead. Last edited by LuciWiz : 08-Apr-2005 at 15:45.
Reason: Fixed broken [c++] tag
|
|
#8
|
|||
|
|||
thank you so much guysyou guys were really helpful, thank you so much
|
|
#9
|
|||
|
|||
now main problemi have created a main function to test the program and Vehicle and Taxi objects are running fine, but for some reason the constructor for class Truck is not being accessed.
this how my main looks like CPP / C++ / C Code:
class truck is as follows CPP / C++ / C Code:
with the follwoing defenition CPP / C++ / C Code:
i am getting weird numbers for an output and looks clearly that the constructor has not ben initiated. can anyone advice me? |
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Apache / PHP problem, maybe output length? | HaganeNoKokoro | Apache Web Server Forum | 3 | 07-Aug-2008 05:42 |
| Please help! Insert into database problem | robsmith | MySQL / PHP Forum | 1 | 24-Apr-2005 04:44 |
| Inheritance problem | CaptnB | C++ Forum | 2 | 27-Jan-2005 09:09 |
| File Serialize's problem | stanely | MS Visual C++ / MFC Forum | 2 | 08-Dec-2004 10:54 |
| Another FX 5600 problem (but with details that might shed light on this) | BobDaDuck | Computer Hardware Forum | 2 | 16-Apr-2004 08:53 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The