Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/09/2009, 00:57
Avatar de tofol
tofol
 
Fecha de Ingreso: febrero-2008
Mensajes: 30
Antigüedad: 16 años, 3 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