C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-13-2002, 02:02 PM   #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.
Attached Files
File Type: cpp test.cpp (962 Bytes, 14 views)
OdyTHeBear is offline   Reply With Quote
Old 12-13-2002, 03:35 PM   #2
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
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;

}
master5001 is offline   Reply With Quote
Old 12-13-2002, 05:20 PM   #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
OdyTHeBear is offline   Reply With Quote
Old 12-18-2002, 02:44 PM   #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
Luigi is offline   Reply With Quote
Old 12-18-2002, 02:49 PM   #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
Luigi is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:43 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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