•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,610 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,507 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 918 | Replies: 2
![]() |
•
•
Join Date: Aug 2004
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
I'm having trouble with the (dynamic_cast<Truck*>(vptr[k])) -> DOTLicense() part.
If my numOfVehicles = 1 it works fine, but if numOfVehicles > 1 I get core dump error. I can remove the dynamic_cast statement and it works for numOfVehicles > 1. Any help or advise on what I'm doing wrong is appreciated.
If my numOfVehicles = 1 it works fine, but if numOfVehicles > 1 I get core dump error. I can remove the dynamic_cast statement and it works for numOfVehicles > 1. Any help or advise on what I'm doing wrong is appreciated.
int main()
{
const size_t maxNumOfVehicles(100);
const size_t maxSnSize(20);
const size_t maxDotSize(20);
size_t numOfVehicles;
std::cout << "Tracker started..." << std::endl;
// get the number of vehicles
std::cin >> numOfVehicles;
// Create an array of vehicle pointers
Vehicle* vptr[maxNumOfVehicles];
VehicleType v; // used to return an enum value
char sn [maxSnSize];
unsigned int passengerCap;
char dotLicense[maxDotSize];
float d1;
float d2;
float d3;
while((numOfVehicles != 0) && (numOfVehicles < maxNumOfVehicles)) // Begin
{
// Process the vehicle segment
for(size_t i=0;i<numOfVehicles;++i) // Begin first for loop
{
// get the vehicle serial number and check it
std::cin >> std::setw(maxSnSize) >> sn;
v = Vehicle::SnDecode(sn);
if(v == badSn) break; // don't process - can't make vehicle object
// get the passenger capacity
std::cin >> passengerCap;
switch(v) // Begin switch
{
case vehicle:
vptr[i] = new Vehicle(sn, passengerCap);
break;
case car:
vptr[i] = new Car(sn, passengerCap);
break;
case truck:
std::cin >> std::setw(maxDotSize) >> dotLicense;
vptr[i] = new Truck(sn, passengerCap, dotLicense);
break;
case van:
std::cin >> std::setw(maxDotSize) >> dotLicense;
std::cin >> d1;
std::cin >> d2;
std::cin >> d3;
vptr[i] = new Van(sn, passengerCap, dotLicense, d1, d2, d3);
break;
case tanker:
std::cin >> std::setw(maxDotSize) >> dotLicense;
std::cin >> d1;
std::cin >> d2;
vptr[i] = new Tanker(sn, passengerCap, dotLicense, d1, d2);
break;
case flatbed:
std::cin >> std::setw(maxDotSize) >> dotLicense;
std::cin >> d1;
std::cin >> d2;
vptr[i] = new Flatbed(sn, passengerCap, dotLicense, d1, d2);
break;
default:
std::cerr << "**Error: bad serial number passed to decision logic\n";
break;
} // end switch
} // end first for loop
// Send Report to Std. Output
std::cout << "Type Pass Cap Load Cap DOT License Serial Number\n";
std::cout << "---- -------- -------- ----------- -------------\n";
for(size_t k=0;k<numOfVehicles;++k) // begin second for loop
{
if(truck <= v) // begin if statement
{
std::cout << " " << vptr[k] -> ShortName() << " " << vptr[k] -> PassengerCapacity() << " " << vptr[k] -> LoadCapacity() << " " << (dynamic_cast<Truck*>(vptr[k])) -> DOTLicense() << " " << vptr[k] -> SerialNumber() << "\n";
}
else
{
std::cout << " " << vptr[k] -> ShortName() << " " << vptr[k] -> PassengerCapacity() << " " << vptr[k] -> LoadCapacity() << " (NA) " << vptr[k] -> SerialNumber() << "\n";
} // end if statement
delete vptr[k];
} // end second for loop
// Finished processing vehicle segment. Wait for next segment.
std::cout << "Tracker started..." << std::endl;
std::cin >> numOfVehicles;
} // end while loop
// Bye Bye message
std::cout << "...Thank you for using Tracker.\n";
return 0;
}•
•
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation:
Rep Power: 9
Solved Threads: 163
if(truck <= v) // begin if statement
{
... (dynamic_cast<Truck*>(vptr[k])) -> DOTLicense() ...
}
else
{
...
} // end if statementassuming that the variable v has the right value of the VehicleType enum,
if( truck <= v ) would be true for things other than a truck. and if that happens, (dynamic_cast<Truck*>(vptr[k])) would result in a zero pointer which you are trying to dereference. that the failure occurs at precisely the second element of the array is probably because you are using the same inputs every time to test your code. to debug the current code, print out the following:Truck* pointer = (dynamic_cast<Truck*>(vptr[k])) ;
std::cout << "pointer: " << pointer
<< " type of object: " << typeid( *(vptr[k]) ).name() << '\n' ;unless you are trying to learn about dynamic_casts, consider using virtual functions instead of the type variable and casts.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Using pointers or reference for two dimensional array input (C++)
- select sort using array of pointers---getting wrong values. (C++)
- array, pointers, and strings in C++ please help me asap please....... (C++)
- Array of pointers to objects (C++)
- Array of pointers (C++)
- Passing 2D Array of Pointers into a function (C++)
- Copying 2D array by pointers (C)
Other Threads in the C++ Forum
- Previous Thread: DYNCALL(writeData)((UCHAR *)
- Next Thread: Help with error in using iterator in a for loop



Linear Mode