function pointers

J

Jeffrey Baker

Hi,
I can't get my code to link. I get
Example error LNK2019: unresolved external symbol "void __cdecl f(void *)"
(?f@@YAXPAX@Z) referenced in function _main
and
Example fatal error LNK1120: 1 unresolved externals

Here is the code:
#include <iostream>

using std::cout;

using std::endl;

void f( void(*)); // void(*) is the function pointer argument

void x();

void y();

int main()

{

f(x); // calls x function

f(y); // calls y function

return 0;

}

void f(void *c); // pointer function


void x()

{

cout << "x() is chosen" << endl;

}

void y()

{

cout << "y() is chosen" << endl;

}

Thanks,
jb
 
B

BigBrian

Jeffrey said:
Hi,
I can't get my code to link. I get
Example error LNK2019: unresolved external symbol "void __cdecl f(void *)"
(?f@@YAXPAX@Z) referenced in function _main
and
Example fatal error LNK1120: 1 unresolved externals

Here is the code:
#include <iostream>

using std::cout;

using std::endl;

void f( void(*)); // void(*) is the function pointer argument

void x();

void y();

int main()

{

f(x); // calls x function

f(y); // calls y function

return 0;

}

void f(void *c); // pointer function


void x()

{

cout << "x() is chosen" << endl;

}

void y()

{

cout << "y() is chosen" << endl;

}

Thanks,
jb

I don't see where f() defined? Your linker isn't either.

-Brian
 
T

Thomas Tutone

Jeffrey said:
I can't get my code to link. I get
Example error LNK2019: unresolved external symbol "void __cdecl f(void *)"
(?f@@YAXPAX@Z) referenced in function _main
and
Example fatal error LNK1120: 1 unresolved externals

Here is the code:

#include <iostream>
using std::cout;
using std::endl;

void f( void(*)); // void(*) is the function pointer argument

Change the above line to:

void f(void(*)());
void x();

void y();

int main()
{
f(x); // calls x function
f(y); // calls y function
return 0;
}

void f(void *c); // pointer function

Change the above line to:

void f(void (* c)())
{
c();
}
void x()
{
cout << "x() is chosen" << endl;
}

void y()
{
cout << "y() is chosen" << endl;
}

Best regards,

Tom
 
J

Jakob Bieling

Isn't void f( void(*)); the defined function?

Nope, its declaring it. In other words, saying "there is such a
function". You still need to tell the compiler what this function is
supposed to do, when it is called, by actually defining it:

void f (void (*))
{
// code here
}

Note the missing semicolon and the added curly braces.

hth
 
J

Jeffrey Baker

Jakob Bieling said:
Nope, its declaring it. In other words, saying "there is such a
function". You still need to tell the compiler what this function is
supposed to do, when it is called, by actually defining it:

void f (void (*))
{
// code here
}

Note the missing semicolon and the added curly braces.

I added the braces and it linked correctly. It didn't run like it should.
f(x) and f(y) in "int main()" did not call the
void x()
{
cout << "x() is chosen" << endl;
} and

void y()
{
cout << "y() is chosen" << endl;
}

Jeff
 
K

Kai-Uwe Bux

This void(*) is not a function pointer. Try:

void f( void (c) ( void ) ) {
c();
}



[snip]
I added the braces and it linked correctly. It didn't run like it should.
f(x) and f(y) in "int main()" did not call the
void x()
{
cout << "x() is chosen" << endl;
} and

void y()
{
cout << "y() is chosen" << endl;
}

Jeff


Best

Kai-Uwe Bux
 
A

Andrey Tarasevich

Jeffrey said:
I can't get my code to link. I get
Example error LNK2019: unresolved external symbol "void __cdecl f(void *)"
(?f@@YAXPAX@Z) referenced in function _main
and
Example fatal error LNK1120: 1 unresolved externals

Here is the code:
#include <iostream>

using std::cout;
using std::endl;

void f( void(*)); // void(*) is the function pointer argument

'void(*)' is not a function pointer argument. 'void(*)' is equivalent to
'void*', which is a regular "data" pointer, not a function pointer.
void x();
void y();

int main()
{
f(x); // calls x function
f(y); // calls y function

Error 1: This will not compile, because 'x' and 'y' in this context are values
of type 'void(*)()', while 'f' expects an argument of type 'void*'. You did not
mention this error in you original message, which most likely means that the
code you are trying to compile is different from the code you posted here.
Please, always post real code.
return 0;
}

void f(void *c); // pointer function

That's another declaration of the same function 'f'.
void x()
{
cout << "x() is chosen" << endl;
}

void y()
{
cout << "y() is chosen" << endl;
}

Error 2: You provided two declarations for 'f' but no definition. That would be
the reason for the linker error message.

Correct the above errors and it would work.
 
J

Jeffrey Baker

Kai-Uwe Bux said:
This void(*) is not a function pointer. Try:

void f( void (c) ( void ) ) {
c();
}
Thanks - my varation of code that is true too and not different:
#include <iostream>

using std::cout;

using std::endl;

void f( void(*)());

//void f( void (*c)());


void x();

void y();

int main()

{

f(x);

f(y);



return 0;

}

void f( void (*c) ( /*void */) ) {


c(); //----> this is what I didn't see with the argument ()'s as prototype
and declaration.

}


void x()

{

cout << "x() is chosen" << endl;

}

void y()

{

cout << "y() is chosen" << endl;

}

Best Regards,
Jeff
[snip]
I added the braces and it linked correctly. It didn't run like it should.
f(x) and f(y) in "int main()" did not call the
void x()
{
cout << "x() is chosen" << endl;
} and

void y()
{
cout << "y() is chosen" << endl;
}

Jeff


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top