Thread: Class has no constructors

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Class has no constructors

    I have the following codes:

    main.cpp

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    #include "player.h"
    
    int main()
    {
    	string tempName;
    	bool success=0;		//flag for storing whether operation is successful
    	cout <<"Please input player's name: ";
    	cin >>tempName;		// get player's name from keyboard input
    	player *human;		// use pointer of base class, convenience for polymorphism
    	int tempJob;		// temp choice for job selection
    	do
    	{
    		cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl;
    		cin>>tempJob;
    		system("cls");		// clear the screen
    		switch(tempJob)
    		{
    		case 1:
    /*error here*/			human=new swordsman(1,tempName);	// create the character with user inputted name and job
    			success=1;		// operation succeed
    			break;
    		default:
    			break;				// In this case, success=0, character creation failed
    		}
    	}while(success!=1);
    
    return 0; }
    swordsman.h

    Code:
    #include "player.h"
    
    class swordsman : public player		// subclass swordsman publicly inherited from base player
    {
    public:
    	swordsman(int lv_in=1, string name_in="Not Given");
    		// constructor with default level of 1 and name of "Not given"
    	void isLevelUp();
    	bool attack (player &p);
    	bool specialatt(player &p);
    		/* These three are derived from the pure virtual functions of base class
    		   The definition of them will be given in this subclass. */
    	void AI(player &p);				// Computer opponent
    };
    The constructor is defined in swordsman.cpp as:

    Code:
    swordsman::swordsman(int lv_in, string name_in)
    {
    	role=sw;	// enumerate type of job
    	LV=lv_in;
    	name=name_in;
    	
    	// Initialising the character's properties, based on his level
    	HPmax=150+8*(LV-1);		// HP increases 8 point2 per level
    	HP=HPmax;
    	MPmax=75+2*(LV-1);		// MP increases 2 points per level
    	MP=MPmax;
    	AP=25+4*(LV-1);			// AP increases 4 points per level
    	DP=25+4*(LV-1);			// DP increases 4 points per level
    	speed=25+2*(LV-1);		// speed increases 2 points per level
    	
    	playerdeath=0;
    	EXP=LV*LV*75;
    	bag.set(lv_in, lv_in);
    }
    I have a problem creating a swordsman object dynamically because apparently the constructor cannot be found. What am I doing wrong?
    Last edited by 843; 04-17-2011 at 07:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class constructors
    By c++.prog.newbie in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2006, 09:15 PM
  2. Class Constructors
    By d_heyzie in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2006, 04:26 AM
  3. Replies: 4
    Last Post: 12-29-2002, 12:29 AM
  4. help with class and constructors
    By stautze in forum C++ Programming
    Replies: 14
    Last Post: 10-16-2002, 07:56 PM
  5. Constructors of a class
    By casanova0o7 in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2002, 03:46 PM