using push back function of a vector class template

This is a discussion on using push back function of a vector class template within the C++ Programming forums, part of the General Programming Boards category; I was trying to open a file of strings, then store them in a vector then print the vector to ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    1

    Question using push back function of a vector class template

    I was trying to open a file of strings, then store them in a vector then print the vector to prove i stored them correctly using c++.
    I wanted to use the push_back function of the vector class template but i keep getting an error on the line with the push_back function like i was using the function the wrong way or something. Originally i used a borland compiler
    here a portion of my code. If you have a better way to store into a vector or can help me use the push back function of the vector class, i would really appreciate it.

    //sample code

    datafile.open(filename);
    for(j=0; !datafile.eof(); j++)
    {
    datafile >> q;
    vector_save.push_back(q);
    cout << vector_save[j];
    j++;
    vector_save[j] = " ";
    cout << vector_save[j];
    }
    Last edited by anti-hw; 07-20-2002 at 12:42 PM.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Is it crashing or just not compiling?

    Code:
    datafile.open(filename);
    for(j=0; !datafile.eof(); j++)
    {
    datafile >> q;
    vector_save.push_back(q);
    cout << vector_save[j];
    j++;
    // crash here if vector save was empty before
    // j is now one passed the last element in the vector
    // accessing that is not good :).
    vector_save[j] = " ";  
    cout << vector_save[j];
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. template function v.s. template class
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2007, 12:46 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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