无法使用指针访问类函数

Unable to access class function using pointers

本文关键字:访问 类函数 指针      更新时间:2023-10-16

当试图访问我的类成员的函数时,我无法在使用指针时找到它们。这构成了一个更大项目的一部分,并尽可能地简化了问题。注意,错误发生在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;
}

使用括号避免歧义:

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

你也可以做

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

同时,在声明时也要这样写:

int getIDnumber();

代替int Person::getIDnumber();。在声明成员函数时不应该使用解析操作符。