C++ 你能告诉我为什么会出现这个错误吗?它涉及函数指针
main.cppC++ 你能告诉我为什么会出现这个错误吗?它涉及函数指针,c++,function-pointers,typedef,typespec,alias-declaration,C++,Function Pointers,Typedef,Typespec,Alias Declaration,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_el
#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);
}
#ifndef PROTOTIPO_HPP
#define PROTOTIPO_HPP
void ordinamentoArray (int pointA[], int num_elem, compare comp);
void swap(int* x,int* y);
#endif
#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]);
}
}
}
}
corpo.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);
}
#ifndef PROTOTIPO_HPP
#define PROTOTIPO_HPP
void ordinamentoArray (int pointA[], int num_elem, compare comp);
void swap(int* x,int* y);
#endif
#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]);
}
}
}
}
#包括
#包括“prototipo.hpp”
使用名称空间std;
无效交换(int*x,int*y){
int temp=*x;
*x=*y;
*y=温度;
}
无效序号数组(int pointA[],int num_elem,comp比较){
int-indice;
int指标2;
内部温度;
对于(指标=0;指标0&&pointA[指标2]
错误:“比较”尚未声明
或
错误:“比较”尚未声明
或
在这个函数声明中
void ordinamentoArray (int pointA[], int num_elem, compare comp);
void (*compare)(int* x, int* y);
使用了尚未声明的名称compare
在函数main中定义了指针(指针类型的对象)compare
与函数类型void(int*,int*)
然后这个不是类型说明符的对象被用在这个语句中
compare comp;
这没有道理
您应该在函数声明之前的标题prototipo.hpp
中包含类型说明符compare的定义
typedef void ( *compare )(int *, int *);
或者
using compare = void ( * )( int *, int * );
之后,主要删除此声明
void ordinamentoArray (int pointA[], int num_elem, compare comp);
void (*compare)(int* x, int* y);
指针指向的已分配数组pointA
int *pointA = new int[num_elem];
传递给未初始化的函数
ordinamentoArray (pointA, num_elem, comp);
注意,比较某物的函数看起来没有什么奇怪的东西。通常C++中的这种函数将一个对象隐式转换为类型<代码>布尔O/< > > < /p> 在这个函数声明< /p>中。
void ordinamentoArray (int pointA[], int num_elem, compare comp);
void (*compare)(int* x, int* y);
使用了尚未声明的名称compare
在函数main中定义了指针(指针类型的对象)compare
与函数类型void(int*,int*)
然后这个不是类型说明符的对象被用在这个语句中
compare comp;
这没有道理
您应该在函数声明之前的标题prototipo.hpp
中包含类型说明符compare的定义
typedef void ( *compare )(int *, int *);
或者
using compare = void ( * )( int *, int * );
之后,主要删除此声明
void ordinamentoArray (int pointA[], int num_elem, compare comp);
void (*compare)(int* x, int* y);
指针指向的已分配数组pointA
int *pointA = new int[num_elem];
传递给未初始化的函数
ordinamentoArray (pointA, num_elem, comp);
<>注意,比较事物的功能看起来奇怪,通常在C++中的函数返回一个对象,隐式地转换为类型<代码> Boo.<代码>。< /p>不应该比较代码吗?COMP=交换;< />代码>代码>比较=和交换< /COD> >缺少代码> TyPulf< /Cord>。这是C++。难道这些<代码>不应该比较COMP吗?COMP=交换;< /C> >只是代码>比较=和交换<代码> >丢失<代码> TyPulfF/Cuth>这是C++,不是C。谢谢指导。我完全错了把它放在“主”中。而不是.hpp文件。至于空数组,我只是省略了populate函数,以便让您能够更流畅地阅读我认为有问题的代码。非常感谢您提供的指导。我把它放在“main”中是完全错误的与.hpp文件不同,对于空数组,我只是省略了populate函数,以便更流畅地阅读我认为有问题的代码。