Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

NiemandIstDa

macrumors newbie
Original poster
Aug 8, 2014
6
0
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
 
Last edited:
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.
 
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

Anybody any idea?

BTW: all g... are constants, based on SI standards and are actually 1

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.
 
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.
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:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.