#include <iostream>
#include <cmath>
using namespace std;
// 這個聲明函數原型的代碼必須有, 如果沒有的話會報use of undeclared identifier 'simon' 這個異常
void simon(double n);
int main()
{
simon(3);
cout << "please input a int num : " << endl;
double n;
cin >> n;
simon(n);
cout<< "the end" << endl;
return 0;
}
void simon(double n)
{
cout<<"simon says n = " << n << endl;
}
如上面所示的代碼, 有一個自定義的函數simon, 如果該函數實在main()方法的下方的話, 那麼注釋下方的哪行代碼 void simon(double n); 不寫的話就會抛如下的異常:
/tmp/652211883/main.cpp:10:2: error: use of undeclared identifier 'simon'
simon(3);
^
/tmp/652211883/main.cpp:14:2: error: use of undeclared identifier 'simon'
simon(n);
^
2 errors generated.
exit status 1
還有種方法來避免這個問題就是把自定義的方法寫到main函數的上方, 如下所示:
#include <iostream>
#include <cmath>
using namespace std;
//void simon(double n);
// 寫到main的上方可以避免這個異常
void simon(double n)
{
cout<<"simon says n = " << n << endl;
}
int main()
{
simon(3);
cout << "please input a int num : " << endl;
double n;
cin >> n;
simon(n);
cout<< "the end" << endl;
return 0;
}