help, what is wron in this code?

This is a discussion on help, what is wron in this code? within the C++ Programming forums, part of the General Programming Boards category; i created this class: class Surgery { public: double cost(); }; I am then using it in this line: patientList[index].addCharges(Surgery.cost()); ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    help, what is wron in this code?

    i created this class:

    class Surgery
    {
    public:
    double cost();
    };

    I am then using it in this line:

    patientList[index].addCharges(Surgery.cost());

    but I get this error:

    g:\hospital\hospital.cpp(22) : error C2275: 'Surgery' : illegal use of this type as an expression

    What is the problem? I have spent hours and dont see the problem!!!!!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    First off, it's bad design. Second off, you should be making an actual instance of the function, or ... I believe you can make it a static function and have it work. I'd have to look it up to be sure. (I never use C++.)

    There's a way to do it, but you can't just make up a class definition and use functions from it with no instance of the class (unless they're static, or whatever the keyword is I'm looking for).

    That being said:
    Code:
    class Surg
    {
        public:
            double cost( ) { return SOMETHING_HERE; }
    };
    
    Surg instance;
    
    foo[bar].addCost( instance.cost( ) );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    You need to make an instance of your class.
    Code:
    Surgery s;
    patientList[index].addCharges(s.cost());
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    thank u guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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