Another beginner question

This is a discussion on Another beginner question within the C++ Programming forums, part of the General Programming Boards category; But they are functions...thats what I'm saying...except PlayerIO a(), thats me attempting to construct an object. You had said to ...

  1. #31
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    But they are functions...thats what I'm saying...except PlayerIO a(), thats me attempting to construct an object. You had said to construct an Object it was ObjectType o(parameters); but if I have no parameters shouldn't it just be ObjectType o();

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that's one of the interesting inconistencies that came out of trying to retrofit object orientation into an already existing language.

    How would you declare a function that takes no parameters that return an object of ClassA? If we assume this is:

    Code:
    ClassA func();
    then how do you expect the compiler to tell the difference from that and the "no parameters" construction, constructing a variable called func?

    Since one form already existed originally in C (the former), the latter had to be the one that differs. [A bit like the joke about the ship sending a message to the lighthouse to "move to your left" - it ain't no good trying to move something solid!]

    --
    Mats

  3. #33
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Ok well I took the parenthesis away and it worked, now to just clean up the rest of the program.

    I'm doing a lot of substring work, which is kind of annoying because it seems to not work the way I want it. For example, I have a line of text that looks like this:
    Code:
          Nick Swisher</td>      1B/0F</td>      266.9</td>
    After some annoying substring manipulation I get it down to this:
    Code:
    1B/0F</td>      266.9</td>
    I need to be able to realize that this line of text contains 1B and 0F so I tried this:
    Code:
    int secpos = line.find("/");
    int endpos = line.find("</td>");
    	if(secpos < endpos)//more than one pos
    	{
    		pos1 = line.substr(0,secpos);
    		pos2 = line.substr(secpos+1,endpos);
    	}
    	else pos1 = line.substr(0,endpos);
    If the / comes before the </td> it means I have more than one position, otherwise it'll pick up the / in </td> and there will only be one position. However,when I want to print out the positions it doesn't give me what I want. This line fore example prints "1B and 0F</t"

    I think I'm using substr wrong. I was thinking it'd be easier to just replace all of the occurences of </td> with something but I'm not sure how to do that either
    Last edited by jcafaro10; 07-31-2007 at 09:11 AM.

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are using substr wrong - the second parameter is the length, not the position to end at...

    --
    Mats

  5. #35
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Ok, that makes sense...does C++ have arraylists or lists of any kind like Java does?
    Last edited by jcafaro10; 07-31-2007 at 02:18 PM.

  6. #36
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Yes, the standard library has a bunch of containers. vector is a dynamic array, deque is a double-ended queue, list is a doubly linked list, map is a sorted map, set is a sorted map where the key and value are a single object, multimap and multiset are versions of map and set that allow duplicates. I might be forgetting some. There are also adapters that confine the behavior of one of the ones above to a specific interface, like stack or queue. There are not-quite standard containers called unordered_map, unordered_set, unordered_multimap and unordered_multiset that are not sorted and use a hash based implementation.

    For many needs, a vector is a good start. I'd try that first.

  7. #37
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I wanted to make the vector a variable in the headerfile for PlayerIO but then I have to include Player.h in PlayerIO.h, and then I get a lot of errors. Is this the best way to do that or should vectors not go into headerfiles
    Last edited by jcafaro10; 07-31-2007 at 03:51 PM.

  8. #38
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    This is how it's currently laid out, with comments to show how I want it to be laid out

    PlayerIO.h
    Code:
    #include <vector>
    #include "Player.h"
    using namespace std;
    
    class PlayerIO
    {
      vector<Player> allPlayers;
    	public:
    		PlayerIO();
    		void makePlayerList();
    		void sortList();
    };
    PlayerIO.cpp
    Code:
    #include <vector>
    #include "PlayerIO.h"
    #include "Player.h"
    using namespace std;
    
    PlayerIO::PlayerIO() 
    {
    
    }
    
    void PlayerIO::makePlayerList()
    {
      Player a(stuff);
      allPlayers.push_back(a);
    }
    
    void PlayerIO::sortList()
    {
      for(int i = 0; i<allPlayers.length; i++)
            stuff;
    }
    Last edited by jcafaro10; 07-31-2007 at 04:19 PM.

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, I take it the errors/warnings are simply because you include "player.h" twice? You need include-guards, as discussed previously in this thread.

    --
    Mats

  10. #40
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Error 1 error C2011: 'Player' : 'class' type redefinition

    When I actually make the changes I wanted to make (I'll edit that code) I get this error.
    I don't understand, the guards are so I don't include it more than once? But don't I need to because differetn classes need to use it? PlayerIO.cpp needs to include Player.h so it can add Players to a list of Players. PlayerIO.h needs to include Player.h so it can make an actual vector of Players.

    EDIT: OH! PlayerIO.cpp includes PlayerIO.h and Player.h but PlayerIO.h already includes Player.h...so I just didn't include Player.h in PlayerIO.cpp...problem solved
    Last edited by jcafaro10; 07-31-2007 at 04:28 PM.

  11. #41
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jcafaro10 View Post
    OH! PlayerIO.cpp includes PlayerIO.h and Player.h but PlayerIO.h already includes Player.h...so I just didn't include Player.h in PlayerIO.cpp...problem solved
    Yes, but the CORRECT way to solve that problem is to have include-guards so that the include file is taken only once, whether you include it once or a hundred times. Consider that somone may not KNOW that you shouldn't include player.h when using playerIO.h. Or someone writes a piece of code that uses player, then needs playerIO too, and adds playerIO.h to the .cpp file, and suddenly all h**l breaks loose in the form of errors and warnings.

    --
    Mats

Page 3 of 3 FirstFirst 123
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, 08:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 03:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 08:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM

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