undefined reference

Hi everyone,

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.

This is my Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

class Math{
public:
	static int fib(int );
	static double exp(double, int);
	static void printArray();
private:
	static int a[20];

};

double exp(double x, int n){
	for(int i = 1; i < n; i++){
		x *= x;
	}
	return x;
}

int Math::fib(int i){
	return 0;
}
void Math::printArray(){
	cout<<"hallo"<<endl;
}

int main(){
	cout<<"Wir berechnen die ersten 20ig Stellen der Fibonacci Zahlen"<<endl;

	double x = 2;
	int n = 4;
	double exp_result;

	exp_result = Math::exp(x, n);
	cout<<x<<"^"<<n<<"ergibt: "<<exp_result<<endl;
}


Some of the functions are empty. Those are prepartions for a second method.

And this is the Error i receive:

D:/eclipse/workspace/test/Debug/../main.cpp:35: undefined reference to `Math::exp(double, int)'

Im glad for every hint someone could give me :)

Regards,
fercs
Last edited on
You forgot the Math:: in front of the declaration of exp().
Thanks very much for your answer!

Naturally it worked after that! ;)

Now i would like to expand that code and i have the same problem again, but this time using a static array out of a static method.

The Code is somewhat bigger now, but i have the Error in the Method Math::printArray.

It again says undefined reference. (im not getting use to this error somehow so far)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

class Math{
public:
	static int fib(int );
	static double exp(double, int);
	static void printArray();
private:
	static int 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?

Thanks again!

Regards,
fercs
Do it from a get/set public function in your class. you can't directly read/write a private member from outside it's class scope.
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?
The problem is that you have to instantiate the array somewhere (in a .cpp file)

 
int Math::a[20];



At this point, why don't you just use a namespace instead of an object? From the user's standpoint, it looks and works identically:

File: Math.h:
1
2
3
4
5
6
7
8
9
10
#indef MY_MATH_H
#define MY_MATH_H

namespace Math {
    int fib(int );
    double exp(double, int);
    void printArray();
}

#endif 


File: Math.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include "Math.h"

namespace Math {
    static int a[ 20 ];

    int fib( int n ) 
       { /* FIXME */ return 0; }

    // ... etc

}
Topic archived. No new replies allowed.