Thread: Need help with classes.

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    65

    Need help with classes.

    Hello, again :P

    I´ve got a problem with my class, im writing the class in a .h file and having inte main() on the .cpp (im very new to this so i´m writing down everything i have done)

    I want the program just to ask a simple question about what the name of the game shall be, i have done it before, when the class and int main() both were on the .cpp file.

    When I try to do the same, I get the error message : Error c2512: 'game': no appropriate default constructor available.

    Here is the code.

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>
    #include <conio.h>
    #include <string>
    #include "ClassGame.h"
    using namespace std;
    
    int main()
    {
    	string nameOfGame;
    	game myGame; //Guessing that it is here the problem is starting.
    	
    	cout << "Please enter the name of the game\n: " << myGame.getGameName();
    	getline(cin,nameOfGame);
    	myGame.setGameName(nameOfGame);
    	myGame.displayMessage();
    	getch();
    	return 0;
    }
    and here is the .h file
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <iostream>
    #include <string>
    using namespace std;
    
    class game
    
    {
    public:
    	game(string name)
    	{
    		setGameName(name);
    	}
    	void setGameName(string name)
    	{
    		gameName = name;
    	}
    	string getGameName()
    	{
    		return gameName;
    	}
    	void displayMessage()
    	{
    		cout << "This is a game called: " << getGameName();
    	}
    private:
    	string gameName;
    };

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So the only constructor you have is one that takes a string. So you must declare your variable as
    Code:
    game MyGame("temporary name")
    or create a constructor that doesn't take a string argument.

    (And there's nothing here about header files; if you had this all in one file it still wouldn't work.)

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Usually, the compiler supplies a "default" constructor for your class, that takes no arguments.
    This, in turn, allows you to just create an instance of the class by typing out its name and a name of the instance (ie myclass class.
    However, when you provide a custom constructor for your class, the compiler won't generate a default one for you, so YOU need to supply one if you need one.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Hmm, have to read more I guess ^^

    cuz im understanding like 10&#37; of what you just said

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    An example, then.
    This is a class named foo.
    Code:
    class foo { };
    foo is a class blueprint. To use it, we must first create the object mentioned in the blueprint. In C++, we call it creating an instance (of the object mentioned in the blueprint).

    You can compare this to the class blueprint being a car blueprint. We can't use the blueprint to drive, now can we? First, we must build a car from the blueprint. This is called creating an instance.

    Now, when we create an instance of a class, it must first be constructed from its blueprint. This is the work of the constructor (in other words, the constructor makes an object from a blueprint).

    You can compare the constructor as a factory. The factory takes a order for your blueprint and builds a car for you.

    If our above example, foo is actually empty. It has no constructor. In this case, the compiler is kind enough to add a "default" constructor. It just makes an object from the blueprint and nothing more.
    We can create more constructors, that do special things.

    You can compare this to a special factory that builds a car and maybe colors it or things like that.

    However, when we add a specialized constructor, the compiler is not kind enough to add its "default" constructor for us! We need to add it ourselves for it to work.

    Constructors often take different amount of information (like what color do you want your car to be?). To pass along this information when creating an instance, we use the syntax:
    Code:
    class car
    {
        car(std::string color) {}
    };
    
    int main()
    {
        car my_car2; // Error
        car my_car("red"); // OK
    }
    (In this example, we create a car whose color should be red, which is what we tell the constructor.)

    Note again that when we do this, the default constructor is no longer there.
    A default constructor looks like:

    Code:
    class car
    {
        car() {}
    };
    
    int main()
    {
        car my_car; // OK
        car my_car("red"); // Error
    }
    Because it takes no arguments, we can create an instance of it like this:
    Code:
    int main()
    {
        car my_car;
    }
    If we want both a default constructor and a specialized constructor, we must add the default constructor ourselves:
    Code:
    class car
    {
        car() {}
        car(std::string color) {}
    };
    
    int main()
    {
        car my_car("red"); // OK
        car my_car2; // OK
    }
    Last edited by Elysia; 10-03-2008 at 08:52 AM.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Hehe thanks for the explanation Elysia
    I do understand some more, but back in to the books!

    Appriciate the help, miss.I know it all

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    So all I had to do, was to add..
    Code:
    game()
    {}
    In the header file
    great ^^

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All you had to do was add a default constructor...
    Note that they, too, can be split up, like the rest of class functions... they aren't special in such a way.
    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
    Oct 2008
    Posts
    65
    This program is special for me atlest ;P
    took me 3 hours to finaly understand what you were saying earlier and some reading on that, im happy =P

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    ^ I think that is worthy of being added to your sig, Elysia. Even I felt touched by that one.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dunno, I'm out of space.
    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.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Drats.. well I guess there is no need to further show gratitude. I personally always try to choose my words carefully when complimenting my peers here since you in particular like the notoriety. For whatever its worth, I do respect your level of conviction in the things that you say.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM