Semantic issues?

Discussion in 'Mac Programming' started by NiemandIstDa, Aug 8, 2014.

  1. NiemandIstDa, Aug 8, 2014
    Last edited: Aug 8, 2014

    NiemandIstDa macrumors newbie

    Joined:
    Aug 8, 2014
    #1
    Hello everybody,

    I'm using OS X Mavericks (latest version) and Xcode (also latest version).

    I'm trying to write some functions.

    Here are the files
    formula.h
    This defines mathematical functions in a class
    e.g.:
    Code:
    class maFormula {
                     public:
                        // Mathematical functions
                        double Power(double a, double b);
                      };
          
    Inside the formula.cpp the Power function is defined as
    Code:
    double maFormula::Power(double a, double exp) {
        double result = 1;
        
        if (exp > 1) {
            for (int i = 0; i < exp; i++) {
                result *=  a;
            }
        } else {
            for (int i = 0; i > exp; i--) {
                result /= a;
            }
        }
        
        return result;
    }
    
    Now I have a third file called constantPhysics.h
    There is a line:
    Code:
    const double        gVolt =  (1000 * gMetre) * (gMetre * gMetre) * Power(gSecond, -3) * gAmpere;
    
    This way I get two "issues" (where I'm not if I can ignore them or not)
    First: Use of undeclared identifier 'Power'; did you mean 'maFormula::power'?
    Second: Call to non-static member function without an object argument

    Now the funny thing that keeps me wondering: If I change the Power to maFormula::power(...) as suggested in the first "issue" this one is gone but the second keeps staying.
    When I now change my own function to the one in math defined pow() function both "issues" are gone. Since my function is almost the same as the one in the standard library, I wondered why I get the two "issues".
    Am I missing something or...

    Anybody any idea?

    BTW: all g... are constants, based on SI standards and are actually 1
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Just saying that Power (x, 1) doesn't work. And that Power (x, exp) doesn't work when exp is not an integer - if your function doesn't handle non-integer powers, make the parameter an integer.

    You may want to use the standard library functions, which will be bug free, faster, and handle things like Power (x, 1.0 / 3.0) just fine.
     
  3. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #3
    Power() is a member function of your class, you can not call it without an instance of maFormula.

    Code:
    maFormula maf;
    const double gVolt = maf.power(1, -3);
    
    But, you shouldn't have any implementation in the header file, it should just declare the variables. So for a const declared identifier you need:

    extern const double gVolt;

    Then implementation, i.e the definition in a .cpp file.
     
  4. NiemandIstDa thread starter macrumors newbie

    Joined:
    Aug 8, 2014
    #4
    Thank you very much. Now it works... I should have thought about that solution, it seems so easy, might got have a bad day...:cool:
     

Share This Page