GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Jun-2006, 02:12
george89 george89 is offline
New Member
 
Join Date: Jun 2006
Posts: 21
george89 is on a distinguished road

Problem compiling a program, my first vector.


I'm having a problem compiling a program that uses a vector. This is just to learn upon, but my problem stems from my book that I am reading: C++ Programming Language 3rd Edition by Bjarne Stroustrup. Seems as though, he doesn't tell you what preprocessor files are needed for vector... so I went hunting online and I believe I found out that it was <vector>. Now my problem is that it seems as though my compiler does not understand what I'm trying to express with my vector. I'm just about positive that it is the correct syntax, though, it doesn't seem to recognize it still... here is the code, it's not big at all, and following it is my compiler errors.

CPP / C++ / C Code:
#include <iostream>
#include <vector>
#include <time.h>

struct Entry
{
	int number;
};

int Index = 10;

vector<Entry> phone_book(Index);

void ReadEntry(int i)
{
	std::cout << phone_book[i] << std::endl;
}

void ResizeEntry()
{
	phone_book.resize(phone_book.size()+1);
	++Index;
}

void InputEntry(int i, int j)
{
	if ( i > index )
	{
		ResizeEntry();
	}
	phone_book[i] = j;
}

int main()
{
	srand(time(NULL));
	
	for ( int i = 0; i < 20; i++ )
	{
		InputEntry(i,rand()%1+25);
	}
	
	for ( int i = 0; i < Index; i++ )
	{
		ReadEntry(i);
	}
	
	return 0;
}

... and the gcc/g++ output...

Code:
george@geobox1 ~/cfiles/GeorgeTris/061706/trashtheseorlearn $ g++ -g Vector.cpp -Wall -Wextra -o Vector Vector.cpp:12: error: expected constructor, destructor, or type conversion before '<' token Vector.cpp: In function `void ReadEntry(int)': Vector.cpp:16: error: `phone_book' was not declared in this scope Vector.cpp:16: warning: unused variable 'phone_book' Vector.cpp: In function `void ResizeEntry()': Vector.cpp:21: error: `phone_book' was not declared in this scope Vector.cpp: In function `void InputEntry(int, int)': Vector.cpp:27: error: ISO C++ forbids comparison between pointer and integer Vector.cpp:31: error: `phone_book' was not declared in this scope Vector.cpp:31: warning: unused variable 'phone_book' george@geobox1 ~/cfiles/GeorgeTris/061706/trashtheseorlearn $

I'm I doing something incorrect?
  #2  
Old 26-Jun-2006, 03:01
MichaelS-R MichaelS-R is offline
Junior Member
 
Join Date: Apr 2006
Location: Berkshire, UK
Posts: 65
MichaelS-R is on a distinguished road

Re: Problem compiling a program, my first vector.


I think that you have missed the point with vectors... There are lots of errors in your code and I am not clear what you are really trying to achieve. Your best bet would be to start with something really simple:

Create a vector of int.
Add the values 10, 20, 30, 40 to the vector (forget about random numbers etc. - make sure you get out what you put in in the order you expect)
Print all the values out from the beginning to the end.

Later on put a class into the vector.
__________________
Michael

Dual Opteron 280 (2 x dual core) with 2Gb RAM, 2x36GB system drives, 2T on 3Ware 9500Mi RAID controller. Running Fedora Core 4. Using Anjuta IDE. Developemnt in C++ with MySQL (via mysql++).
  #3  
Old 26-Jun-2006, 21:10
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 172
Blake will become famous soon enough

Re: Problem compiling a program, my first vector.


there's no need to make a struct for an entry. Just use a typedef. That will make your life easier, and it will simplify debugging.

CPP / C++ / C Code:
typedef int Entry;
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Unwinding a recursive bool function? earachefl C++ Forum 13 08-Jun-2006 08:20
Combining Vectors and References Frankg C++ Forum 7 14-Jan-2006 06:17
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
Problem with program breggo C++ Forum 3 08-Jun-2005 13:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:31.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.