2016-09-11 6 views
-1

最近私のcppコードに問題があります。私はcppの開発者ではなく、この問題はちょっと混乱しているようです。コードはフローです。エラー: '<methodname>'はこのスコープで宣言されていません

#include <stdio.h> 
#include <stdlib.h> 
#include <stdint.h> 
#include <float.h> 
#include <math.h> 
#include <iostream> 
#include "libarff/arff_parser.h" 
#include "libarff/arff_data.h" 

using namespace std; 

int second_loop(int i, ArffData* dataset, float smallestDistance){ 

    int smallestDistanceClass; 

    for(int j = 0; j < dataset->num_instances(); j++) // target each other instance 
     { 
      if(i == j) 
       continue; 

      float distance = third_loop(i, j, dataset); 

      distance = sqrt(distance); 

      if(distance < smallestDistance) // select the closest one 
      { 
       smallestDistance = distance; 
       smallestDistanceClass = dataset->get_instance(j)->get(dataset->num_attributes() - 1)->operator int32(); 
      } 
     } 

    return smallestDistanceClass; 

} 

float third_loop(int i, int j, ArffData* dataset){ 

    float distance = 0; 

    for(int k = 0; k < dataset->num_attributes() - 1; k++) // compute the distance between the two instances 
    { 
     float diff = dataset->get_instance(i)->get(k)->operator float() - dataset->get_instance(j)->get(k)->operator float(); 
     distance += diff * diff; 
    } 

    return distance; 
} 

int* KNN(ArffData* dataset) 
{ 
    int* predictions = (int*)malloc(dataset->num_instances() * sizeof(int)); 

    for(int i = 0; i < dataset->num_instances(); i++) // for each instance in the dataset 
    { 
     float smallestDistance = FLT_MAX; 
     int smallestDistanceClass = second_loop(i, dataset, smallestDistance); 

     predictions[i] = smallestDistanceClass; 
    } 

    return predictions; 
} 

int main(int argc, char *argv[]) 
{ 
    if(argc != 2) 
    { 
     cout << "Usage: ./main datasets/datasetFile.arff" << endl; 
     exit(0); 
    } 

    ArffParser parser(argv[1]); 
    ArffData *dataset = parser.parse(); 
    struct timespec start, end; 

    clock_gettime(CLOCK_MONOTONIC_RAW, &start); 

    int* predictions = KNN(dataset); 
} 

投げられるエラーは次のとおりです。

main_new.cpp: In function ‘int second_loop(int, ArffData*, float)’: 
main_new.cpp:21:54: error: ‘third_loop’ was not declared in this scope 
      float distance = third_loop(i, j, dataset); 
                ^

誰でもこのエラーで私を助けることができます。 third_loopの構文は正しいと思われますが、このエラーは引き続き発生します。

答えて

1

3番目のループに問題があります。 second_loop()では、コンパイラは、まだここでそれを見ていないthird_loop()にもかかわらず、機能にアクセス:

float distance = third_loop(i, j, dataset); 

最適解は次のように、ファイルの先頭に関数を宣言前方に次のようになります。

float third_loop(int, int, ArffData*); 

このようにしてsecond_loop()third_loop()にアクセスすると、エラー無くこれを行うことができます。アドバイスとして、私はのためにこれを行うことをお勧めします。このようなすべてのあなたの機能、:それは使用することができます前に、

#include <stdio.h> 
#include <stdlib.h> 
#include <stdint.h> 
#include <float.h> 
#include <math.h> 
#include <iostream> 
#include "libarff/arff_parser.h" 
#include "libarff/arff_data.h" 

using namespace std; 
//Forward declarations: 
int second_loop(int, ArffData*, float); 
float distance = third_loop(i, j, dataset); 
int* KNN(ArffData*); 
//End of Forward Declarations 
//Rest of code... 
+0

ありがとうArnav。私はまだ基​​本を学んでいます。それは本当に役立ちます。したがって、CPPでは、メソッドが呼び出される前にコンパイラに警告する必要があります。とった。ありがとうございました。 –

0

関数が宣言されなければなりません。 second_loopの中で使用する前にthird_loopを宣言していません。あなたはthird_loopを宣言しましたが、それはsecond_loopの定義の後です。それだけでは不十分です。

プログラムを修正するには、使用する前にthird_loopを宣言してください。

関連する問題