【问题标题】:When calling a member function of a class, I get Error C3867调用类的成员函数时,出现错误 C3867
【发布时间】:2019-02-17 20:59:26
【问题描述】:

在我的汽车类的 displayData 成员函数中,错误说我应该创建一个指向该成员的指针,我是否必须创建指向该成员的指针?如果我这样做怎么办?我完全忘记了指针。我是否将对象设为指针然后指向 displayData 成员函数?我没有收到红色波浪线,只是一条错误消息说“使用 '&' 创建指向成员的指针。我试过但没有运气。

#include<iostream>
#include<string>

using namespace std;
class Car
{
private:
int xVal, yVal, zVal;

protected:

public:
Car() { int xVal = 0; int yVal = 0; int zVal = 0; }
Car(int x,int y,int z) { xVal = x; yVal = y; zVal = z; }
~Car() {};
int getX() { return xVal; }
int getY() { return yVal; }
int getZ() { return zVal; }
void changeX(int n) { xVal = n; }
void changeY(int n) { yVal = n; }
void changez(int n) { zVal = n; }
virtual void getData();
void displayData();

};

class Sensor : public Car
{
private:
string sensorType;

protected:

public:
Sensor() { sensorType = "EMPTY"; }
Sensor(int x, int y, int z, string type) :Car(x,y,z) { sensorType = type; }


};

void Car::displayData()
{
cout << "The x values is: " << getX() << endl;
cout << "The y values is: " << getY() << endl;
cout << "The z values is: " << getZ() << endl;
}

int main()
{
Sensor n1;
Sensor n2(20,30,40, "Accelerometer");
n1.displayData;
n2.displayData;

return 0;
}

【问题讨论】:

    标签: class pointers member-function-pointers


    【解决方案1】:

    调用函数时出现语法错误。

    应该是

    n1.displayData();
    

    而不是

    n1.displayData;
    

    【讨论】:

    • 是的,不是要删除它,但仍然对多态性感到困惑
    • 请详细说明您特别困惑的事情
    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多