im pretty new to C++ programming and i have a few errors that i just cant solve on my own.
Here is my code. Everything in the File main.cpp (i already tried to split it up in 3 different files, including a header File. Tough the Error stays the same.)
I want to build a class using different mathematical formulas. Now to not use an object i use the keyword static.
As i understand this should help me to use methods of a class without creating an object.
#include <iostream>
usingnamespace std;
class Math{
public:
staticint fib(int );
staticdouble exp(double, int);
staticvoid printArray();
private:
staticint a[20];
};
double Math::exp(double x, int n){
double result = x;
for(int i = 1; i < n; i++){
result *= x;
}
return result;
}
int Math::fib(int i){
return 0;
}
void Math::printArray(){
cout<<"Member of Private Static Array of A"<<endl;
for(int i =0; i < 20;i++){
cout<<Math::a[i]<<endl;
}
}
int main(){
cout<<"Ein kleines Program um ein paar Mathe Formel zu rechnen:"<<endl;
cout<<"Wollen Sie Exponential Rechnungen durchführen? (y/n)"<<endl;
char answer;
cin>>answer;
if(answer == 'y'){
double exp_result, x;
int y;
cout<<"Mit was fuer Zahlen wollen Sie rechnen? x^y"<<endl;
cin>>x;
cin>>y;
exp_result = Math::exp(x, y);
cout<<x<<"^"<<y<<"ergibt: "<<exp_result<<endl;
}
cout<<"Wollen Sie die ersten 20ig Stellen der Fibonacci Zahlen kennen lernen? (y/n)"<<endl;
cin>>answer;
if(answer == 'y'){
Math::printArray();
}
}
Error: D:/eclipse/workspace/test/Debug/../main.cpp:28: undefined reference to `Math::a'
First Question is, how can i access (read/write) this static Array.
And 2nd Question is, is it true (could test it myself if i could read the array) that static arrays are initialised with Value 0 at start?
As far as i understand the whole class thing, my function printArray is public.
And a public function should be allowed to read private attributes of the same class?
So how i understand it, in the function printArray i am inside the classes scope?