Thread: Problem with tutorial (Vector class)

  1. #1
    Registered User OdyTHeBear's Avatar
    Join Date
    Dec 2002
    Posts
    4

    Question Problem with tutorial (Vector class)

    Hi!

    I'm currently reading the tutorials on this page. For the moment, I'm reading the tutorial about the Vector Class.
    To understand the tutorial, I tried to copy, paste and compile the source code as it was written in the tutorial.
    Unfortunately, my compiler generated 8 (!!) error messages. I found out that when adding the '.h' extension at the header files in the include statements, most of the errors disappeared (Though I really wonder why; the tutorial explicitly states that you're supposed to write the header filenames without the '.h' extension.)

    The actual error message i get when compiling (after adding the '.h' twice) says :
    'Member identifier expected in function main()'

    Also, I receive some warnings saying that I'm comparing signed an unsigned integers. (??)

    Does anyone know why the tutorial is not working with my compiler?
    (I'm using the Borland C++ 5.5 Compiler.)

    I'm submitting my source code as well, please have a look at it.

    Thanx.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You had a couple of problems. The big one is that you were trying to use switch as a member of the vector example. I tested this on bcc55 as well as mingw (testing it on bcc55 was an afterthought). This follows the standard better than what you had.


    Code:
    #include <iostream>
    #include <vector>
    
    using std::vector;
    using std::cin;
    using std::cout;
    using std::swap;
    
    int main()
    
    {
    
      vector <int> example;        //Vector to store integers
      example.push_back(3);         //Add 3 onto the vector
    
      example.push_back(10);        //Add 10 to the end
    
      example.push_back(33);        //Add 33 to the end
    
      for(int x=0; x<example.size(); x++) 
    
      cout<<example[x]<<" ";        //Should output: 3 10 33
    
      if(!example.empty())          //Checks if empty
    
      example.clear();              //Clears vector
    
      vector <int> another_vector;  //Creates another vector to store integers
    
      another_vector.push_back(10); //Adds to end of vector
    
      example.push_back(10);        //Same
    
      if(example==another_vector)   //To show testing equality
    
      example.push_back(20); 
    
    // this is wrong!!! switch is actually a statement!
    //  example.switch(1, 2);         //Switches elements 1 and 2
      swap(example[1], example[2]);
    
    
      for(int y=0; y<example.size(); y++)
    
      cout<<example[y]<<" ";        //Should output 20 10
    
      cin.get();	
    
      return 0;
    
    }

  3. #3
    Registered User OdyTHeBear's Avatar
    Join Date
    Dec 2002
    Posts
    4

    Thumbs down not quite...

    Thanks, that really helped me.
    However, the output is all messed up.
    According to the the tutorial (which I copied), the output is supposed to be "3 10 33 20 10".
    When I run my application, I get the output "3 10 33 10 33".

    Why does the code not work correctly, I wonder??
    Obviously, the last to numbers, 10 and 33 are somehow connected with the 10 and 33 in the middle of the output, which must mean that I'm getting output from the wrong vector...?
    (Of course, that's just my guess, I really have no clue...)
    Anyone plz help me out here.

    Thanks

  4. #4
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    Im getting same error..
    i get : 3 10 33 10 33;

    nobody can help here?

    Luigi

  5. #5
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    heheh

    hehe I found the error myself after all..
    try this instead..

    swap(example[0],example[1]);

    pretty dumb he..

    dumb me.. :P

    luigi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tutorial problem (headers)
    By AsiNisiMasa in forum C++ Programming
    Replies: 1
    Last Post: 08-08-2005, 11:24 PM
  2. MSVC Tutorial problem - Monochrome Palette
    By colinH in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2002, 03:57 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Problem running a Cboard C++ tutorial!
    By Monkey Liar in forum C++ Programming
    Replies: 6
    Last Post: 02-15-2002, 03:32 AM