CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2008
    Posts
    1

    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?

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Need help converting an array to a vector

    To use the vector class you'll need to include the 'vector.h' header class:
    Code:
    #include <vector>
    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

  3. #3
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    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. =)
    Last edited by Plasmator; November 4th, 2008 at 08:40 PM. Reason: Fixed spelling.

  4. #4
    Join Date
    Nov 2008
    Posts
    39

    Re: Need help converting an array to a vector

    please, in the future, post your code in code tags

    after you have
    Code:
    #include <vector>
    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

  5. #5
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    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.

  6. #6
    Join Date
    Nov 2008
    Posts
    39

    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...
    Last edited by Mal Reynolds; November 4th, 2008 at 09:17 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured