Another beginner question

This is a discussion on Another beginner question within the C++ Programming forums, part of the General Programming Boards category; Your name data member is of type string , so in order for this to all work either: 1) The ...

  1. #16
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Your name data member is of type string, so in order for this to all work either:

    1) The header needs to #include the string header, or...
    2) Any source file that #includes this class header must also #include the string header before it #includes the class header.

    You also need to be mindful of any namespace issues as well.

    Maybe:
    Code:
    #include <string>
    
    class Player 
    {
    	double points,value;
    	std::string name, pos;
    	public:
    		Player (std::string,std::string,double);
    };
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You also need header include guards. You also need more code in main to create variables named name, pos and ppoints. hk_mp5kpdw's code was an example, not meant to be used directly with your class and compiled immediately. You ahve more work to do.

  3. #18
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I don't know what header guards are but I'm close...The variables are all defined in my main
    class. I'm gettin an error error C2664: 'Player::Player(std::string,std::string,double)' : cannot convert parameter 3 from 'std::string' to 'double'

    Player.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "Player.h"
    using namespace std;
    
    Player::Player (string n, string p, double pts) 
    {
      name = n;
      pos = p;
      points = pts;
    }
    Player.h
    Code:
    #include <string>
    
    using namespace std;
    
    class Player 
    {
    	double points,value;
    	string name, pos;
    	public:
    		Player (string,string,double);
    };
    main
    Code:
    Player a(name, pos, ppoints);

  4. #19
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    that was a dumb error, nvm working now, i'll be back when it breaks again

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I'm gettin an error error C2664
    Apparently ppoints is a string variable, but you should be passing a double.

    >> I don't know what header guards are but I'm close.
    Look them up. You will need them to get this program working.

  6. #21
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    How do you properly provide implementation. Apparently this is wrong haha

    Code:
    #include <iostream>
    #include <string>
    #include "Player.h"
    using namespace std;
    
    Player::Player (string n, string p, double pts) 
    {
      name = n;
      pos = p;
      points = pts;
    }
    
    Player::void calcPoints()
    {
    	value = 10;
    }
    
    Player::double getPoints()
    {
    	return value;
    }

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <iostream>
    #include <string>
    #include "Player.h"
    using namespace std;
    
    Player::Player (string n, string p, double pts) 
    {
      name = n;
      pos = p;
      points = pts;
    }
    
    void Player::calcPoints()
    {
        value = 10;
    }
    
    double Player::getPoints()
    {
        return value;
    }

  8. #23
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    ah sweet, ok, one(two) more errors. It says I need a class/struct/union
    Code:
    int main () 
    {
      PlayerIO a();
      a.makeFile();
      a.makePlayerList();
      return 0;
    }

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Include guards is when you have a mechanism to prevent the same header-file having it's content included multiple times - most of the time, once is perfectly fine.

    Example: myclass.h
    Code:
    #ifndef __MYCLASS_H__
    #define __MYCLASS_H__
    
    class myclass {
    ....
    }
    ...
    ...
    ...
    #endif
    It is good practice to ALWAYS do this for ALL header-files, because somtimes (in larger projects) header-files are included by other header files. Let's say we have a "baseclass" that, that is then needed by classA and classB, and in our "main.c", we need both classA and classB, we want to include both "classA.h" and "classB.h". At the same time, we may not want to tell everyone using "classA" that they need to FIRST include "baseclass.h". So we add "baseclass.h" to both "classA.h" and "classB.h" - but without guarding against multiple includes, the content of "baseclass" would now be twice in the same compile - that's generally a bad idea. This is where include-guards are needed.

    --
    Mats

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Note that identifiers with double underscores are reserved for the implementation, so it is best to not use them in your include guards. MYCLASS_H would be better.


    >> It says I need a class/struct/union
    What is PlayerIO? You have a class called Player, but the compiler doesn't recognize PlayerIO. Don't forget to post the entire text of the error message(s) to make it easier for us to help. Also, don't put empty prentheses after the variable when declaring it, the compiler will think it is a function declaration.

  11. #26
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    It is a function declaration...Error 1 error C2228: left of '.makeFile' must have class/struct/union c:\Documents and Settings\chyron\Desktop\jp\TestApp\TestApp\TestApp .cpp 10

    I do have a PlayerIO class, I just made it. It has a function makeFile

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And do you include the correct headerfile? Do you use the correct spelling everywhere?

    --
    Mats

  13. #28
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    yes, this is my main class. Its very short. I included both of the headers. They both exist

    Code:
    #include <string>
    #include "Player.h"
    #include "PlayerIO.h"
    using namespace std;
    
    int main () 
    {
      PlayerIO a();
      a.makeFile();
      a.makePlayerList();
      
      return 0;
    }

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Didn't DaveD say something like "Don't put parenthesis behind the variable name, the compiler will think it's a function"? I clearly seem to remember that. Read your code again...

    --
    Mats

  15. #30
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Didn't DaveD say something like "Don't put parenthesis behind the variable name, the compiler will think it's a function"?
    I added that as an edit, sorry if it was too late to be seen.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 07:39 PM

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