I'm having a bit of a problem instantiating class objects in my program main function with respect to the syntax. Here are some code that illustrates my class declaration, method declaration, and driver:
Car.h:
Car.cppCode:#include "rt.h" <-- this has proven to work, and so is ActiveClass class Car:public ActiveClass { private: bool state; // suspended or resumed double speed; // speed of the vehicle string label; // label of the vehicle public: Car(bool s_input=TRUE, double vi = 0.0, string name = "This car"); // constructor void accelerate(); // accelerate by 1 kph/s void cruise(double cruise_speed); // cruise control double v_get() const; // speed accessor bool s_get() const; // state accessor string l_get() const; // label accessor int main(void *args); // main function };
driver.cpp:Code:#include "Car.h" #include <iostream> #include <string> using namespace std; Car::Car(bool s_input, double vi, string name) { // store state, speed and label of vehicle state = s_input; speed = vi; label = name; // deal with suspending or resuming the vehicle initially if(state) // Suspend { Suspend(); } else // Resume { Resume(); } } void Car::accelerate() { speed++; } void Car::cruise(double cruise_speed) { speed = cruise_speed; } double Car::v_get() const { return speed; } bool Car::s_get() const { return state; } string Car::l_get() const { return label; } int Car::main(void *args) { string state_output; if(s_get()) // Suspend { state_output = "suspended"; } else // Resume { state_output = "running"; } while(1) { system("cls"); cout << l_get() << " is " << state_output << " and travelling at " << v_get() << " kph \n"; SLEEP(200); } return 0; }
I have tried both methods in the driver (non-commented out portion and commented out code) to instantiate three objects. However, I have gotten a compiler error with the commented out code like below in Microsoft Visual C++ 6.0:Code:#include <iostream> #include <string> #include "Car.h" using namespace std; int main(void) { /*Car Impala = new Car(FALSE, 23.0, "Impala"); Car NineOneOne = new Car(TRUE, 0, "911"); Car ThisCar = new Car;*/ Car Impala(FALSE, 23.0, "Impala"); Car NineOneOne(TRUE, 0.0, "911"); Car ThisCar; Impala.WaitForThread() ; NineOneOne.WaitForThread() ; ThisCar.WaitForThread() ; cout << "Finished" << endl; return 0; }
and with the non-commented code:--------------------Configuration: car - Win32 Debug--------------------
Compiling...
driver.cpp
I:\Current Courses\EECE 314\projects\car\driver.cpp(15) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(15) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
I:\Current Courses\EECE 314\projects\car\driver.cpp(15) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(15) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
I:\Current Courses\EECE 314\projects\car\driver.cpp(15) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(15) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
I:\Current Courses\EECE 314\projects\car\driver.cpp(15) : warning C4800: 'class Car *' : forcing value to bool 'true' or 'false' (performance warning)
I:\Current Courses\EECE 314\projects\car\driver.cpp(16) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(16) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
I:\Current Courses\EECE 314\projects\car\driver.cpp(16) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(16) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
I:\Current Courses\EECE 314\projects\car\driver.cpp(16) : warning C4800: 'class Car *' : forcing value to bool 'true' or 'false' (performance warning)
I:\Current Courses\EECE 314\projects\car\driver.cpp(17) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(17) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
I:\Current Courses\EECE 314\projects\car\driver.cpp(17) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(17) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
I:\Current Courses\EECE 314\projects\car\driver.cpp(17) : warning C4800: 'class Car *' : forcing value to bool 'true' or 'false' (performance warning)
Error executing cl.exe.
car.exe - 7 error(s), 10 warning(s)
I'm getting quite lost with the signature when instantiating the class objects. Any suggestions?--------------------Configuration: car - Win32 Debug--------------------
Compiling...
driver.cpp
I:\Current Courses\EECE 314\projects\car\driver.cpp(19) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(19) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
I:\Current Courses\EECE 314\projects\car\driver.cpp(19) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(19) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
I:\Current Courses\EECE 314\projects\car\driver.cpp(20) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(20) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
I:\Current Courses\EECE 314\projects\car\driver.cpp(21) : error C2259: 'Car' : cannot instantiate abstract class due to following members:
i:\current courses\eece 314\projects\car\car.h(9) : see declaration of 'Car'
I:\Current Courses\EECE 314\projects\car\driver.cpp(21) : warning C4259: 'int __thiscall ActiveClass::main(void)' : pure virtual function was not defined
i:\current courses\eece 314\projects\car\rt.h(477) : see declaration of 'main'
Error executing cl.exe.
car.exe - 4 error(s), 4 warning(s)