C ++ request by member 'init' in 'mainRender', which is of type 'Renderer ()' which is not class

2

Hello I'm having problems with the methods of a class in my program, I think it's due to the distribution in different header files, I wanted to know in which particular cases this error occurs?

error: request by member 'init' in 'mainRender', which is of type 'Renderer ()' which is not class.

This is my code in predefs.cpp :

#include "predefs.h"
#include "allegro.h"
#include "renderer/renderer.h"

void init(){
    Renderer mainRender();
    allegro_init();
    install_keyboard();
    mainRender.init();
}
    
asked by Facundo Lorenzo 12.09.2016 в 00:15
source

2 answers

1

Typically, this type of error occurs when you want to access a supposed member of an entity, when in reality that entity is not a class or object of any kind.

For example:

int f();       // declaración de una función f, 
               // que no tiene parámetros 
               // y que devuelve int.

f.miembro = 5; // ¿Eh? 
               // no se puede llamar a un supuesto "miembro"
               // de f, porque f es una función
    
answered by 12.09.2016 / 00:57
source
0

Initializations.

C ++ has different ways to initialize values:

int a = 0.;    // Inicializamos un int con un literal double: estrechamiento.
int b = 0;     // Inicializacion "clasica".
int c(0);      // Inicializacion de estilo "funcional".
int d = int(); // Inicializacion con el valor por defecto de int (que es 0).

All these initializations are valid and initialize an integer, but all these initializations sometimes, besides being confusing to the programmer's view, can be confusing to the compiler's view:

int e();       // Esto es una declaracion de funcion no una declaracion de entero.
auto r = x(a); // Esto que es?

In the case of r if x is a function the type of r will be the return type of the function x but in the case that x is an object r sera of type x .

In the case of e and in the first case of r we are in what is known as "The parse more vexatious" (Most Vexing Parse in English).

More vexatious initialization.

Initializations with parentheses ( ( and ) ) can cause the most humiliating parsing in some contexts; This parsing is a type of syntactic ambiguity that the compiler can resolve in " unexpected " ways, for example the following code ...

// Un int llamado numero_natural?
int numero_natural();
// Un float llamado numero_real?
float numero_real();
// Un short llamado otro_natural inicializado con short()?
short otro_natural(short());
// Un double llamado otro_real inicializado con double()?
double otro_real(double());
// Un objeto de tipo Renderer llamado mainRender?
Renderer mainRender();

... is not what it seems, none of the four statements corresponds to a escalar because they are all statements of functions!

// Funcion que devuelve int y no recibe parametros
int numero_natural();
// Funcion que devuelve float y no recibe parametros
float numero_real();
/* Funcion que devuelve short y recibe como parametro una funcion
   que devuelve short y no recibe parametros */
short otro_natural(short());
/* Funcion que devuelve double y recibe como parametro una funcion
   que devuelve double y no recibe parametros */
double otro_real(double());
// Una funcion que devuelve Renderer y no recibe parametros
Renderer mainRender();

Initialization by keys {} .

To avoid this problem of ambiguity (and some problems of narrowing of types ) from C ++ 11, initialization with keys is allowed ( { and } ):

int a = {0.};  // ERROR! Se estrecha un double en un int!
int b = {0};   // Inicializacion a 0.
int c{0};      // Inicializacion a 0.
int d = int{}; // Inicializacion al valor por defecto de int (que es 0).
int e = {};    // Inicializacion al valor por defecto de int (que es 0).

Renderer mainRender{} // Esto YA NO ES UNA FUNCION, es una inicializacion

auto r = x(a); // Llamada a funcion o construccion de objeto? quien sabe!?
auto s = y{a}; // Construccion de objeto sin ningun tipo de duda.

Solution.

Apart from the solution suggested by asdasdasd you can also use the initialization by keys to solve your problem:

#include "predefs.h"
#include "allegro.h"
#include "renderer/renderer.h"

void init(){
    Renderer mainRender{}; // No mas vejaciones!!
    allegro_init();
    install_keyboard();
    mainRender.init();
}

Note that key initialization is only possible with compilers that support C ++ 11 or higher.

    
answered by 12.09.2016 в 15:26