Thread: Vector reading data unsuccessfully

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75

    Vector reading data unsuccessfully

    If I have this vector in the private portion of my class I have:

    Code:
    vector <int> numbers;
    and I have this in my main() function:

    Code:
    ifstream in_file;
    	in_file.open("numbers.dat");
    	
        if (in_file.fail())
    	{
           cout << "Failed to open file." << endl;
           exit(1);
        }
        
        while (in_file >> next)
        {  
           numbers.push_back[next];
           next++;
        }
        in_file.close();
    and I get an error on the push_back line. The error reads:
    expected primary-expression before '.' token
    I don't really see anything wrong with this...what can I do to fix it?! Thanks for the help!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by porsche911nfs View Post
    Code:
           numbers.push_back[next];
    Those should be parentheses, not square brackets.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    that would make sense, haha. Unfortunately, I get the same error message. It has to do something with calling the private vector....

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Only the member functions of the class (and functions/classes declared as friends) are allowed access to private members. It is most probably a mistake on your part to attempt to access the vector elsewhere.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    how would i fix this then?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by porsche911nfs View Post
    how would i fix this then?
    If users of the class need the ability to insert things into the vector, you need to provide a public method which does that. However, they should not be allowed to insert anything they want "willy-nilly." If that were the case, the vector might as well have been public.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
        while (in_file >> next)
        {  
           numbers.push_back[next];
           next++;
        }
    If you have that in your main() function, and numbers is supposed to be a member of some class, then where is the object of the class that contains numbers? You can't just call a member variable from outside of a class...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> next++;

    Note that the line above is useless. You are reading into the next variable anyway, so incrementing it does nothing.

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. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM