Need help converting an array to a vector
This is my current coding, in my header file
#ifndef _ADDRESSBOOK
#define _ADDRESSBOOK
const int MAXADDRESS = 25;
struct PERSON
{
char fName[25];
char lname[25];
char Address[100];
};
class addressBook
{
private:
PERSON people[10];
public:
addressBook();
addressBook(char *fName, char*lname, char *add);
addressBook(PERSON a);
addressBook(PERSON init[], int count);
bool addPerson(const PERSON &p);
bool getPerson(PERSON &p);
bool findPerson(char *lastName, PERSON &p);
bool findPerson(char *lastName, char *firstName, PERSON &p);
void bubbleSort();
void printBook();
};
#endif
I have to change this the
"private:
PERSON people[10];"
to a vector, so I tried declaring it like this
vector <PERSON> people;
but i keep getting error messages saying stuff like this
1>e:\c++ level 2\address book\address book\addressbook.h(17) : error C2143: syntax error : missing ';' before '<'
1>e:\c++ level 2\address book\address book\addressbook.h(17) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\c++ level 2\address book\address book\addressbook.h(17) : error C2238: unexpected token(s) preceding ';'
1>e:\c++ level 2\address book\address book\addressbook.cpp(20) : error C2065: 'people' : undeclared identifier
but that was how my teacher showed me I declare a vector. well actually, he showed me while using "vector <int> x;" but he said that I could use a struct as a base type..I think. Can anybody tell me why I'm getting these errors?
Re: Need help converting an array to a vector
To use the vector class you'll need to include the 'vector.h' header class:
In addition you need to specify the 'std' namespace, either like this:
Code:
using namespace std;
or like this:
Code:
std::vector<PERSON> people;
- petter
Re: Need help converting an array to a vector
Quote:
Originally Posted by bf2loser
Code:
#ifndef _ADDRESSBOOK
#define _ADDRESSBOOK
//...
#endif
Technically speaking, your naming convention isn't "valid".
The C++ standard reserves such names (underscore followed by an upper case letter) for the implementation.
While it's unlikely that your naming scheme will cause a collision, it's still something that you can avoid altogether.
Quote:
Originally Posted by wildfrog
In addition you need to specify the 'std' namespace, either like this:
Code:
using namespace std;
OP, it's worthwhile to mention that it's often a very bad idea to include this in a header file.
You're forcing this on your users -- namespace pollution is usually not a good thing. =)
Re: Need help converting an array to a vector
please, in the future, post your code in code tags :thumb:
after you have
it is possible that your problem is very simple.
some compilers do not like it when you leave a space between 'vector', and the first '<', and will give you errors similar to those you have. try taking the space out like how wildfrog has in his above post:
Code:
vector<PERSON> people;
and see if that clears up the errors, otherwise, post the exact code that is giving you problems, in code tags ;)
Re: Need help converting an array to a vector
Quote:
Originally Posted by Mal Reynolds
some compilers do not like it when you leave a space between 'vector', and the first '<', and will give you errors similar to those you have.
Can you name at least one? I know older MS compilers had trouble with these things:
Code:
T <Y<U>> t; //In reference to operator>>
I've never heard of what you described, though.
Re: Need help converting an array to a vector
ok, i cannot find where I originally heard that, so I am sorry that i am not able to substantiate that idea, however...
as I was looking for any information on it, I did not find a single code example that had a space between them... though, as i'm sure you are aware, you could easily argue that that is just a coincidence due to normal coding conventions. so... though i cannot substantiate that idea, it may still be worth a try.
edit: on closer look, it may very well just be the absence of the <vector> header that is causing those errors...