Using Vectors in classes - public access

Discussion in 'C++' started by andrewmorrey@aol.com, May 11, 2007.

  1. Guest

    Hello,

    I've got a VC++ project containing multiple classes and a main
    function. In one of the class functions, it reads from a text file and
    places the data into a vector;
    //
    std::vector<std::vector<std::string> > applications (50,
    std::vector<std::string>(12));
    applications[colArr][0] = "Test1";
    applications[colArr][1] = "Test2";
    //
    Which works fine for that classes function, however, I need for other
    functions of the class, or potentially other classes to be able to
    access the applications vector, most likely done through friend-ing
    and inheritance.
    I seem to be unable to get the applications vector to be publicly or
    even privately declared inside the main class definition. I've tried
    placing the vector definition in both sections, it causes
    error C2059: syntax error : 'constant'
    when I do.
    Is there something I'm missing with this?

    Thanks,
    - Andy


    -- Copy of UpdateFileArray.h ---
    #ifndef _UPDATEFILEARRAY_H_
    #define _UPDATEFILEARRAY_H_

    #include <string>
    #include <vector>

    class UpdateFileArray
    {
    std::vector<std::vector<std::string> > applications (50,
    std::vector<std::string>(12));
    public:
    void UpdateArray();
    void ArrayLookUp();
    friend class SearchVehicle;
    };
    extern UpdateFileArray UFA;

    #endif
    , May 11, 2007
    #1
    1. Advertising

  2. On May 11, 12:57 pm, wrote:
    > Hello,
    >
    > I've got a VC++ project containing multiple classes and a main
    > function. In one of the class functions, it reads from a text file and
    > places the data into a vector;
    > //
    > std::vector<std::vector<std::string> > applications (50,
    > std::vector<std::string>(12));
    > applications[colArr][0] = "Test1";
    > applications[colArr][1] = "Test2";
    > //
    > Which works fine for that classes function, however, I need for other
    > functions of the class, or potentially other classes to be able to
    > access the applications vector, most likely done through friend-ing
    > and inheritance.
    > I seem to be unable to get the applications vector to be publicly or
    > even privately declared inside the main class definition. I've tried
    > placing the vector definition in both sections, it causes
    > error C2059: syntax error : 'constant'
    > when I do.
    > Is there something I'm missing with this?
    >
    > Thanks,
    > - Andy
    >
    > -- Copy of UpdateFileArray.h ---
    > #ifndef _UPDATEFILEARRAY_H_
    > #define _UPDATEFILEARRAY_H_
    >
    > #include <string>
    > #include <vector>
    >
    > class UpdateFileArray
    > {
    > std::vector<std::vector<std::string> > applications (50,
    > std::vector<std::string>(12));
    > public:
    > void UpdateArray();
    > void ArrayLookUp();
    > friend class SearchVehicle;};
    >
    > extern UpdateFileArray UFA;
    >
    > #endif


    Have a function in the vectors class to return a reference to the
    vector.

    One question though, why are you defining a vector of vector of
    strings? Surely if you're just reading from a text file, a vector of
    std::strings will be more than sufficent.
    Keith Halligan, May 11, 2007
    #2
    1. Advertising

  3. Guest

    On May 11, 1:14 pm, Keith Halligan <> wrote:
    > On May 11, 12:57 pm, wrote:
    >
    >
    >
    >
    >
    > > Hello,

    >
    > > I've got a VC++ project containing multiple classes and a main
    > > function. In one of the class functions, it reads from a text file and
    > > places the data into a vector;
    > > //
    > > std::vector<std::vector<std::string> > applications (50,
    > > std::vector<std::string>(12));
    > > applications[colArr][0] = "Test1";
    > > applications[colArr][1] = "Test2";
    > > //
    > > Which works fine for that classes function, however, I need for other
    > > functions of the class, or potentially other classes to be able to
    > > access the applications vector, most likely done through friend-ing
    > > and inheritance.
    > > I seem to be unable to get the applications vector to be publicly or
    > > even privately declared inside the main class definition. I've tried
    > > placing the vector definition in both sections, it causes
    > > error C2059: syntax error : 'constant'
    > > when I do.
    > > Is there something I'm missing with this?

    >
    > > Thanks,
    > > - Andy

    >
    > > -- Copy of UpdateFileArray.h ---
    > > #ifndef _UPDATEFILEARRAY_H_
    > > #define _UPDATEFILEARRAY_H_

    >
    > > #include <string>
    > > #include <vector>

    >
    > > class UpdateFileArray
    > > {
    > > std::vector<std::vector<std::string> > applications (50,
    > > std::vector<std::string>(12));
    > > public:
    > > void UpdateArray();
    > > void ArrayLookUp();
    > > friend class SearchVehicle;};

    >
    > > extern UpdateFileArray UFA;

    >
    > > #endif

    >
    > Have a function in the vectors class to return a reference to the
    > vector.
    >
    > One question though, why are you defining a vector of vector of
    > strings? Surely if you're just reading from a text file, a vector of
    > std::strings will be more than sufficent.- Hide quoted text -
    >
    > - Show quoted text -


    Hm, I'm a bit puzzled, what do you mean, return a reference to the
    vector?

    Also, vector of a vector of strings, since it needs to be two
    dimentional. A line is read from the text file then split where spaces
    occor, then the pieces are placed inside the vector.
    , May 11, 2007
    #3
  4. Salt_Peter Guest

    On May 11, 8:45 am, wrote:
    > On May 11, 1:14 pm, Keith Halligan <> wrote:
    >
    >
    >
    > > On May 11, 12:57 pm, wrote:

    >
    > > > Hello,

    >
    > > > I've got a VC++ project containing multiple classes and a main
    > > > function. In one of the class functions, it reads from a text file and
    > > > places the data into a vector;
    > > > //
    > > > std::vector<std::vector<std::string> > applications (50,
    > > > std::vector<std::string>(12));
    > > > applications[colArr][0] = "Test1";
    > > > applications[colArr][1] = "Test2";
    > > > //
    > > > Which works fine for that classes function, however, I need for other
    > > > functions of the class, or potentially other classes to be able to
    > > > access the applications vector, most likely done through friend-ing
    > > > and inheritance.
    > > > I seem to be unable to get the applications vector to be publicly or
    > > > even privately declared inside the main class definition. I've tried
    > > > placing the vector definition in both sections, it causes
    > > > error C2059: syntax error : 'constant'
    > > > when I do.
    > > > Is there something I'm missing with this?

    >
    > > > Thanks,
    > > > - Andy

    >
    > > > -- Copy of UpdateFileArray.h ---
    > > > #ifndef _UPDATEFILEARRAY_H_
    > > > #define _UPDATEFILEARRAY_H_

    >
    > > > #include <string>
    > > > #include <vector>

    >
    > > > class UpdateFileArray
    > > > {
    > > > std::vector<std::vector<std::string> > applications (50,
    > > > std::vector<std::string>(12));
    > > > public:
    > > > void UpdateArray();
    > > > void ArrayLookUp();
    > > > friend class SearchVehicle;};

    >
    > > > extern UpdateFileArray UFA;

    >
    > > > #endif

    >
    > > Have a function in the vectors class to return a reference to the
    > > vector.

    >
    > > One question though, why are you defining a vector of vector of
    > > strings? Surely if you're just reading from a text file, a vector of
    > > std::strings will be more than sufficent.- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Hm, I'm a bit puzzled, what do you mean, return a reference to the
    > vector?
    >
    > Also, vector of a vector of strings, since it needs to be two
    > dimentional. A line is read from the text file then split where spaces
    > occor, then the pieces are placed inside the vector.



    Look at your class, you are ignoring the constructor(s) altogether.
    Declare a ctor and initialize the vector-vector in its initialization
    list.

    The following is a declaration:
    std::vector<std::vector<std::string> > applications;
    .... while this is a definition:
    std::vector<std::vector<std::string> >
    applications (50, std::vector<std::string>(12));
    .... and definitions are not allowed in a class declaration.

    #include <iostream>
    #include <string>
    #include <vector>

    class UpdateFileArray
    {
    typedef std::vector<std::vector<std::string> > VecVecStr;
    VecVecStr applications;
    public:
    UpdateFileArray()
    : applications(10, std::vector<std::string>(10)) { }
    // member functions
    VecVecStr& getapp() { return applications; } // get ref to private v-
    v
    size_t size() const { return applications.size(); }
    };

    int main()
    {
    UpdateFileArray ufa;
    std::cout << ufa.size() << std::endl;

    std::vector<std::string> v(10, "some string");

    typedef std::vector<std::vector<std::string> > VecVecStr;
    VecVecStr& r_apps = ufa.getapp(); // seat the reference
    r_apps.push_back(v);

    std::cout << ufa.size() << std::endl;
    }

    Although i'm a little puzzled as well as to why you need a vector of
    vectors as well.
    If each line in the text file represents a Record composed of multiple
    strings seperated by spaces, you should be storing std::vector<
    Records > where a Record class reflects the fields involved.
    Salt_Peter, May 11, 2007
    #4
  5. On 2007-05-11 14:45, wrote:
    > On May 11, 1:14 pm, Keith Halligan <> wrote:
    >> On May 11, 12:57 pm, wrote:
    >>
    >>
    >>
    >>
    >>
    >> > Hello,

    >>
    >> > I've got a VC++ project containing multiple classes and a main
    >> > function. In one of the class functions, it reads from a text file and
    >> > places the data into a vector;
    >> > //
    >> > std::vector<std::vector<std::string> > applications (50,
    >> > std::vector<std::string>(12));
    >> > applications[colArr][0] = "Test1";
    >> > applications[colArr][1] = "Test2";
    >> > //
    >> > Which works fine for that classes function, however, I need for other
    >> > functions of the class, or potentially other classes to be able to
    >> > access the applications vector, most likely done through friend-ing
    >> > and inheritance.
    >> > I seem to be unable to get the applications vector to be publicly or
    >> > even privately declared inside the main class definition. I've tried
    >> > placing the vector definition in both sections, it causes
    >> > error C2059: syntax error : 'constant'
    >> > when I do.
    >> > Is there something I'm missing with this?

    >>
    >> > Thanks,
    >> > - Andy

    >>
    >> > -- Copy of UpdateFileArray.h ---
    >> > #ifndef _UPDATEFILEARRAY_H_
    >> > #define _UPDATEFILEARRAY_H_

    >>
    >> > #include <string>
    >> > #include <vector>

    >>
    >> > class UpdateFileArray
    >> > {
    >> > std::vector<std::vector<std::string> > applications (50,
    >> > std::vector<std::string>(12));
    >> > public:
    >> > void UpdateArray();
    >> > void ArrayLookUp();
    >> > friend class SearchVehicle;};

    >>
    >> > extern UpdateFileArray UFA;

    >>
    >> > #endif

    >>
    >> Have a function in the vectors class to return a reference to the
    >> vector.
    >>
    >> One question though, why are you defining a vector of vector of
    >> strings? Surely if you're just reading from a text file, a vector of
    >> std::strings will be more than sufficent.- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > Hm, I'm a bit puzzled, what do you mean, return a reference to the
    > vector?


    Something like this:

    class UpdateFileArray
    {
    typedef Apps std::vector<std::vector<std::string> >;
    Apps applications (50, std::vector<std::string>(12));
    public:
    void UpdateArray();
    void ArrayLookUp();
    Apps& getArray() { return applications; }
    const Apps& getArray() { return applications; }
    friend class SearchVehicle;
    };

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, May 11, 2007
    #5
  6. Salt_Peter Guest

    On May 11, 1:07 pm, Erik Wikström <> wrote:
    > On 2007-05-11 14:45, wrote:
    >
    >
    >
    > > On May 11, 1:14 pm, Keith Halligan <> wrote:
    > >> On May 11, 12:57 pm, wrote:

    >
    > >> > Hello,

    >
    > >> > I've got a VC++ project containing multiple classes and a main
    > >> > function. In one of the class functions, it reads from a text file and
    > >> > places the data into a vector;
    > >> > //
    > >> > std::vector<std::vector<std::string> > applications (50,
    > >> > std::vector<std::string>(12));
    > >> > applications[colArr][0] = "Test1";
    > >> > applications[colArr][1] = "Test2";
    > >> > //
    > >> > Which works fine for that classes function, however, I need for other
    > >> > functions of the class, or potentially other classes to be able to
    > >> > access the applications vector, most likely done through friend-ing
    > >> > and inheritance.
    > >> > I seem to be unable to get the applications vector to be publicly or
    > >> > even privately declared inside the main class definition. I've tried
    > >> > placing the vector definition in both sections, it causes
    > >> > error C2059: syntax error : 'constant'
    > >> > when I do.
    > >> > Is there something I'm missing with this?

    >
    > >> > Thanks,
    > >> > - Andy

    >
    > >> > -- Copy of UpdateFileArray.h ---
    > >> > #ifndef _UPDATEFILEARRAY_H_
    > >> > #define _UPDATEFILEARRAY_H_

    >
    > >> > #include <string>
    > >> > #include <vector>

    >
    > >> > class UpdateFileArray
    > >> > {
    > >> > std::vector<std::vector<std::string> > applications (50,
    > >> > std::vector<std::string>(12));
    > >> > public:
    > >> > void UpdateArray();
    > >> > void ArrayLookUp();
    > >> > friend class SearchVehicle;};

    >
    > >> > extern UpdateFileArray UFA;

    >
    > >> > #endif

    >
    > >> Have a function in the vectors class to return a reference to the
    > >> vector.

    >
    > >> One question though, why are you defining a vector of vector of
    > >> strings? Surely if you're just reading from a text file, a vector of
    > >> std::strings will be more than sufficent.- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > > Hm, I'm a bit puzzled, what do you mean, return a reference to the
    > > vector?

    >
    > Something like this:
    >
    > class UpdateFileArray
    > {
    > typedef Apps std::vector<std::vector<std::string> >;


    typedef [source] [target], you got it backwards

    > Apps applications (50, std::vector<std::string>(12));


    This is C++, not Java, definitions go either in a ctor definition's
    body, that ctor's init list or in a function definition.
    Definitions go with definitions.
    You aren't allowed to define members in a class declaration.
    A declaration is for declarations.

    class N
    {
    int n; // int n = 0; is illegal
    N(); // also a declaration
    void foo(); // a declaration
    };

    // a ctor *definition* with an init list
    N::N() : n( 0 ) { }

    // this is a definition:
    void N::foo() { }

    > public:
    > void UpdateArray();
    > void ArrayLookUp();
    > Apps& getArray() { return applications; }
    > const Apps& getArray() { return applications; }
    > friend class SearchVehicle;
    >
    > };
    >
    > --
    > Erik Wikström
    Salt_Peter, May 11, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,323
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    528
    SpamProof
    Oct 21, 2003
  3. Christian Bongiorno
    Replies:
    5
    Views:
    529
    Chris Uppal
    Aug 30, 2004
  4. Replies:
    3
    Views:
    675
    Shadowman
    Mar 26, 2008
  5. Guest
    Replies:
    0
    Views:
    426
    Guest
    Sep 14, 2005
Loading...

Share This Page