Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Error con punteros y struct

Estas en el tema de Error con punteros y struct en el foro de C/C++ en Foros del Web. Hola, estoy intentando ejecutar un programa que viene en los apuntes de la librería GSL pero me da fallo. Lo hago desde Linux con Geany. ...
  #1 (permalink)  
Antiguo 15/09/2009, 00:57
Avatar de tofol  
Fecha de Ingreso: febrero-2008
Mensajes: 30
Antigüedad: 16 años, 2 meses
Puntos: 1
Pregunta Error con punteros y struct

Hola, estoy intentando ejecutar un programa que viene en los apuntes de la librería GSL pero me da fallo. Lo hago desde Linux con Geany. Pongo el código:

demo_fn.h

Código c++:
Ver original
  1. struct parametros_cuadratica
  2. {
  3.     double a,b,c;
  4. };
  5. double cuadratica(double x, void *parametros);
  6. double deriv_cuadratica(double x, void *parametros);
  7. void cuadratica_fdf(double x, void *parametros, double *y, double *dy);

demo_fn.cpp

Código c++:
Ver original
  1. //      demo_fn.cpp
  2. //      
  3. //      Copyright 2009 Cristobal Lopez
  4. //      
  5. //      This program is free software; you can redistribute it and/or modify
  6. //      it under the terms of the GNU General Public License as published by
  7. //      the Free Software Foundation; either version 2 of the License, or
  8. //      (at your option) any later version.
  9. //      
  10. //      This program is distributed in the hope that it will be useful,
  11. //      but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //      GNU General Public License for more details.
  14. //      
  15. //      You should have received a copy of the GNU General Public License
  16. //      along with this program; if not, write to the Free Software
  17. //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. //      MA 02110-1301, USA.
  19.  
  20.  
  21. #include <iostream>
  22.  
  23. double cuadratica(double x, void *parametros)
  24. {
  25.     struct parametros_cuadratica *p = (struct parametros_cuadratica *) parametros;
  26.    
  27.     double a = p->a;
  28.     double b = p->b;
  29.     double c = p->c;
  30.    
  31.     return (a*x+b)*x+c;
  32. }
  33. double deriv_cuadratica(double x, void *parametros)
  34. {
  35.     struct parametros_cuadratica *p = (struct parametros_cuadratica *) parametros;
  36.    
  37.     double a = p->a;
  38.     double b = p->b;
  39.     double c = p->c;
  40.    
  41.     return 2.0*a*x+b;
  42. }
  43.  
  44. void cuadratica_fdf(double x, void *parametros, double *y, double *dy)
  45. {
  46.     struct parametros_cuadratica *p = (struct parametros_cuadratica *) parametros;
  47.    
  48.     double a = p->a;
  49.     double b = p->b;
  50.     double c = p->c;
  51.    
  52.     *y = (a*x+b)*x+c;
  53.     *dy = 2.0*a*x+b;
  54. }

main.cpp

Código c++:
Ver original
  1. //      main.cpp
  2. //      
  3. //      Copyright 2009 Cristobal Lopez
  4. //      
  5. //      This program is free software; you can redistribute it and/or modify
  6. //      it under the terms of the GNU General Public License as published by
  7. //      the Free Software Foundation; either version 2 of the License, or
  8. //      (at your option) any later version.
  9. //      
  10. //      This program is distributed in the hope that it will be useful,
  11. //      but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //      GNU General Public License for more details.
  14. //      
  15. //      You should have received a copy of the GNU General Public License
  16. //      along with this program; if not, write to the Free Software
  17. //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. //      MA 02110-1301, USA.
  19.  
  20.  
  21. #include <iostream>
  22. #include <gsl/gsl_errno.h>
  23. #include <gsl/gsl_math.h>
  24. #include <gsl/gsl_roots.h>
  25.  
  26. #include "demo_fn.h"
  27. #include "demo_fn.cpp"
  28.  
  29. using namespace std;
  30.  
  31. int main(int argc, char** argv)
  32. {
  33.     int estado, iter = 0, max_iter = 100;
  34.     const gsl_root_fsolver_type *T;
  35.     gsl_root_fsolver *s;
  36.     double r = 0, r_esperada = sqrt(5.0);
  37.     double x0 = 0.0, x_hi = 5.0;
  38.    
  39.     gsl_function F;
  40.     struct parametros_cuadratica parametros = {1.0, 0.0, -5.0};
  41.    
  42.     F.function = &cuadratica;
  43.     F.parametros = &parametros;
  44.     T = gsl_root_fsolver_brent;
  45.     s = gsl_root_fsolver_alloc(T);
  46.     gsl_root_fsolver_set(s, &F, x0, x_hi);
  47.    
  48.     cout<<"Utilizando el metodo"<<gsl_root_fsolver_name (s)<<endl;
  49.     printf("%5s [%9s, %9s] %9s %10s %9s\n", "iter", "inferior", "superior",
  50.     "raiz", "error", "error(est)");
  51.    
  52.     do
  53.     {
  54.         iter++;
  55.         estado = gsl_root_fsolver_iterate (s);
  56.         r = gsl_root_fsolver_root(s);
  57.         x0 = gsl_root_fsolver_x_lower(s);
  58.         x_hi = gsl_root_fsolver_x_upper(s);
  59.         estado = gsl_root_test_interval(x0, x_hi, 0, 0.001);
  60.        
  61.         if(estado == GSL_SUCCESS)
  62.             cout<<"Solucion"<<endl;
  63.         printf("%5d [%.7f, %.7f] %.7f %+.7f %.7f\n", iter, x0, x_hi, r,
  64.         r-r_esperada, x_hi-x0);
  65.     }
  66.     while(estado == GSL_CONTINUE && iter < max_iter);
  67.    
  68.     gsl_root_fsolver_free(s);
  69.    
  70.     return estado; 
  71.     return 0;
  72. }

Y el mensaje de error es el siguiente:

Cita:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:43: error: ‘struct gsl_function’ no tiene un miembro llamado ‘parametros’
Las opciones de compilación con GSL son:

Cita:
g++ -Wall -c "main.cpp" -lgsl -lgslcblas -lm
Las GSL son estas librerias: buscad en google porque el foro no me deja poner el enlace por motivos de antispam.
Si alguien me pudiese echar un cable se lo agradecería, ya que aprendo GSL por mi cuenta en mis ratos libres y no tengo a quien consultar.

Muchas gracias por adelantado
  #2 (permalink)  
Antiguo 15/09/2009, 09:03
Avatar de Eternal Idol  
Fecha de Ingreso: mayo-2004
Ubicación: Lucentum
Mensajes: 6.192
Antigüedad: 20 años
Puntos: 74
Respuesta: Error con punteros y struct

El error indica que parametros no es un miembro de la estructura gsl_function, no tengo ganas de bajarme la libreria pero no sera parameters en lugar de parametros:

Código:
F.parameters = &parametros;
__________________
¡Peron cumple, Evita dignifica! VIVA PERON CARAJO
  #3 (permalink)  
Antiguo 15/09/2009, 09:29
Avatar de Anubis_Slash  
Fecha de Ingreso: mayo-2009
Ubicación: aqui y haya
Mensajes: 173
Antigüedad: 15 años
Puntos: 5
Respuesta: Error con punteros y struct

hola

me parece que la opcion correcta es asi:

Código:
F.params = &parametros;
saludos
__________________
Bienvenidos los karmas.
Quejas, sugerencias o dudas, se awantan......
  #4 (permalink)  
Antiguo 15/09/2009, 09:32
Avatar de tofol  
Fecha de Ingreso: febrero-2008
Mensajes: 30
Antigüedad: 16 años, 2 meses
Puntos: 1
Respuesta: Error con punteros y struct

¡Muchas gracias a ambos! Solucionado
Y sí Anubis es params.
Al intentar traducir las variables al castellano me cegué y quise españolizar los miembros de gsl_function
Estoy ciego

Gracias

Saludos
  #5 (permalink)  
Antiguo 15/09/2009, 09:42
Avatar de Eternal Idol  
Fecha de Ingreso: mayo-2004
Ubicación: Lucentum
Mensajes: 6.192
Antigüedad: 20 años
Puntos: 74
Respuesta: Error con punteros y struct

Casi
__________________
¡Peron cumple, Evita dignifica! VIVA PERON CARAJO
  #6 (permalink)  
Antiguo 15/09/2009, 10:02
Avatar de Anubis_Slash  
Fecha de Ingreso: mayo-2009
Ubicación: aqui y haya
Mensajes: 173
Antigüedad: 15 años
Puntos: 5
Respuesta: Error con punteros y struct

que bueno que solucionaste el problema.....

por poco lo atinas Eternal.....

Saludos a ambos
__________________
Bienvenidos los karmas.
Quejas, sugerencias o dudas, se awantan......
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 15:27.