function pointers

Hi guys,
I am receiving this error from the compiler:
1
2
../main.cpp:51: undefined reference to `ParallelVector::ptExecute'
../main.cpp:52: undefined reference to `ParallelVector::ptExecute'


with this 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
class ParallelVector{
protected:
	static void (*ptExecute)(unsigned index);


	class Worker{
	private:
		double tmp;

	public:
		void* svc(void* ptrIndex){
			unsigned index = *((unsigned*) ptrIndex);
			if(! (ParallelVector::ptExecute)) { // ERROR
				(*ptExecute)(index); // ERROR
			}
			return GO_ON;
		}

	};

        friend class Worker;
}

void (ParallelVector::*ptExecute)(unsigned) = 0;


why is it blaming about undefined references?
closed account (o3hC5Di1)
Hi there,

Not entirely sure about this - but I think you can just use ptExecute.
The class doesn't know itself at the moment you define it, so referring to it makes the compiler go "paralelel-what-now?".

Hope that helps.

All the best,
NwN
hey, I cannot neither assign the function. Take for instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ParallelVector{
protected:
	static void (*ptExecute)(unsigned index); // pointer
	static void fsum(unsigned index); // impl

public:
        void sum(ParallelVector&x, ParallelVector &y); // interface
}

void (ParallelVector::*ptExecute)(unsigned index) = 0;

void ParallelVector::fsum(unsigned index){
	std::cout << "Sum " << index << std::endl;
}

void ParallelVector::sum(ParallelVector&x, ParallelVector &y){
	ptExecute = &fsum; // ERROR
}


I am starting to think that perhaps you are not allowed to work with static function pointers..

regards
-dean
closed account (o3hC5Di1)
Something else seems weird to me (but again, no expert, just trying to help):

static void (*ptExecute)(unsigned index);

Why do you have braces around *ptExecute?

If you want a void pointer as a return type shouldn't it be:
static void* ptExecute(...);

Also:

1
2
3
4
5
6
7
8
class ParallelVector{
protected:
	static void (*ptExecute)(unsigned index); // pointer
	static void fsum(unsigned index); // impl

public:
        void sum(ParallelVector&x, ParallelVector &y); // interface
}  ;  //You need a semicolon after the class definition 


Anyway - just my 2 cents, hopefully one of the experts around here can give you a more definite answer.

All the best,
NwN
What is "index" for:static void (*ptExecute)(unsigned index); // pointer
Last edited on
Hey NwN,
void* foo() is a function that returns a pointer to something
void (*foo)() is a pointer to a function that does not return anything

so if the function is not a static method of the class but a global function (as in C), everything works. I'd like to keep it static though.

regards
-dean
closed account (o3hC5Di1)
Hi there,

Thanks for clearing that up.
I tried to run your code as such:

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
#include <iostream>

class ParallelVector{
protected:
	static void (*ptExecute)(unsigned index);


	class Worker{
	private:
		double tmp;

	public:
		void* svc(void* ptrIndex){
			unsigned index = *((unsigned*) ptrIndex);
			if(! (ParallelVector::ptExecute)) { // ERROR
				(*ptExecute)(index); // ERROR
			}
			return GO_ON;
		}

	};

        friend class Worker;
};

void (ParallelVector::*ptExecute)(unsigned) = 0;


int main()
{
	return 0;
}


And it compiled.
Notice I put a semicolon after your ParallelVector class closing brace though.
Perhaps it's telling you its not defined because the class is not recognised properly as it is missing the semicolon at the end?

All the best,
NwN
hey NwN, I'm starting to become crazy, lol. Can you compile this?

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
#include <iostream>

class ParallelVector{
protected:
	static void (*ptExecute)(unsigned index);

protected:
	static void worker_sumxyz(unsigned index);

public:

	void sum();


};


void ParallelVector::worker_sumxyz(unsigned index_start){


}

void ParallelVector::sum(){
	std::cout << "[ParallelVector::sum]" << std::endl;
	ptExecute = &worker_sumxyz;
}



int main(int argc, char* argv[]){
	std::cout << "Hello world GCC" << std::endl;

	return 0;
}


regards
-dean
Last edited on
closed account (o3hC5Di1)
Hi there,

I couldn't at first, but then I saw you never defined ptExecute:

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
#include <iostream>

class ParallelVector{
protected:
	static void (*ptExecute)(unsigned index);

protected:
	static void worker_sumxyz(unsigned index);

public:

	void sum();


};

//added this line here
void (*ParallelVector::ptExecute)(unsigned) = 0;

void ParallelVector::worker_sumxyz(unsigned index_start){


}

void ParallelVector::sum(){
	std::cout << "[ParallelVector::sum]" << std::endl;
	ptExecute = &worker_sumxyz;
}



int main(int argc, char* argv[]){
	std::cout << "Hello world GCC" << std::endl;

	return 0;
}


Then it compiled and ran perfectly.
As far as I know, static members must be defined outside the class.

Hope that works for you :)

All the best,
NwN
mate - you are missing a very subtle point.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class ParallelVector
{
protected:
	
    //This is a  standard C++ (non-member) function pointer
    //it is also static member
    static void (*ptExecute)(unsigned index);

};


// correct definition of the static member function pointer above.
void (*ParallelVector::ptExecute) (unsigned index) =0;

//THIS IS INCORRECT - you are actually creating a new GLOBAL variable
//which is apointer to a  Parallelvector class member function that
//takes  an unsigned parameter with void return
void (ParallelVector::*ptExecute) (unsigned index) =0;
You shall define static data member ptExecute

void (*ParallelVector::ptExecute)(unsigned index);
thank you all guys!
Topic archived. No new replies allowed.