Thread: class instantiation

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    class instantiation

    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:
    Code:
    #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
    };
    Car.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;
    }
    driver.cpp:
    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;
    }
    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:
    --------------------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)
    and with the non-commented code:
    --------------------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)
    I'm getting quite lost with the signature when instantiating the class objects. Any suggestions?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It appears to me that there is a Car::main(void) that you should declare, but you declared Car::main(void *args).

    And you should be using "true" and "false" not TRUE and FALSE for bool values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    new is for pointers, not regular objects, so that's why the commented out part doesn't work.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed, but you can also omit the "void". I don't know why Microsoft keeps insisting to add "void" to every C++ function
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    Indeed, but you can also omit the "void". I don't know why Microsoft keeps insisting to add "void" to every C++ function
    Maybe because they have Extern "C" linkage? Or you mean some other (not Win32) functions?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I mean, all functions. If you, for example, use the MFC wizard or class wizard, it automatically adds "void" to empty function lists.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Because it's less ambiguous. For C++ it's not a big deal, but for C
    Code:
    int main(void)
    is different than
    Code:
    int main()
    And adding void to empty parameter lists has nothing to do with Microsoft. Pretty much any C++ programmer coming from a C background will do it. And apparently you do it too if your sig is to be trusted.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't do it. I find it annoying. Classes are C++, so what's the big deal of adding void? Same with MFC - it's classes! C++! Not C!
    The compiler itself also adds void to anything it outputs, whether it be C or C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Quote Originally Posted by cpjust View Post
    new is for pointers, not regular objects, so that's why the commented out part doesn't work.
    I actually tried the object instantiation and dynamic memory allocation methods, but both of them don't work either.

  10. #10
    coder
    Join Date
    Feb 2008
    Posts
    127
    It's a hard big deal trying to debug a so long list of errors.
    You might break your code and debug it step by step, compiling little pieces.
    Did you write the code you posted and then compiled it all in one time?
    If so, I'd say it's a bad way to work, expecially if you are not really expert.

    Personally, I'm not expert and neither a newbie, but I'd had compiled a code like yours at least 20 times, even more.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Well, I initially had nothing in my driver .cpp file and my Car class compiled with no complaints. The error happened right after I instantiate the class.

  12. #12
    coder
    Join Date
    Feb 2008
    Posts
    127
    Well, so I tell what I would do if I was you:
    I'd /*comment*/ almost all the class code, but keeping active the object instantiation in driver.cpp.
    In other words I'd try to get a condition where the compiler says no errors.
    Then, step by step, I will uncomment small parts of code and see what happens:
    actually your error list is too long

    edit: hmm nevermind that suggestion... I didn't pay enough attention to see the matter is the instantiation
    Last edited by carlorfeo; 02-11-2008 at 02:17 PM.

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I actually found the problem now. Thanks for your help.

  14. #14
    coder
    Join Date
    Feb 2008
    Posts
    127
    Please tell us so :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM