繁体   English   中英

C ++中指向成员的指针错误

[英]Error for pointer to member in C++

头文件

#include <iostream>
#include <cstring>

const unsigned MaxLength = 11;

class Phone {
public:

    Phone(const char *phone) {
    setPhone(phone);
    }

    void        setPhone(const char Phone[ ]);
    const char* getPhone();

private:
    char phone[MaxLength+1];
};

Cpp文件

#include "Phone.h"
#include <iostream>
#include <ctype.h>
#include <cstring>

using namespace std; 
bool checkNum(char num[]);

void Phone::setPhone(const char Phone[ ]) {
    strncpy(phone, Phone, MaxLength);
    phone[MaxLength] = '\0';
}

const char* Phone::getPhone() {
    return phone;
}


int main() {
    Phone i1("12345678901");

    cout << i1.getPhone() << endl;
    if (checkNum(i1.getPhone)) 
        cout << "Correct" << endl;
    else 
        cout << "Invalid Wrong" << endl;

}

bool checkNum(char num[]) {
    bool flag = true;
        if (isdigit(num[0]) == 0)
            flag = false;
    return flag;
}

当我尝试编译时,出现以下错误:

错误C3867:'Phone :: getPhone':函数调用缺少参数列表; 使用'&Phone :: getPhone'创建一个指向成员的指针

我在“ if(checkNum(i1.getPhone))”这一行出现错误。 我创建了一个Phone对象,然后尝试使用checkNum函数查看数组的第一个索引是否为数字。 我引用的对象有误吗? 我应该改用间接选择运算符吗? 知道我在做什么错吗?

谢谢

if (checkNum(i1.getPhone))中,在getPhone之后缺少一对括号; 应该是if (checkNum(i1.getPhone()))

该行:

if (checkNum(i1.getPhone))

应该

if (checkNum(i1.getPhone()))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM