Not able to access private data members

This is a discussion on Not able to access private data members within the C++ Programming forums, part of the General Programming Boards category; Hi, Thanks. I was going on a hint the instructor gave us: Code: friend ostream& operator <<(ostream&, vector <length>); ...from ...

  1. #16
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Hi, Thanks. I was going on a hint the instructor gave us:
    Code:
    friend ostream& operator <<(ostream&, vector <length>);
    ...from another example. He then went on:
    Code:
    for(int i=0;i<=v.size()-1;i++)
    {
      os<<v[i].feet;
      os<<v[i].inches;
      return os;
    }
    I was trying to apply something like that here...
    I'll try what you said...

  2. #17
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You can still use that idea, but it must be in addition to what I've suggested, or you'll have to change other things to get it to work. When I said, "you can't just use >> to input into a vector", I meant with your current code. Your instructor's hint will help you so that you can do it. When I added, "you'll probably just want to loop getting the value for one card at a time and pushing it back into the vector", I meant something like your instructor's hint, in addition to the specific change I suggested you make.

    And by the way, I hope your instructor's hint was more like this:
    Code:
    for(int i=0;i<=v.size()-1;i++)
    {
      os<<v[i].feet;
      os<<v[i].inches;
    }
    return os;
    Otherwise, it wouldn't make as much sense.

  3. #18
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    And by the way, I hope your instructor's hint was more like this:
    You are absolutely right, it was.

  4. #19
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Stuck here:
    Code:
    ifstream& operator >>(ifstream& ifs, BunchOfCards& bcds)
    {
      for(int i=0;i<bcds.mSomeCards.size();i++)
      {
        ifs>>bcds.mSomeCards.push_back();
      }
      return ifs;
    }

  5. #20
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    There is a difference between input and output. Your instructor's hint gives an example of output. However, your operator>> is used for input. Some of the differences between the two are:
    • With output, you know how many objects (Cards) you will output. With input, you must just input them until there is no more. It is possible that your assignment specifies exactly how many should be input, but otherwise, you cannot use a for loop for input because you must keep going while the input is still valid.
    • With output, you access the existing objects in the vector using [], but with input those objects don't exist yet. You have already made that modification by using push_back.
    So, some advice is to read data into a temporary Card. If reading in data fails, then you are done and you can return from the function. Otherwise, if you are able to read all the data into the temporary Card, then you can push back that card into the vector at that time. Continue until you fail to read in a valid card.

  6. #21
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    I am very confused. The instructions I was given are to write an overloaded >> operator for the Card class, and then an overloaded >> for the BunchOfCards class so that the just 2 lines of code like this:
    BunchOfCards aDeck;
    aStreamOfCards >> aDeck ;
    will stuff the aDeck object from aStreamOfCards. (assuming the precondition that aStreamOfCards is a properly declared ifstream object that is attached to the file I created. I have done this in main():
    Code:
    int main()
    {
      BunchOfCards aDeck;
      ifstream aStreamOfCards;
      aStreamOfCards.open("Deck.dat");
      aStreamOfCards>>aDeck;
      return 0;
    }
    Now I am trying to overload the operator to work with an object, but I'm just a beginner who is learning. The instructor has taught all of the things that you are refering to, but I have only implemented them a little bit in practice. Thanks

  7. #22
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You are very close, in my opinion, to getting it right, but I don't want to just give you the answer. Since you are so close, I will nudge you in the right direction again. Here is the code I have gathered from what you have so far. It doesn't have everything that you have, I'm sure, but it is enough for that main function to work. Notice that I changed all the ifstream's to istream (no 'f') because that way it will work with any stream, not just file streams. That is how you had it in your first post, so I am assuming that it is true throughout.

    So really all you need is something to replace SOMETHING:
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    using namespace std;
    
    class Card
    {
      public:
        friend istream& operator >>(istream& is, Card& cd);  
      private:
        string mSuit;
        int mValue;
    };
    
    istream& operator >> (istream& is, Card& cd)
    {
      is>>cd.mSuit>>cd.mValue;
      return is;
    }
    
    class BunchOfCards
    {
      public:
        friend istream& operator >>(istream& is, BunchOfCards& bcds);
      private:
        vector <Card> mSomeCards;
    };
    
    istream& operator >>(istream& is, BunchOfCards& bcds)
    {
      // SOMETHING
      return is;
    }
    
    int main()
    {
      BunchOfCards aDeck;
      ifstream aStreamOfCards;
      aStreamOfCards.open("Deck.dat");
      aStreamOfCards>>aDeck;
      return 0;
    }
    The point you must understand, is that you have already written the input operator for a Card object. That means that you can simply write:

    Card cd;
    is >> cd;

    and the card will automatically have the data from the input stream stored inside of it. At that point, you can do whatever you want with the card, including adding it to the list in a deck of cards.

    The other part is being able to read in one card at a time while there is still data in the input stream. By the way, just a little side note that might help you:is >> cd evaluates to false if it is unable to read in any more cards (or if there is some other error).

    Anyway, just replace SOMETHING with something based on that information. I was able to get that code to work by adding only three real lines of code. See if you can do the same.

  8. #23
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Thanks a lot. Great to know that I am close. Yes, the instructor taught us how to use a while loop in the way that you have described. I have work today and class tonight, but I will be back trying to solve the problem this evening.

  9. #24
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    I'm back. I did this:
    Code:
    ifstream& operator >>(ifstream& ifs, BunchOfCards& bcds)
    {
      while(ifs>>bcds)
      {
        bcds.mSomeCards.push_back();
      }
      return ifs;
    }
    ...and it compiles OK. It closes and will not run though because of an error. I am not sure what to supply as an item to push_back() though, as in push_back(item). Thanks.

  10. #25
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Hmmm, I think you better say in english how you think the input operator should work for a bunch of cards. Maybe this will help you with that:
    Quote Originally Posted by jlou
    The point you must understand, is that you have already written the input operator for a Card object. That means that you can simply write:

    Card cd;
    is >> cd;

    and the card will automatically have the data from the input stream stored inside of it. At that point, you can do whatever you want with the card, including adding it to the list in a deck of cards.

  11. #26
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Well, to tell you the truth, I'm not sure. This is the first time I have seen any of this. I'm not alone: the whole class is stunned. This is supposed to be an entry-level course. The instructor gave us a review class tonight for the test. I didn't even have time to ask my question. He reiterated the following: ...he uses a "Length Class," and a "VectorLength Class" as follows:
    Code:
    class Length
    {
      public:
    
      private:
        int feet;
        int inches;
    };
    
    class VectorLength
    {
      private:
        vector <Length> Ls;
    };
    
    int main()
    {
      VectorLength VL;
      cout<<VL;
      return 0;
    }
    
    ostream& operator <<(ostream& os, vector <Length> Ls)
    {
      for(...
      {
        os<<Ls[i];
      }
      return os;
    }
    I have not even been able to get something like this figured out. Furthermore, we have an assignment which is "out of this world." It asks us to write a file as follows:
    Code:
    1 Hearts
    2 Hearts
    3 Hearts
    ...
    10 Hearts
    1 Spades
    2 Spades
    ...
    ...and attach an overloaded ifstream object (or something) and load the vector with it (I guess). This is only the first step... believe it or not! Thanks again.

  12. #27
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    ...OK, let me try another approach. Maybe if I ask this question, some knowledgable soul will enlighten this very willing beginner. Where is this vector loaded? I tried loading it here:
    Code:
    ifstream& operator >>(ifstream& ifs, BunchOfCards& bcds)
    {
      for(int i=0;i<bcds.mSomeCards.size();i++)
      {
        ifs>>bcds.mSomeCards.push_back();
      }
      return ifs;
    }
    ...but I was told that I was, perhaps, heading down a dark alley.
    Let me reflect on another problem I encountered. This one is in many ways similar to the task I am now facing.
    When I was first being taught file streams, I was given a problem of inputting from a file. Let me give an example:
    Code:
    90 s
    79 j
    98 k
    I named this file MyFile.dat, and put it into the folder where I was running my program. I discovered after much trial and error, that unless I used something like this:
    Code:
      ifstream GradesStream;
      int aGrade;
      string aName;
      while(GradesStream>>aGrade>>aName)
      ...
    I would not be able to load the information from the file. I tried for a long time in vain with code such as:
    Code:
      ifstream GradesStream;
      int aGrade;
      while(GradesStream>>aGrade)
      ...
    ...and as you can imagine, it would not run. Now, I am prepared to use the same strategy here. Again the question comes: Where is the vector loaded? Is it loaded in main()?
    Code:
    int main()
    {
      BunchOfCards aDeck;
      ifstream aStreamOfCards;
      aStreamOfCards.open("Deck.dat");
      aStreamOfCards>>aDeck;
      while(aStreamOfCards>>aDeck)
      {...
      return 0;
    }
    Thanks again in advance.

  13. #28
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    smitsky, I understand you are trying to understand this in your own way, but I must impress upon you how close you were to achieving your desired result. I was attempting to guide you those last few steps so that you could discover them on your own.

    If you look at the code I posted earlier, where I left SOMETHING to be done, you were oh so close to finishing the input operator. The problem, I fear, is that you are having trouble conceptualizing what is going on in these functions.

    The input operator, also known as operator>>, allows you to fill a variable with data. The data that goes into the variable depends on the type of that variable. If you said ifs >> myInt; where myInt had the type int, then myInt would be filled with an integer number. If you said ifs >> myFloat; where myFloat had the type float, then myFloat would be filled with a floating point number. If you said ifs >> myString; where myString had the type string, then myString would be filled with a string of characters.

    So, when you create your own type (which is really what your classes are, new types), you can add an input operator that allows you to do the same thing as above. If you said ifs >> myCard; where myCard had the type Card, then myCard would be filled with a suit and a value. If you said ifs >> myDeck; where myDeck had the type BunchOfCards, then myDeck would be filled with a vector of Card objects, each containing a suit and a value.

    Now, there is such a thing as an output operator (or operator<<). That is similar to the input operator, except of course it does the opposite. It takes information already stored in a variable, and outputs it to the output stream. That is of course the difference between the input and the output. The output is done when the information is already stored in the variable, where as the input puts the information into the variable. Your instructor gave you an example of an output operator. That was meant to help you understand what was going on, not for you to copy the code and make little changes to turn it into input.

    So what can you do? Well, as I mentioned earlier, you must understand what is going on. A good way to determine if you understand what is going on is to right down in English or pseudocode what you want to happen in the BunchOfCards input operator. Here is an example of how to do that. I will describe what is happening in your instructors example of an output operator:
    Code:
    ostream& operator <<(ostream& os, vector <Length> v)
    {
      for(int i=0;i<=v.size()-1;i++)
      {
        os<<v[i].feet;
        os<<v[i].inches;
      }
      return os;
    }
    English:
    Given an output stream and a vector of Length objects, this function will output all the objects in that vector. To do that, it loops through each Length object one at a time and does the following: Ouput the feet property, then output the inches property. When all objects have been output (which is determined by keeping a counter that reaches the size of the vector), then return the output stream.
    Now if you can do the same thing for what your input operator for BunchOfCards is supposed to do, then the next step of writing the code will be a lot easier. I have given a bunch of hints above about how in English it should work. The key is to not take them literally but to try to understand the point behind them. You won't understand all of it if you are just a beginner, but you should get enough information to write in English what it is your function should do.

    So in English, what is the operator>> for BunchOfCards supposed to do?

  14. #29
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79
    Fill the vector with Card objects-one by one- using an input stream. Vectors are filled with the push_back() built-in function.
    I keep trying to fill that something area. Nothing works.

  15. #30
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Here is my English for what it is supposed to do:
    Create a temporary Card. Attempt to read information into that card from the input stream. If that succeeds, push the temporary card on to the vector. This will put a copy of that card into the vector with the information that was just read in. Go back and try to read new information into the card. Repeat these steps until reading information into the card fails. Then return the input stream.
    Chances are that it won't work until you understand what is supposed to be going on.

    By the way, I noticed you had an output operator (ostream& operator<<(...)) in earlier code. Have you written one for the Card class and the BunchOfCards class? The one for the BunchOfCards class would closely follow your instructor's hint. If you wrote that and got it to compile and work, then it would make your life easier when testing the input iterator. In my long code example earlier, if you had the output operator you could add cout << aDeck; and have it output what it just read in so you could see if it worked.

    Also, in your Card input operator, you read in the suit first, and then the value, but in your example input file, the value comes before the suit. That will screw you up big time. Make sure the order you read data into the Card is the same as the order it exists in the input file.

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

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Question with accessing private data
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2008, 02:14 AM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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