2012-02-18 17 views
5

Estoy trabajando en una tarea para mi clase C++ y me he encontrado con un problema que no puedo entender lo que estoy haciendo mal.Un puntero a una función encuadernada solo se puede usar para llamar a la función

Solo para observar, la separación de los archivos es necesaria y me doy cuenta de que esto sería mucho más fácil si acabo de hacer una estructura AttackStyles dentro del main y renuncio por completo al archivo de clase adicional.

La base de mi problema es que parece que no puedo recorrer un conjunto de clases y extraer los datos base. Aquí está el código:

// AttackStyles.h 
#ifndef ATTACKSTYLES_H 
#define ATTACKSTYLES_H 
#include <iostream> 
#include <string> 

using namespace std; 

class AttackStyles 
{ 
private: 
    int styleId; 
    string styleName; 

public: 
    // Constructors 
    AttackStyles(); // default 
    AttackStyles(int, string); 

    // Destructor 
    ~AttackStyles(); 

    // Mutators 
    void setStyleId(int); 
    void setStyleName(string); 

    // Accessors 
    int getStyleId(); 
    string getStyleName(); 

    // Functions 

}; 
#endif 


///////////////////////////////////////////////////////// 
// AttackStyles.cpp 
#include <iostream> 
#include <string> 
#include "AttackStyles.h" 
using namespace std; 


// Default Constructor 
AttackStyles::AttackStyles()  
{} 

// Overloaded Constructor 
AttackStyles::AttackStyles(int i, string n) 
{ 
    setStyleId(i); 
    setStyleName(n); 
} 

// Destructor 
AttackStyles::~AttackStyles()  
{} 

// Mutator 
void AttackStyles::setStyleId(int i) 
{ 
    styleId = i; 
} 

void AttackStyles::setStyleName(string n) 
{ 
    styleName = n; 
} 

// Accessors 
int AttackStyles::getStyleId() 
{ 
    return styleId; 
} 

string AttackStyles::getStyleName() 
{ 
    return styleName; 
} 


////////////////////////////////////////////// 
// main.cpp 
#include <cstdlib> 
#include <iostream> 
#include <string> 
#include "attackStyles.h" 

using namespace std; 

int main() 
{ 
    const int STYLE_COUNT = 3; 
    AttackStyles asa[STYLE_COUNT] = {AttackStyles(1, "First"), 
            AttackStyles(2, "Second"), 
            AttackStyles(3, "Third")}; 

    // Pointer for the array 
    AttackStyles *ptrAsa = asa; 

    for (int i = 0; i <= 2; i++) 
    { 
     cout << "Style Id:\t" << ptrAsa->getStyleId << endl; 
     cout << "Style Name:\t" << ptrAsa->getStyleName << endl; 
     ptrAsa++; 
    } 

    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

Mi pregunta es ¿por qué recibo el error:

"a pointer to a bound function may only be used to call the function" 

tanto ptrAsa->getStyleId y ptrAsa->getStyleName?

¡No entiendo qué está mal con esto!

Respuesta

15

Te echa en falta () llamadas a la función. Debe ser ptrAsa->getStyleId().

+1

OMG! Ahora me siento realmente tonto. ¡Gracias chicos! – Kardsen

6

Te faltan paréntesis sobre ambas llamadas, debe ser

ptrAsa->getStyleId() 

llamar a la función.

ptrAsa->getStyleId 

se utiliza para hacer referencia a un valor/atributo de miembro.

+0

¡OMG! Voy a arrastrarme de vuelta a mi agujero ahora. Extremadamente triste por la estúpida pregunta jajaja. – Kardsen

+0

le sucede a todos nosotros =) –

2

Es necesario invocar la función, no simplemente hacer referencia a ella:

std::cout << "Style Id:\t" << ptrAsa->getStyleId() << "\n"; 
    std::cout << "Style Name:\t" << ptrAsa->getStyleName() << "\n"; 
Cuestiones relacionadas