Can not create a Vector of Strings

Discussion in 'C++' started by arnuld, Sep 9, 2008.

  1. arnuld

    arnuld Guest

    Hi Friends,

    I hope you have remembered me. Its been a long time since I hung here at
    comp.lang.c++. I still remember names like Daniel T., Victor Bazarov,
    Erik Wikstrom and many others :) .Good news is that I got a job and I am
    earning money and no longer dependent on my old parents. Bad news is that
    in these last 6 months , the job which required me to learn C and Socket
    Programming, nearly ate all of my time everyday. so I have not touched the
    C++ and forgotten the style and feel of it.

    Now today, I have started to walk again on C++ and crated a small program.
    I can't compile it anyway:



    // simple illustration of Std. Lib. Vector

    // this program will ask user to input a single word and will store that input
    // into a vector and will print it when user have hit the EOF or entered 3 words

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


    int main()
    {
    const int svec_size = 3;
    std::vector<std::string> svec[svec_size];
    std::string user_input;

    for( int i = 0; (std::cin >> user_input) || (i != svec_size); ++i )
    {
    svec.push_back( user_input );
    }

    std:cout << "-------------------------" << std::endl;

    for( std::vector<std::string>::const_iterator citer=svec.begin();
    citer != svec.end(); ++citer )
    {
    std::cout << *citer;
    }

    return 0;
    }


    ================= OUTPUT =========================
    [arnuld@dune ztest]$ g++ -ansi -pedantic -Wall -Wextra second.cpp
    second.cpp: In function `int main()':
    second.cpp:19: error: request for member `push_back' in `svec',
    which is of non-class type `std::vector<std::string,
    std::allocator<std::string> >[3]' second.cpp:22: error: `cout' was not
    declared in this scope second.cpp:24: error: request for member `begin' in
    `svec', which is of non-class type `std::vector<std::string,
    std::allocator<std::string> >[3]' second.cpp:25: error: request for member
    `end' in `svec', which is of non-class type `std::vector<std::string,
    std::allocator<std::string> >[3]' second.cpp:22: warning: unused variable
    'cout' second.cpp:22: warning: label `std' defined but not used
    [arnuld@dune ztest]$




    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
    Google Groups is Blocked. Reason: Excessive Spamming
     
    arnuld, Sep 9, 2008
    #1
    1. Advertising

  2. arnuld

    Ian Collins Guest

    arnuld wrote:

    > int main()
    > {
    > const int svec_size = 3;
    > std::vector<std::string> svec[svec_size];


    Did you intend to create an array of vectors?

    --
    Ian Collins.
     
    Ian Collins, Sep 9, 2008
    #2
    1. Advertising

  3. arnuld

    Fred Zwarts Guest

    "arnuld" <> wrote in message news:p...
    > Hi Friends,
    >
    > I hope you have remembered me. Its been a long time since I hung here at
    > comp.lang.c++. I still remember names like Daniel T., Victor Bazarov,
    > Erik Wikstrom and many others :) .Good news is that I got a job and I am
    > earning money and no longer dependent on my old parents. Bad news is that
    > in these last 6 months , the job which required me to learn C and Socket
    > Programming, nearly ate all of my time everyday. so I have not touched the
    > C++ and forgotten the style and feel of it.
    >
    > Now today, I have started to walk again on C++ and crated a small program.
    > I can't compile it anyway:
    >
    >
    >
    > // simple illustration of Std. Lib. Vector
    >
    > // this program will ask user to input a single word and will store that input
    > // into a vector and will print it when user have hit the EOF or entered 3 words
    >
    > #include <iostream>
    > #include <string>
    > #include <vector>
    >
    >
    > int main()
    > {
    > const int svec_size = 3;
    > std::vector<std::string> svec[svec_size];


    What do you want to do with the [svec_size]?
    You are now creating an array of three vectors. Why not creating just one vector?

    std::vector<std::string> svec;

    > std::string user_input;
    >
    > for( int i = 0; (std::cin >> user_input) || (i != svec_size); ++i )
    > {
    > svec.push_back( user_input );


    If you want to use the push_back function, you need to specify on which vector of the three vectors in the array.
    You cannot push it on all vectors in the array at once.
    Did you mean

    svec.push_back( user_input );

    > }
    >
    > std:cout << "-------------------------" << std::endl;
    >
    > for( std::vector<std::string>::const_iterator citer=svec.begin();
    > citer != svec.end(); ++citer )


    Again, the begin and end functions cannot be applied to the array of vectors.
    Select one of the three vectors, or don't use an array.

    > {
    > std::cout << *citer;
    > }
    >
    > return 0;
    > }
    >
    >
    > ================= OUTPUT =========================
    > [arnuld@dune ztest]$ g++ -ansi -pedantic -Wall -Wextra second.cpp
    > second.cpp: In function `int main()':
    > second.cpp:19: error: request for member `push_back' in `svec',
    > which is of non-class type `std::vector<std::string,
    > std::allocator<std::string> >[3]' second.cpp:22: error: `cout' was not
    > declared in this scope second.cpp:24: error: request for member `begin' in
    > `svec', which is of non-class type `std::vector<std::string,
    > std::allocator<std::string> >[3]' second.cpp:25: error: request for member
    > `end' in `svec', which is of non-class type `std::vector<std::string,
    > std::allocator<std::string> >[3]' second.cpp:22: warning: unused variable
    > 'cout' second.cpp:22: warning: label `std' defined but not used
    > [arnuld@dune ztest]$
    >
    >
    >
    >
    > --
    > www.lispmachine.wordpress.com
    > my email is @ the above blog.
    > Google Groups is Blocked. Reason: Excessive Spamming
    >
     
    Fred Zwarts, Sep 9, 2008
    #3
  4. Hi,
    I'll inline my 2 cent.

    Thomas

    arnuld wrote:
    > Hi Friends,
    >
    > I hope you have remembered me. Its been a long time since I hung here at
    > comp.lang.c++. I still remember names like Daniel T., Victor Bazarov,
    > Erik Wikstrom and many others :) .Good news is that I got a job and I am
    > earning money and no longer dependent on my old parents. Bad news is that
    > in these last 6 months , the job which required me to learn C and Socket
    > Programming, nearly ate all of my time everyday. so I have not touched the
    > C++ and forgotten the style and feel of it.
    >
    > Now today, I have started to walk again on C++ and crated a small program.
    > I can't compile it anyway:
    >
    >
    >
    > // simple illustration of Std. Lib. Vector
    >
    > // this program will ask user to input a single word and will store that input
    > // into a vector and will print it when user have hit the EOF or entered 3 words
    >
    > #include <iostream>
    > #include <string>
    > #include <vector>
    >
    >
    > int main()
    > {
    > const int svec_size = 3;

    I suspect that you really wanted to use one vector instead of a array of
    vectors:
    std::vector<std::string> svec(svec_size);
    > std::vector<std::string> svec[svec_size];
    > std::string user_input;
    >


    If you want the for loop to exit after 'svec_size' elements you should
    do something like this:
    for( int i = 0; (std::cin >> user_input) && (i != svec_size); ++i )
    But I don't know enough about iostreams to know when '(std::cin >>
    user_input)' will return false. In fact I always thought that it
    returned a std::istream reference so you might want to read up on it.
    > for( int i = 0; (std::cin >> user_input) || (i != svec_size); ++i )
    > {
    > svec.push_back( user_input );
    > }
    >


    There is a typo below e.g. it should have been 'std::cout'. But the
    compiler probably gave you a warning about it.
    > std:cout << "-------------------------" << std::endl;
    >
    > for( std::vector<std::string>::const_iterator citer=svec.begin();
    > citer != svec.end(); ++citer )
    > {
    > std::cout << *citer;
    > }
    >
    > return 0;
    > }
    >
    >
    > ================= OUTPUT =========================
    > [arnuld@dune ztest]$ g++ -ansi -pedantic -Wall -Wextra second.cpp
    > second.cpp: In function `int main()':
    > second.cpp:19: error: request for member `push_back' in `svec',
    > which is of non-class type `std::vector<std::string,
    > std::allocator<std::string> >[3]' second.cpp:22: error: `cout' was not
    > declared in this scope second.cpp:24: error: request for member `begin' in
    > `svec', which is of non-class type `std::vector<std::string,
    > std::allocator<std::string> >[3]' second.cpp:25: error: request for member
    > `end' in `svec', which is of non-class type `std::vector<std::string,
    > std::allocator<std::string> >[3]' second.cpp:22: warning: unused variable
    > 'cout' second.cpp:22: warning: label `std' defined but not used
    > [arnuld@dune ztest]$
    >
    >
    >
    >
     
    Thomas Austad, Sep 9, 2008
    #4
  5. arnuld

    arnuld Guest

    > On Tue, 09 Sep 2008 10:12:54 +0200, Fred Zwarts wrote:


    > What do you want to do with the [svec_size]?
    > You are now creating an array of three vectors. Why not creating just
    > one vector?


    Thats array .. oh .. no. and I thought I was giving the maximum number of
    elements this vector must have. This is the new version with one error:


    int main()
    {
    const int svec_size = 3;
    std::vector<std::string> svec;
    std::string user_input;

    for( int i = 0; (std::cin >> user_input) || (i != svec_size); ++i )
    {
    svec.push_back( user_input );
    }

    std::cout << "-------------------------" << std::endl;

    for( std::vector<std::string>::const_iterator citer=svec.begin();
    citer != svec.end(); ++citer )
    {
    std::cout << *citer;
    }

    return 0;
    }



    =================== OUTPUT ===============================
    [arnuld@dune ztest]$ g++ -ansi -pedantic -Wall -Wextra second.cpp
    second.cpp: In function `int main()':
    second.cpp:19: error: no matching function for call to
    `std::basic_string<char, std::char_traits<char>, std::allocator<char>
    >::push_back(std::string&)'

    /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/basic_string.h:795:
    note: candidates are: void std::basic_string<_CharT, _Traits,
    _Alloc>::push_back(_CharT) [with _CharT = char, _Traits =
    std::char_traits<char>, _Alloc = std::allocator<char>] [arnuld@dune ztest]$


    Line number 19 is where i did push_back on svec.


    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
    Google Groups is Blocked. Reason: Excessive Spamming
     
    arnuld, Sep 9, 2008
    #5
  6. arnuld

    arnuld Guest

    > On Tue, 09 Sep 2008 14:21:28 +0500, arnuld wrote:

    > .. SNIP....


    > =================== OUTPUT ===============================
    > [arnuld@dune ztest]$ g++ -ansi -pedantic -Wall -Wextra second.cpp
    > second.cpp: In function `int main()':
    > second.cpp:19: error: no matching function for call to
    > `std::basic_string<char, std::char_traits<char>, std::allocator<char>
    >>::push_back(std::string&)'

    > /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/basic_string.h:795:
    > note: candidates are: void std::basic_string<_CharT, _Traits,
    > _Alloc>::push_back(_CharT) [with _CharT = char, _Traits =
    > std::char_traits<char>, _Alloc = std::allocator<char>] [arnuld@dune ztest]$
    >


    > Line number 19 is where i did push_back on svec.



    okay, I got it, it should be svec.push_back(..) . This code works fine. DO
    you have any advise or views, like a I did change one thing, the type of
    svec_size:



    int main()
    {
    const std::vector<std::string>::size_type svec_size = 3;
    std::vector<std::string> svec;
    std::string user_input;

    for( std::vector<std::string>::size_type i = 0;
    (i != svec_size) && (std::cin >> user_input); ++i )
    {
    svec.push_back( user_input );
    }

    std::cout << "-------------------------" << std::endl;

    for( std::vector<std::string>::const_iterator citer=svec.begin();
    citer != svec.end(); ++citer )
    {
    std::cout << *citer << std::endl;
    }

    return 0;
    }



    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
    Google Groups is Blocked. Reason: Excessive Spamming
     
    arnuld, Sep 9, 2008
    #6
  7. On 9 Sep, 10:21, arnuld <> wrote:
    > > On Tue, 09 Sep 2008 10:12:54 +0200, Fred Zwarts wrote:
    > > What do you want to do with the [svec_size]?
    > > You are now creating an array of three vectors. Why not creating just
    > > one vector?

    >
    > Thats array .. oh .. no. and I thought I was giving the maximum number of
    > elements this vector must have. This is the new version with one error:
    >
    > int main()
    > {
    >   const int svec_size = 3;
    >   std::vector<std::string> svec;
    >   std::string user_input;
    >
    >   for( int i = 0; (std::cin >> user_input) || (i != svec_size); ++i )  
    >     {
    >       svec.push_back( user_input );


    svec is an element of the vector not the vector. Ypu probably mean

    svec.push_back( user_input );


    >     }
    >
    >  std::cout << "-------------------------" << std::endl;
    >
    >   for( std::vector<std::string>::const_iterator citer=svec.begin();  
    >        citer != svec.end();  ++citer )
    >     {
    >       std::cout << *citer;
    >     }
    >
    >   return 0;
    >
    > }
    >
    > =================== OUTPUT ===============================
    > [arnuld@dune ztest]$ g++ -ansi -pedantic -Wall -Wextra second.cpp
    > second.cpp: In function `int main()':
    > second.cpp:19: error: no matching function for call to
    > `std::basic_string<char, std::char_traits<char>,  std::allocator<char>>::push_back(std::string&)'
    >
    > /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/bas­ic_string.h:795:
    > note: candidates are: void std::basic_string<_CharT, _Traits,
    > _Alloc>::push_back(_CharT) [with _CharT = char, _Traits =
    > std::char_traits<char>, _Alloc = std::allocator<char>] [arnuld@dune ztest]$
    >
    > Line number 19 is where i did push_back on svec.
    >
    > --www.lispmachine.wordpress.com
    > my email is @ the above blog.
    > Google Groups is Blocked. Reason: Excessive Spamming


    so you won't see this post


    --
    Nick Keighley

    "The Dinosaurs have come and gone,
    we Theriodonts remain"
     
    Nick Keighley, Sep 9, 2008
    #7
  8. arnuld <> writes:
    > [...]
    > std::vector<std::string> svec;
    > [...]
    > svec.push_back( user_input );
    > [...]



    svec is a vector of strings.
    Therefore svec is a string.

    A string is like a vector of character.
    string::push_back will take a *character* and append it to the string.

    --
    __Pascal Bourguignon__
     
    Pascal J. Bourguignon, Sep 9, 2008
    #8
  9. On 2008-09-09 11:30, arnuld wrote:
    >> On Tue, 09 Sep 2008 14:21:28 +0500, arnuld wrote:

    >
    >> .. SNIP....

    >
    >> =================== OUTPUT ===============================
    >> [arnuld@dune ztest]$ g++ -ansi -pedantic -Wall -Wextra second.cpp
    >> second.cpp: In function `int main()':
    >> second.cpp:19: error: no matching function for call to
    >> `std::basic_string<char, std::char_traits<char>, std::allocator<char>
    >>>::push_back(std::string&)'

    >> /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/basic_string.h:795:
    >> note: candidates are: void std::basic_string<_CharT, _Traits,
    >> _Alloc>::push_back(_CharT) [with _CharT = char, _Traits =
    >> std::char_traits<char>, _Alloc = std::allocator<char>] [arnuld@dune ztest]$
    >>

    >
    >> Line number 19 is where i did push_back on svec.

    >
    >
    > okay, I got it, it should be svec.push_back(..) . This code works fine. DO
    > you have any advise or views, like a I did change one thing, the type of
    > svec_size:
    >
    >
    >
    > int main()
    > {
    > const std::vector<std::string>::size_type svec_size = 3;


    The type specified by size_type is very probably size_t, and unless you
    are going to work with really large vectors it will probably not matter
    if it is some other type. But size_t is much easier to type and read, so
    I would use size_t.

    --
    Erik Wikström
     
    Erik Wikström, Sep 9, 2008
    #9
  10. arnuld

    Pranav Guest

    Or you may be interested in defining as,
    const int svec_size = 3;
    std::vector<std::string> svec(svec_size);
     
    Pranav, Sep 10, 2008
    #10
  11. arnuld

    James Kanze Guest

    On Sep 9, 7:23 pm, Erik Wikström <> wrote:
    > On 2008-09-09 11:30, arnuld wrote:


    [...]
    > > int main()
    > > {
    > > const std::vector<std::string>::size_type svec_size = 3;


    > The type specified by size_type is very probably size_t,


    The type specified by std::vector< std::string >::size_type is
    required to be std::size_t. The size_type of all standard
    containers comes in fact from the allocator, and the standard
    allocator is required to use size_t for size_type. So unless
    you're dealing with containers using custom allocators, you can
    just use size_t.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Sep 10, 2008
    #11
    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. pmatos
    Replies:
    6
    Views:
    23,851
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    771
    Malcolm
    Jun 24, 2006
  3. Replies:
    8
    Views:
    1,938
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    574
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    368
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page