"return function" doesn't work

This is a discussion on "return function" doesn't work within the C++ Programming forums, part of the General Programming Boards category; here's my simplified codes: Code: class Drug { public: Drug(): avai(0) {}; bool& get_avai() { return avai; } protected: bool ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    66

    "return function" doesn't work

    here's my simplified codes:
    Code:
    class Drug
    {
               public:
                       Drug(): avai(0) {};
                       bool& get_avai()
                       {
                                return avai;
                       }
               protected:
                       bool avai;
    };
    
    if (somedrug.get_avai()) 
    {
                //do something
    }
    my compiler (Dev-C++) says " cannot convert Drug::get_avai()' to `

    how come?
    Last edited by Cris987; 03-02-2004 at 12:21 AM.
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You're missing a semi-colon at the end of your class.

    Code:
    class Drug
    {
               public:
                       Drug(): avai(0) {};
                       bool& get_avai()
                       {
                                return avai;
                       }
               protected:
                       bool avai;
    };
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    Originally posted by MrWizard
    You're missing a semi-colon at the end of your class.

    Code:
    class Drug
    {
               public:
                       Drug(): avai(0) {};
                       bool& get_avai()
                       {
                                return avai;
                       }
               protected:
                       bool avai;
    };
    well, ya....sry, but that wasn't the problem (i just typed it wrong)
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    The error message is weird; is that all you get?
    You might wanna omit the ampersand (&) as the return type
    Code:
    // bool& get_avai()
    bool get_avai()
    {
          return avai;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    Originally posted by alphaoide
    The error message is weird; is that all you get?
    You might wanna omit the ampersand (&) as the return type
    Code:
    // bool& get_avai()
    bool get_avai()
    {
          return avai;
    }
    Umm....tried it. Stilll doesn't work (same error)
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  6. #6
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    I am not sure about this.

    I tried to compile your class with an empty main and the only line that gave me problems was
    Code:
    Drug(): avai(0) {};
    there is a colon after the function Drug. I think that is supposed to be a constructor? If it is there is no colon after it just braces with whatever constructor code. The next part with the avai function gives me a syntax error for a constant? Not sure if that helps or not. If I take out the line above it compiles with no errors. I don't see where you have any destructors?

    Code:
    Drug(paramater list here for constructor)
    { constructor code here }
    
    ~Drug()
    { destructor code here or empty if no clean up needed }
    Last edited by manofsteel972; 03-03-2004 at 03:09 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Maybe you should post the full code where you get this problem, and the full error. For example, maybe you passed const Drug& somedrug to a method, then called somedrug.get_avai(). That might cause a "cannot convert" error because of the const. This code works fine in MSVC++6.0:
    Code:
    class Drug
    {
    public:
        Drug(): avai(0) {};
        bool& get_avai()
        {
            return avai;
        }
    protected:
        bool avai;
    };
    
    int main()
    {
        Drug somedrug;
        if (somedrug.get_avai()) 
        {
        }
    }
    By the way, the constructor is fine, and there is no need to write a destructor if it isn't going to do anything. The default will work just fine.

  8. #8
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Unhappy Your right.

    I tried to recreate a similar class instead of copy and paste. I will avoid making comments in the future until I have a better understanding of the problem. Sorry.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    Okya, I 'm gonna post up the actual code that makes the error

    Code:
    //TOWN.H
    #ifndef TOWN_H
    #define TOWN_H
    #include <string>
    #include <iostream>
    using namespace std;
    
    int rand_between(int min, int max);         //returns random number
    void cho_destin();          //prompts user to choose destination
    
    class Drug
    {
        public:
            Drug():avai(0) {}
            Drug(bool Avai):avai(Avai){}        
            bool& get_avai();                   
        
        protected:       
            bool avai;       //availabilty
    };
    
    class Town
    {
        public:
            Town();      //constructor        
            void refresh();         //refreshes each drug's price        
                    
        protected:               
            Drug drugs[8];        
    
    };    
    
    #endif
    
    --------------------------------------------
    //TOWN.CPP
    #include "town.h"
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    bool& Drug::get_avai()
    {
        return avai;
    }
          
    void Town::refresh()
    {
        if (drugs[0].get_avai)                                 //ERROR!
            {
                  cout << "ERROR!!!"; 
            }
        
    }
    town.cpp In member function `void Town::refresh()':
    line 13 town.cpp could not convert `this->Town::drugs[0].Drug::get_avai()' to `bool
    [Build Error] [town.o] Error 1
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if (drugs[0].get_avai) //ERROR!

    To call the function you need parentheses:
    if (drugs[0].get_avai())

  11. #11
    Registered User
    Join Date
    Nov 2003
    Posts
    66
    Originally posted by swoopy
    > if (drugs[0].get_avai) //ERROR!

    To call the function you need parentheses:
    if (drugs[0].get_avai())

    OMFG....thx alot
    An Unofficial Cristiano Ronaldo Website : Ronaldo 7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 11:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 11:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21