2011-02-23 66 views
0

頭文件誤差成員指針在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; 
} 

當我試圖編譯,我得到這個錯誤:

error C3867: 'Phone::getPhone': function call missing argument list; use '&Phone::getPhone' to create a pointer to member

我越來越這條線上的錯誤「if(checkNum(i1.getPhone))」。我創建了一個Phone對象,我試圖做的是使用函數checkNum來查看數組的第一個索引是否是一個數字。我指的是對象錯誤嗎?我應該使用間接選擇操作符嗎?任何想法我做錯了什麼?

感謝

回答

3

你缺少getPhone後一對括號中if (checkNum(i1.getPhone));它應該是if (checkNum(i1.getPhone()))

+0

OMG ......我一直在尋找在這個代碼,所以long..kept通過這個沒有注意到。哈哈謝謝! – Daniel 2011-02-23 07:44:36

2

行:

if (checkNum(i1.getPhone)) 

應該

if (checkNum(i1.getPhone()))