无法使用指针访问类函数

时间:2016-10-04 09:39:39

标签: c++ function class pointers

当试图访问我的类成员的函数时,我无法在使用指针时找到它们。这构成了一个更大的项目的一部分,并尽可能简化了问题。请注意,错误发生在 void getthenumberhere ... 内部,它无法检测到getIDnumber函数:

#include <iostream>
#include <stdlib.h>
#include <vector>
#include "windows.h"
#include <stdio.h>
//#include <math.h>
#include <string>
using namespace std;

class Person
{
public:
    Person(int);
    ~Person();
    vector<RECT>* processrectangles;

    int Person::getIDnumber();
private:
    int IDnumber;

};

Person::Person(int x)
{
    IDnumber = x;
}
Person::~Person()
{
}

int Person::getIDnumber() {
    return IDnumber;
}

void getthenumberhere(vector<Person>* thisone) {
    int outID = *thisone[1].getIDnumber(); //IT CANT FIND THIS FUNCTION
}


int main() {
    int NextID = 1;

    vector<Person> People;
    Person newguy(1);
    People.push_back(newguy);
    getthenumberhere(&People);
    return 0;
}

1 个答案:

答案 0 :(得分:2)

使用括号来避免含糊不清:

((*thisone)[1]).getIDnumber();

您也可以

thisone->operator[](1).getIDnumber();

正如评论中所建议的那样。

此外,虽然声明写这个:

int getIDnumber();

而不是int Person::getIDnumber();。在声明成员函数时,不应使用解析运算符。