Class has no constructors

This is a discussion on Class has no constructors within the C++ Programming forums, part of the General Programming Boards category; I have the following codes: main.cpp Code: #include <iostream> #include <string> using namespace std; #include "player.h" int main() { string ...

  1. #1
    843
    843 is offline
    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 08:24 AM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You don't seem to include swordsman.h above main. Try that first. If it doesn't work, maybe posting the exact error may help because most people here don't like to guess Your compiler already knows what's wrong and you didn't tell us.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    It's okay, I just fixed it. Basically, in all the cpp codes I #include the corresponding headers, and in the main.cpp I #include both player.cpp and swordsman.cpp. This is the ONLY way I got the code to work, even though in the given codes there was no mention of the headers being erroneous.

    Originally, the #includes were something like this:

    Code:
    player.cpp
    player.h includes container.h
    main.cpp includes swordsman.h
    swordsman.cpp
    swordsman.h includes player.h
    which returned a lot of linker errors.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    #include'ing a .cpp file is almost invariably the wrong solution to any problem.

    If you are having to #include .cpp files to get your program to work, you need to look at your project settings (through your development environment). Your project needs to have all of the .cpp files listed, so they may all be compiled and linked.

    This is exactly the same problem as you described in another thread, but apparently you deem it inappropriate to look at or bother to understand answers you have already received.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    I got it! I was modifying the files as standalone files. I have never bothered with project files before, so the thought never crossed my mind.

    It's not exactly the same problem, because for this one I changed the swordsman.h to player.h and the linker errors disappeared (though it produced other errors). I did look through (and understood) your answer but it didn't work because that's not the solution to my problem. There was no mistake with the function prototypes and definitions.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    Quote Originally Posted by 843 View Post
    There was no mistake with the function prototypes and definitions.
    Which means the problem is with your project settings. Which I pointed out in your other thread, but you simply overlooked it.

    The fact you had both coding errors and errors with your project means you had two problems to solve. Solving only one such problem does not eliminate the other problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  7. #7
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Right, my bad.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    The code has some indentation issues.
    Also, you are forgetting to delete what you new.
    But you could also use smart pointers to avoid having to delete (SourceForge.net: Raw pointer issues - cpwiki).
    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.

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, 10:15 PM
  2. Class Constructors
    By d_heyzie in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2006, 05: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, 08:56 PM
  5. Constructors of a class
    By casanova0o7 in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2002, 03:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21