how to make this possible

N

nik

hello friends, the following code is not running but it replace n by j
& create error i.e. undeclared tokenj . but i want to call
token0,token1 from the loop in main.how it could be achieved.plz tell
me

#include <iostream.h>
#define paster(n) token##n##() //token pasting
int token1() //token1() defination
{
return 1;

}
int token0() //token1() defination
{
return 0;

}
void main( )
{
for(int j=0;j<2;j++) //for Loop for j=0,1
{
int i=paster(j); //to call
token0(),token1()
cout<<"\n"<<i;
}
}
 
R

Rolf Magnus

nik said:
hello friends, the following code is not running but it replace n by j
& create error i.e. undeclared tokenj . but i want to call
token0,token1 from the loop in main.how it could be achieved.plz tell
me

This is not possible. The names are static at compile time and typicall
don't exist anymore at runtime. You want them to be dynamically chosen. The
paster() macro is resolved in the preprocessor, before the actual compiler
even sees the code.
Use a vector of function pointers instead.
#include <iostream.h>

Non-standard header.
#define paster(n) token##n##() //token pasting
int token1() //token1() defination
{
return 1;

}
int token0() //token1() defination
{
return 0;

}
void main( )

main() must return int.
 
N

nik

plz tell me the coding for that
Rolf said:
This is not possible. The names are static at compile time and typicall
don't exist anymore at runtime. You want them to be dynamically chosen. The
paster() macro is resolved in the preprocessor, before the actual compiler
even sees the code.
Use a vector of function pointers instead.


Non-standard header.


main() must return int.
 
T

Thorsten Kiefer

nik said:
plz tell me the coding for that


#include <iostream.h>
using namespace std;

int token1() //token1() defination
{
return 1;
}
int token0() //token1() defination
{
return 0;
}

typedef int MyFunction();
MyFunction myfunctions[] = {token0,token1};

int main(){
for(int i = 0;i < 2;++i)
cout << myfuntions() << endl;
}
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top