错误:"<methodname>"未在此范围内声明
error: ‘<methodname>’ was not declared in this scope
我最近遇到了我的cpp代码问题。我不是 cpp 开发人员,这个问题似乎有点令人困惑。代码作为流。
#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语法似乎是正确的,但仍然会出现此错误。
第三个循环有问题。在 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...
必须先声明函数,然后才能使用它。在 second_loop
中使用它之前,您没有声明third_loop
。您确实声明了third_loop
但仅在second_loop
的定义之后。这还不够。
若要修复程序,请在使用前声明third_loop
。
相关文章:
- 错误:未在此范围内声明'reverse'
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 未在此范围内声明错误 'xy'
- Socklen_t未在此范围内声明
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- "Main"已在当前范围内声明
- c++ 变量在宏的扩展中没有在这个范围内声明
- C++ 在编译过程中 strtok 函数 Eclipse 说没有在范围内声明?
- 'num1'未在此范围内声明
- 在此范围内未声明错误"Clreol"
- 如何找到在本地范围内声明的变量的地址?
- 简化在 Pybind11 中为 C++ 模板类生成包装类:模板声明不能出现在块范围内
- NT状态未在此范围内声明
- 变量未在此范围内声明 数组线性搜索
- 包含文件中的 Typdef "未在此范围内声明"
- 'ClassName'和'ClassInstance'未在此范围内声明
- "ChaCha"尚未在此范围内使用Crypto++声明?
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 如何在类声明范围内使用“using space::function”