Naveen Balasubramanian Naveen Balasubramanian - 2 months ago 15
C++ Question

error: ‘<methodname>’ was not declared in this scope

I recently facing an issue with my cpp code. I'm not a cpp developer and this issue seems to be bit confusing. The code is as flows.

#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);
}


The error getting thrown is as follows.

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);
^


Can anyone help me out with this error. The third_loop syntax seems to be right and still this error comes up.

Thanks.

Answer

You have a problem with third loop. In second_loop(), you access the function third_loop() even though the compiler hasn't seen it yet here:

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

The optimal solution would be forward declare the function in the beginning of the file, like this:

float third_loop(int, int, ArffData*);

This way, when second_loop() accesses third_loop(), it will be able to do so without error. As advice, I recommend doing this for all your functions, like this:

#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...
Comments