passing argument in STL loop ?

Discussion in 'C++' started by sd2004, Jan 20, 2006.

  1. sd2004

    sd2004 Guest

    I am still learning, could someone
    show/explain to me how to fix the error.
    I can see it is being wrong but do not know how to fix.
    could you also recommend a book that I can ref. to ?
    ////////////////// ERROR MESSAGE /////////////////////


    bash-2.05b$ g++ test5c.cpp
    test5c.cpp: In function `int main()':
    test5c.cpp:36: error: invalid initialization of reference of type
    'astruct&'
    from expression of type 'std::string'
    test5c.cpp:20: error: in passing argument 1 of `void
    dog_name(astruct&)'
    test5c.cpp: In function `void dog_name(astruct&)':
    test5c.cpp:49: error: overloaded function with no contextual type
    information
    test5c.cpp:49: error: base operand of `->' is not a pointer
    bash-2.05b$
    ////////////////////// SOURCE CODE //////////////////////////////
    #include<iostream>
    #include <string>
    #include<vector>
    #include<sstream>
    #include<fstream>
    using namespace std;


    struct astruct
    {
    string name;
    int id;
    string type;



    };


    #define goldenretriever 11
    #define Lab 33
    #define Boxer 44
    #define Terrier 55

    void dog_name (astruct& astructref);
    int main()
    {
    vector<astruct> v;
    astruct astr;
    ifstream in ("test5c.txt");
    string line;
    while (getline(in,line)){
    istringstream anyname(line);
    anyname>>astr.name>>astr.id>>astr.type;
    v.push_back(astr);
    }
    vector<astruct>::iterator search;
    for (search=v.begin();search!=v.end();++search){
    switch (search->id){
    case goldenretriever:
    dog_name(search->name);
    break;
    case Lab:
    cout <<search->name <<" is Lab. "<<endl;
    default:
    break;
    break;
    }


    }


    return 0;


    }


    void dog_name (astruct& s){
    cout <<search->name <<" is GoldenRetriever "<<endl;


    }


    ////////////////////// INPUT FILE "test5c.txt" ///////////////////

    cricket 11 GoldenRetriever
    nitro 11 GoldenRetriever
    Maxtor 33 Lab
    Aaxtor 44 Boxer
    Dora 55 Terrier
     
    sd2004, Jan 20, 2006
    #1
    1. Advertising

  2. sd2004 wrote:

    > could you also recommend a book that I can ref. to ?


    To learn: Koenig & Moo, Accelerated C++
    For reference: Stroustrup, The C++ Programming Language

    Best regards,

    Tom
     
    Thomas Tutone, Jan 20, 2006
    #2
    1. Advertising

  3. sd2004

    Marcus Kwok Guest

    sd2004 <> wrote:
    > I am still learning, could someone
    > show/explain to me how to fix the error.
    > I can see it is being wrong but do not know how to fix.


    > ////////////////// ERROR MESSAGE /////////////////////
    >
    > bash-2.05b$ g++ test5c.cpp
    > test5c.cpp: In function `int main()':
    > test5c.cpp:36: error: invalid initialization of reference of type
    > 'astruct&'
    > from expression of type 'std::string'
    > test5c.cpp:20: error: in passing argument 1 of `void
    > dog_name(astruct&)'


    Here is your function prototype:
    > void dog_name (astruct& astructref);


    Here is your definition of astruct:
    > struct astruct
    > {
    > string name;
    > int id;
    > string type;
    > };


    This is your iterator:
    > vector<astruct>::iterator search;


    Here is how you call dog_name:
    > dog_name(search->name);


    search->name is equivalent to (*search).name, and (*search) is of type
    astruct, so you are passing the "name" member (which is of type string)
    of the astruct associated with the current iterator. Therefore you are
    passing a string into your dog_name() function, but dog_name() is
    expecting a reference to an astruct.

    > test5c.cpp: In function `void dog_name(astruct&)':
    > test5c.cpp:49: error: overloaded function with no contextual type
    > information
    > test5c.cpp:49: error: base operand of `->' is not a pointer


    > void dog_name (astruct& s){
    > cout <<search->name <<" is GoldenRetriever "<<endl;
    > }


    You use operator-> with pointers (and pointer-like types, like
    iterators) to refer to members of structs or classes. You use operator.
    to refer to members of structs or classes when they are actual objects,
    or references to objects. Also, you are using the wrong variable in
    your function, and the parameter should be const since you are not
    changing it.

    > could you also recommend a book that I can ref. to ?


    The C++ FAQ is a good source of information:
    http://www.parashift.com/c -faq-lite/

    There is also the newsgroup:
    alt.comp.lang.learn.c-c++
    which is more geared towards beginners.

    For book recommendations, check out:
    http://www.accu.org/

    --
    Marcus Kwok
     
    Marcus Kwok, Jan 21, 2006
    #3
  4. sd2004

    sd2004 Guest

    Thanks for taking your time. I really appreciated.
    After reading your thread. I did fixed the code and it worked.
     
    sd2004, Jan 21, 2006
    #4
    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. Replies:
    5
    Views:
    612
    benben
    Jan 31, 2006
  2. Trans
    Replies:
    3
    Views:
    201
    Robert Klemme
    Jan 26, 2005
  3. ±è ÁØ¿µ
    Replies:
    7
    Views:
    262
    Einar Magnús Boson
    Nov 27, 2008
  4. User1014
    Replies:
    1
    Views:
    192
    Richard Cornford
    Nov 30, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    397
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page