A pointer to a related function can only be used to call a function.

I am working on homework for my C ++ class and have run into a problem that cannot understand what I'm doing wrong.

Just note that file separation is necessary, and I understand that it would be much easier if I just created the structure AttackStylesinside mainand generally abandoned the extra class file.

At the heart of my problem is that I cannot skip the class loop and pull out the underlying data. Here is the code:

// 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;
}

My question is: why am I getting the error:

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

on ptrAsa->getStyleIdand ptrAsa->getStyleName?

I can’t understand what’s wrong with that!

+5
source share
3 answers

() . ptrAsa->getStyleId().

+15

,

ptrAsa->getStyleId() 

.

ptrAsa->getStyleId 

/ .

+6

You need to call the function, not just reference it:

    std::cout << "Style Id:\t" << ptrAsa->getStyleId() << "\n";
    std::cout << "Style Name:\t" << ptrAsa->getStyleName() << "\n";
+2
source

All Articles