Function question!
I have 2 functions, and I want them to be able to call to each other, but they''re inside the same .cpp file.
I''ll give an example of what I have and what I''d want to do..
void hi1()
{
cout << "how are you?";
hi2();
}
void hi2()
{
cout << "i am fine." << endl;
hi1()
}
hi2 works fine, but hi1 won''t work because it says hi2 wasnt declared or created yet....so how would i get past this problem??
Declare all the functions at the start of your cpp file, then create them further down.
Like this:
void hi1();
void hi2();
void hi1()
{
cout << "how are you?";
hi2();
}
void hi2()
{
cout << "i am fine." << endl;
hi1()
}
Be careful; This code will create an infinte loop.
Like this:
void hi1();
void hi2();
void hi1()
{
cout << "how are you?";
hi2();
}
void hi2()
{
cout << "i am fine." << endl;
hi1()
}
Be careful; This code will create an infinte loop.
Hi. What you need is a function declaration. These are lines that tell the compiler that there is a particular function in the file and it should just wait to see it.
// declare hi1
void hi1();
// declare hi2
void hi2();
void hi1()
{
cout << "how are you?";
hi2();
}
void hi2()
{
cout << "i am fine." << endl;
hi1()
}
Now the code will compile and the program will run...forever! This code has no way to stop calling itself!
Pete
// declare hi1
void hi1();
// declare hi2
void hi2();
void hi1()
{
cout << "how are you?";
hi2();
}
void hi2()
{
cout << "i am fine." << endl;
hi1()
}
Now the code will compile and the program will run...forever! This code has no way to stop calling itself!
Pete
Damn it! Beaten to the punch again!
By the way ghostboy, aybe you should invest in a good c/c++ book.
By the way ghostboy, aybe you should invest in a good c/c++ book.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement