[SOLVED] (beginner) Calling a function from another class ?



  • Hi everyone!
    I've got 2 classes :
    player.h
    @class Player
    {
    private :
    QString index ;
    public :
    void setindex(const QString &s);@
    handler.h
    @class Handler
    (
    bool characters(const QString &str, Player &pl);@
    And in Handler.cpp (the function inherits from another class)
    @
    bool Handler::characters(const QString &str, Player &pl)
    {
    void pl->setindex(&str);@ //ERROR : 'unexpected token after '->'

    The last one is a syntax error. How do you access the function member of another class?
    Thanks!



  • I can see two issues:

    1. you don't call a function with the return type eg void in your code
    2. you don't pass a &str (that is the address of a QString object and would be good if your function would take a QString*)

    So correct code is:
    @ bool Handler::characters(const QString &str, Player &pl)
    {
    pl.setindex(str); //no void and no & LE: and use . (dot) not ->
    @

    //and in the future you can tell us the exact error that your compiler gave you

    LE: deleted previous LE since now i saw that you pass pl as a parameter
    the correct syntax is pl.setindex(str); i thought pl is a member pointer in your Handler class



  • Thanks a lot! It was not very clear for me the difference between the call and the declaration for the use of reference. I've checked it and it's clearer now.

    For this call, how can I call a member function without creating an object ?
    @Handler::characters(const QString &str, Player &pl)
    {
    pl.setindex(str);@
    How can I do without passing the class as argument as above (It's a function that I can't rewrite and that I inherit).
    If I create an object inside the function, the object will disappear at the end of life of the function...?

    I would like to do something like :
    @Handler::characters(const QString &str)
    {
    Player::setindex(str);@
    but it's forbidden ('cannot call member function without object')



  • Hello.
    The first question: what do you want to achive?
    If you don't want to pass object, you may create a pointer to object inside your function, initialize it and then return it.
    @
    Player *Handler::characters(const QString &str) {
    Player *new_player = new Player ();
    new_player ->setindex(str);
    //...
    return new_player
    }
    @
    You may also use pointer as argument instead of reference.
    @
    Handler::characters(const QString &str, Player *pl) {
    pl ->setindex(str);
    //...
    }
    @
    [quote author="Philili" date="1336915608"]Thanks a lot! It was not very clear for me the difference between the call and the declaration for the use of reference. I've checked it and it's clearer now.[/quote]
    My advice is to read some books about C and C++. For example, "The C Programming Language":http://cm.bell-labs.com/cm/cs/cbook/ for information about C, and "The C++ Programming Language":http://www2.research.att.com/~bs/3rd.html. It will make it easy for you to understand, how to achive what you want.



  • [quote]
    My advice is to read some books about C and C++. For example, "The C Programming Language":http://cm.bell-labs.com/cm/cs/cbook/ for information about C, and "The C++ Programming Language":http://www2.research.att.com/~bs/3rd.html. It will make it easy for you to understand, how to achive what you want.[/quote]
    Thanks for the advice. I've just ordered this book. Hope to receive that soon!

    [quote author="Wilk" date="1336916999"]Hello.
    The first question: what do you want to achive?
    If you don't want to pass object, you may create a pointer to object inside your function, initialize it and then return it.
    @
    Player *Handler::characters(const QString &str) {
    Player *new_player = new Player ();
    new_player ->setindex(str);
    //...
    return new_player
    }
    @
    You may also use pointer as argument instead of reference.
    @
    Handler::characters(const QString &str, Player *pl) {
    pl ->setindex(str);
    //...
    }
    @
    [/quote]
    In fact, I can't modify the function at all in terms of parameters or return type (the function is part of Qt classes by inheritance). But you helped me a lot since I think I'm near my goal now: I use a constructor :
    in Player.h
    @ Class Player
    { QString index;@

    in handler.h
    @public:
    bool characters(const QString &str);
    Player* playerptr;
    @
    in handler.cpp
    @Handler::Handler(Player* playertest)
    {
    playerptr = playertest;
    bool SaxHandler::characters(const QString &str)
    {
    f (test == "john") playerptr->setindex(str);
    ..... //Different functions which modify members of class Player
    }@

    and, finally, in main.cpp
    @Player playertest;
    Handler handler(&playertest);
    QString test = playertest.index;
    qDebug()<< test;
    @

    and it's working! Finally. Thanks for everything and your comments!



  • While waiting for those books you can search for Thinking in C++ there are two volumes written by Bruce Eckel and are available as free download.

    //my opinion is that you will need some C++ knowledge to read Stroustrup's book (because C++ programming language is not exactly for beginners, he has another book for beginners called "Programming - principles and practice using C++":http://www2.research.att.com/~bs/programming.html)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.