C++ 调用函数指针会导致错误
我正在尝试使用指针算法构建对象的动态数组。但是,编译器在main.cpp的此行中返回以下错误C++ 调用函数指针会导致错误,c++,C++,我正在尝试使用指针算法构建对象的动态数组。但是,编译器在main.cpp的此行中返回以下错误 (*(lista+n))(id1,seleccion1,edad1,camiseta1); 欢迎任何建议,谢谢 这是一节课 class jugador { private: int id; short seleccion; short edad; short camiseta; public: jugador(); jugador(int ID,
(*(lista+n))(id1,seleccion1,edad1,camiseta1);
欢迎任何建议,谢谢 这是一节课
class jugador
{
private:
int id;
short seleccion;
short edad;
short camiseta;
public:
jugador();
jugador(int ID, short SELECCION, short EDAD, short CAMISETA);
int obtener_id();
short obtener_seleccion();
short obtener_edad();
short obtener_camiseta();
void cambiar_id(int nueva_id);
void cambiar_seleccion(short nueva_seleccion);
void cambiar_edad(short nueva_edad);
void cambiar_camiseta(short nueva_edad);
void cambiar_todo(int nueva_ID, short nueva_SELECCION, short nueva_EDAD, short nueva_CAMISETA);
void mostrar_jugador();
};
施工人员
jugador::jugador()
{
id=999999;
seleccion=32;
edad=99;
camiseta=99;
}
jugador::jugador(int ID, short SELECCION, short EDAD, short CAMISETA)
{
id=ID;
seleccion=SELECCION;
edad=EDAD;
camiseta=CAMISETA;
}
.您不使用
std::vector是否有特殊原因
请检查此项,了解用vector
替换realloc
的优点
您没有提供足够的信息,因此我可以从错误的外观看出:
(*(列表A+n))(id1,选择1,edad1,camiseta1)代码>
这不是函数指针,它甚至一开始都没有指向函数
似乎您正试图通过移动lista
指针来构造jugador
的数组。如果这是您想要做的,那么您可以进行后期初始化
jugador * lista; //< unitialized pointer
int n = 11; //< your number of players, lets suppose 11
lista = new jugador[11]; // now you have an array of jugadores
for(int i = 0; i != n; ++i)
{
lista[i] = jugador(id1,seleccion1,edad1,camiseta1);
}
// use your jugadores, let's suppose you want to use the tenth jugador
jugador *iterator = lista;
iterator+10;
use(*iterator); //*iterator variable holds your 10th jugador object
delete[] lista;
我完全同意克劳迪奥德吉斯的回答。但是,如果希望使用参数调用构造函数(而不制作额外副本),则需要制作指针数组而不是对象数组。我正在粘贴一个版本的代码。然而,我仍然认为使用向量的版本更安全、更优越
代码:
intmain()
{
INTID1;
短选择1、edad1、camiseta1;
jugador arreglo[5];
int n=0,i;
char opcion='s';
jugador**lista=NULL;
而(opcion=='s')
{
lista=新的jugador*[n];
可以从外观(*(列表A+n))(id1、选择1、edad1、camiseta1)
似乎是对函数指针的一种令人毛骨悚然的误解。但既然你推迟了它,编译器就会寻找一个对象。我们需要知道你的意图。你能告诉我们主要的部分吗?或者具体的部分吗?你还没有告诉我们导致错误的代码。请发表一篇文章。你的代码中到底有什么带n的东西吗ameplayer
?与其在错误消息上运行翻译器,不如在语言设置设置为英语的情况下运行编译器。lista
是jugador
对象的数组吗?我已经理解了您的问题,通过这种解释realloc
是一个错误的选择,您应该使用new
和删除
,如果您需要进一步的jugador
对象,则分配更多内存。您甚至可以分配100个jugador
,只使用10个。realloc
是一个c函数,可以与c++
中的对象组合。它被命名为new
并不意味着您不能动态更改它。您仍然需要我会对lista=new jugador[11]这一行有问题,因为它当时正在调用隐式构造函数。我同意使用向量的版本更优越,但如果他仍然想使用数组,那么他必须有一个指针数组来代替我读你说的…但我能读的只是“你想要指针算术吗?我给你指针算术!"但是,是的,这样您就不会调用默认构造函数,也不会创建副本+1@Claudiordgz是的,例如,我不知道如何指向具有重新分配的构造函数。根据您的建议,我可以轻松解决问题。在这之后,我对重新分配有了更多的了解,并可以使用它生成一个工作代码,但我看不到调用构造函数的方法。@user3443151是一个错误,它是一个实用的记忆库,它是一个为
llamar el构造函数而设计的。laúnica manera de llamar el constructor desde la alocación de memoria de la sugerencia dePedrom
de utilizar un arred glo de apuntadores a apu恩塔多雷斯。
jugador * lista; //< unitialized pointer
int n = 11; //< your number of players, lets suppose 11
lista = new jugador[11]; // now you have an array of jugadores
for(int i = 0; i != n; ++i)
{
lista[i] = jugador(id1,seleccion1,edad1,camiseta1);
}
// use your jugadores, let's suppose you want to use the tenth jugador
jugador *iterator = lista;
iterator+10;
use(*iterator); //*iterator variable holds your 10th jugador object
delete[] lista;
// let's say in this point you need 20 jugador more
jugador * newlista = new jugador[n+20];
std::copy(lista, lista+11, newlista);
delete[] lista; //you delete the old buffer
for(int i = 11; i != n+20; ++i)
{
newlista[i] = jugador(id1,seleccion1,edad1,camiseta1);
}
// and now newlista has your jugadores, you can even make a function that does that
delete[] newlista ; // delete your jugadores
int main()
{
int id1;
short seleccion1, edad1, camiseta1;
jugador arreglo[5];
int n = 0, i;
char opcion = 's';
jugador **lista=NULL;
while (opcion == 's')
{
lista = new jugador*[n];
cout<<"id: "<<endl;
cin>>id1;
cout<<"Seleccion: "<<endl;
cin>>seleccion1;
cout<<"Edad: "<<endl;
cin>>edad1;
cout<<"Camiseta: "<<endl;
cin>>camiseta1;
lista[n] = new jugador(id1,seleccion1,edad1,camiseta1);
n++;
cout << "Desea ingresar otro elemento? (s/n): ";
cin >> opcion;
}
cout << "\nArreglo completo\n";
for (i=0; i<n; i++)
{
lista[n].mostrar_jugador();
}
//deallocating memory
for (int i=0; i<n; i++)
{
delete jugador[i];
}
delete [] jugador;
return 0;
}