Issue
The system is: Linux/CentOS 6.4
I keep getting an error for functions not declared in scope. Is it not legal to call a function within another function? I read an article on function, thought it was because I needed to declare the functions void when I call them, but I received new errors. I am not sure if I need to declare them global or something.
client.cpp:32: error: 'takef' was not declared in this scope
client.cpp:33: error: 'putf' was not declared in this scope
client.cpp: In function 'void takef(int&)':
client.cpp:44: error: 'testa' was not declared in this scope
client.cpp: In function 'void putf(int&)':
client.cpp:70: error: 'test' was not declared in this scope
Example of the type of code I'm trying to implement:
sem_t mutex;
sem_t S;
char buffer[1024];
void error(const char *msg)
{
perror(msg);
exit(0);
}
/*
void signal_callback_handler()
{
close(sockfd);
}
*/
void father(int &sockfd)
{
while(1)
{
srand(time(NULL));
int ms = rand() % 2000 + 5000
send(sockfd, DATA, strlen(DATA), 0);
usleep(1000*ms);
takef(sockfd);
putf(sockfd);
}
}
void takef(int &sockfd)
{
/*
*
* *Other code*
*
*
*/
testa(sockfd);
/* *Other code*
*
*
*/
}
void testa(int &sockfd)
{
/*
*
*
* *Other code*
*
*
*/
}
void putf(&sockfd)
{
/*
*
*
* *Other code*
*
*
*/
test();
test();
sem_post(&mutex);
}
int main(int argc, char *argv[])
{
/*
*
*
* *Other code*
*
*
*/
father(sockfd);
return 0;
}
Solution
Simple fix. Move the function definitions above the void father() function.
In code
sem_t mutex;
sem_t S;
char buffer[1024];
void error(const char *msg)
{
perror(msg);
exit(0);
}
/*
void signal_callback_handler()
{
close(sockfd);
}
*/
void takef(int &sockfd)
{
/*
*
* *Other code*
*
*
*/
testa(sockfd);
/* *Other code*
*
*
*/
}
void testa(int &sockfd)
{
/*
*
*
* *Other code*
*
*
*/
}
void putf(&sockfd)
{
/*
*
*
* *Other code*
*
*
*/
test();
test();
sem_post(&mutex);
}
void father(int &sockfd)
{
while(1)
{
srand(time(NULL));
int ms = rand() % 2000 + 5000
send(sockfd, DATA, strlen(DATA), 0);
usleep(1000*ms);
takef(sockfd);
putf(sockfd);
}
}
int main(int argc, char *argv[])
{
/*
*
*
* *Other code*
*
*
*/
father(sockfd);
return 0;
}
Or another alternative, like mentioned below, is to write a function prototype. This is similar to how one would write a prototype for functions in a header file and then define the functions in a .cpp file. A function prototype is a function without a body and lets the compiler know the function exists but is not defined yet.
Here is an example using prototyping
sem_t mutex;
sem_t S;
char buffer[1024];
void error(const char *msg)
{
perror(msg);
exit(0);
}
/*
void signal_callback_handler()
{
close(sockfd);
}
*/
// Prototypes
void takef(int &sockfd);
void testa(int &sockfd);
void putf(&sockfd);
void father(int &sockfd)
{
while(1)
{
srand(time(NULL));
int ms = rand() % 2000 + 5000
send(sockfd, DATA, strlen(DATA), 0);
usleep(1000*ms);
takef(sockfd);
putf(sockfd);
}
}
void takef(int &sockfd)
{
/*
*
* *Other code*
*
*
*/
testa(sockfd);
/* *Other code*
*
*
*/
}
void testa(int &sockfd)
{
/*
*
*
* *Other code*
*
*
*/
}
void putf(&sockfd)
{
/*
*
*
* *Other code*
*
*
*/
test();
test();
sem_post(&mutex);
}
int main(int argc, char *argv[])
{
/*
*
*
* *Other code*
*
*
*/
father(sockfd);
return 0;
}
Answered By - Paul Renton Answer Checked By - Terry (WPSolving Volunteer)