私の講師はライブコードクリニックでこの関数を作成しました。コードを学習して理解するまで、それをコピーしてから何度も書き直そうとしています。
現在、プログラムを実行するために「findgreatest」関数をどこで定義する必要があるのかわかりません。main()内で関数を定義する必要があるという印象を受けました。ただし、表示されていないエラーがさらにある可能性があります。とにかく、このコードを実行してより詳細に説明するためのいくつかの助けをいただければ幸いです。
ありがとう
アレックス
#include <iostream>
#include <ctime>
using namespace std;
int main(int argc, const char * argv[]) {
const unsigned int size = 15; // creates a const int for array
int a_sig [size]; // assigns int to array size
for(int i=0; i<size; i++) {
a_sig[i] = rand() % 100;
cout << *(a_sig+i) << endl;
}
int findgreatest (int size, int a_sig) { //"F deceleration not allowed"
int max = -1;
for (int i = 0; i < size; i++) {
if (*(a_sig+i) > max){
max = *(a_sig+i);
}
}
return max;
};
int maximum;
maximum = findgreatest(size, a_sig); //"undeclared identifier"
return 0;
};
現在、プログラムを実行するために「findgreatest」関数をどこで定義する必要があるのかわかりません。main()内で関数を定義する必要があるという印象を受けました。
実際、mainで名前付き関数を定義することはできません。これはエラーで確認できますdeclaration not allowed
。
関数は使用前に宣言する必要があります。コンパイル中に、コンパイラは関数が定義されている場所を記録します。コンパイラーは、これらの関数の1つが呼び出されるのを見ると、これらの関数が存在することを認識しているため、どの命令を入れるかを認識しています。ただし、関数が宣言(および定義)されていない場合、コンパイラーは関数呼び出しを解釈できません。
mainの前に関数を定義するか、mainの前に関数を宣言してmainの後に定義する必要があります。
mainの前に関数を定義したい場合、コードは次のようになります。
#include <iostream>
#include <ctime>
#include <cstdlib> // rand is defined here
using namespace std;
// Defines findgreatest
// findgreatest is now available for use later in program
int findgreatest (int size, int a_sig[]) // I added a `[]` in this signature
{
int max = -1;
for (int i = 0; i < size; i++) {
if (*(a_sig+i) > max){
max = *(a_sig+i);
}
}
return max;
}
int main(int argc, const char * argv[]) {
const unsigned int size = 15; // creates a const int for array
int a_sig [size]; // assigns int to array size
for(int i=0; i<size; i++) {
a_sig[i] = rand() % 100;
cout << *(a_sig+i) << endl;
}
int maximum;
maximum = findgreatest(size, a_sig);
// You probably want to do something with the maximum?
cout << "\nMaximum is " << maximum << endl;
return 0;
} // (I removed an unnecessary semicolon here)
または、関数を宣言して(つまり、名前と署名の説明を付けて)、後で定義することもできます。あなたはこれを行うことができます
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
// declares findgreatest
// The compiler knows that this function is defined somewhere and can make
// references to it. If the definition isn't also provided during compilation,
// an error is raised.
int findgreatest (int size, int a_sig[]);
int main(int argc, const char * argv[]) {
const unsigned int size = 15; // creates a const int for array
int a_sig [size]; // assigns int to array size
for(int i=0; i<size; i++) {
a_sig[i] = rand() % 100;
cout << *(a_sig+i) << endl;
}
int maximum;
maximum = findgreatest(size, a_sig);
// You probably want to do something with the maximum?
cout << "\nMaximum is " << maximum << endl;
return 0;
}
// Definition
int findgreatest (int size, int a_sig[])
{
int max = -1;
for (int i = 0; i < size; i++) {
if (*(a_sig+i) > max){
max = *(a_sig+i);
}
}
return max;
}
rand()
、する必要があり#include <cstdlib>
ます。main(){}
。findgreatest(int size, int a_sig)
が正しくありません。2番目の引数は配列であり、intではありません。これを示す方法はいくつかありますがfindgreatest(int size, int a_sig[])
、配列を予期する必要があることをコンパイラーに示すように変更しました。配列についてさらに学ぶと、それについてもう少し知ることができます。そして、あなたはおそらく*(a_sig + i)
少し奇妙な表現を再訪するでしょう。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加