你能告诉我为什么会出现这个错误吗?它涉及函数指针
can you tell me why this error appears? it concerns function pointers
main.cpp
#include <iostream>
#include "prototipo.hpp"
using namespace std;
int main(){
int num_elem;
int minimo;
void (*compare)(int* x, int* y);
compare comp;
comp = swap;
cout << "Inserire numero di elementi: ";
cin >> num_elem;
int *pointA = new int[num_elem];
ordinamentoArray (pointA, num_elem, comp);
}
prototipo.hpp
#ifndef PROTOTIPO_HPP
#define PROTOTIPO_HPP
void ordinamentoArray (int pointA[], int num_elem, compare comp);
void swap(int* x,int* y);
#endif
corpo.cpp
#include <iostream>
#include "prototipo.hpp"
using namespace std;
void swap(int* x, int* y){
int temp = *x;
*x = *y;
*y = temp;
}
void ordinamentoArray (int pointA[], int num_elem, compare comp){
int indice;
int indice2;
int temp;
for(indice=0; indice<num_elem; indice++){
for(indice2=indice; indice2>0 && pointA[indice2]<pointA[indice2-1]; indice2--){
if(pointA[indice2] < pointA[indice2-1]){
comp(&pointA[indice2], &pointA[indice2-1]);
}
}
}
}
这些是出现的错误。其中,最主要的是"error: 'compare' has not been declared"。老实说,我不知道错误是什么。谁能给我一个解决问题的有用提示?
不知道是我函数指针声明错误,还是参数传递错误
error: 'compare' has not been declared
void (*compare)(int* x, int* y);
compare =swap;
或
typedef void (*compare)(int* x, int* y);
compare comp;
comp = swap;
在此函数声明中
void ordinamentoArray (int pointA[], int num_elem, compare comp);
使用了尚未声明的名称compare
。
在函数 main 中定义了指向函数类型 void( int *, int * )
的指针(object 指针类型)compare
void (*compare)(int* x, int* y);
然后这个 object 不是类型说明符用于此语句
compare comp;
这没有意义。
您应该在函数声明之前的 header prototipo.hpp
中包含类型说明符比较的定义,例如
typedef void ( *compare )(int *, int *);
或喜欢
using compare = void ( * )( int *, int * );
在 main 中删除此声明后
void (*compare)(int* x, int* y);
也是指针pointA指向的分配数组
int *pointA = new int[num_elem];
传递给未初始化的函数
ordinamentoArray (pointA, num_elem, comp);
注意比较东西的函数看起来很奇怪returns什么都没有。通常 C++ 中的这样一个函数 returns 一个 object 隐式转换为类型 bool
.
main.cpp
#include <iostream>
#include "prototipo.hpp"
using namespace std;
int main(){
int num_elem;
int minimo;
void (*compare)(int* x, int* y);
compare comp;
comp = swap;
cout << "Inserire numero di elementi: ";
cin >> num_elem;
int *pointA = new int[num_elem];
ordinamentoArray (pointA, num_elem, comp);
}
prototipo.hpp
#ifndef PROTOTIPO_HPP
#define PROTOTIPO_HPP
void ordinamentoArray (int pointA[], int num_elem, compare comp);
void swap(int* x,int* y);
#endif
corpo.cpp
#include <iostream>
#include "prototipo.hpp"
using namespace std;
void swap(int* x, int* y){
int temp = *x;
*x = *y;
*y = temp;
}
void ordinamentoArray (int pointA[], int num_elem, compare comp){
int indice;
int indice2;
int temp;
for(indice=0; indice<num_elem; indice++){
for(indice2=indice; indice2>0 && pointA[indice2]<pointA[indice2-1]; indice2--){
if(pointA[indice2] < pointA[indice2-1]){
comp(&pointA[indice2], &pointA[indice2-1]);
}
}
}
}
这些是出现的错误。其中,最主要的是"error: 'compare' has not been declared"。老实说,我不知道错误是什么。谁能给我一个解决问题的有用提示? 不知道是我函数指针声明错误,还是参数传递错误
error: 'compare' has not been declared
void (*compare)(int* x, int* y);
compare =swap;
或
typedef void (*compare)(int* x, int* y);
compare comp;
comp = swap;
在此函数声明中
void ordinamentoArray (int pointA[], int num_elem, compare comp);
使用了尚未声明的名称compare
。
在函数 main 中定义了指向函数类型 void( int *, int * )
的指针(object 指针类型)compare
void (*compare)(int* x, int* y);
然后这个 object 不是类型说明符用于此语句
compare comp;
这没有意义。
您应该在函数声明之前的 header prototipo.hpp
中包含类型说明符比较的定义,例如
typedef void ( *compare )(int *, int *);
或喜欢
using compare = void ( * )( int *, int * );
在 main 中删除此声明后
void (*compare)(int* x, int* y);
也是指针pointA指向的分配数组
int *pointA = new int[num_elem];
传递给未初始化的函数
ordinamentoArray (pointA, num_elem, comp);
注意比较东西的函数看起来很奇怪returns什么都没有。通常 C++ 中的这样一个函数 returns 一个 object 隐式转换为类型 bool
.