2011-11-28 79 views
1

我想寫一個程序,但我不明白爲什麼我的私人會員功能不能訪問我的私人數據成員。有人請幫忙嗎?這是我的功能。 nStocks,capacity和slots []都是私有數據成員,而hashStr()是一個私有函數。私人會員功能中的非靜態成員非法引用

bool search(char * symbol) 
{ 
    if (nStocks == 0) 
      return false; 

    int   chain = 1; 
    bool   found = false; 

    unsigned int index = hashStr(symbol) % capacity; 

    if (strcmp(symbol, slots[index].slotStock.symbol) != 0) 
    { 
      int start = index; 
      index ++; 
      index = index % capacity; 
      while (!found && start != index) 
      { 
        if(symbol == slots[index].slotStock.symbol) 
        { 
          found = true; 
        } 
        else 
        { 
          index = index % capacity; 
          index++; 
          chain++; 
        } 
      } 
      if (start == index) 
        return false; 
    } 

    return true; 
} 

這裏是我的.h文件的私有成員部分:

private: 
    static unsigned int hashStr(char const * const symbol); // hashing function 
    bool search(char * symbol); 

    struct Slot 
    { 
      bool occupied; 
      Stock slotStock; 
    }; 

    Slot *slots;      // array of instances of slot 
    int capacity;     // number of slots in array 
    int nStocks;     // current number of stocks stored in hash table 

請讓我知道如果我可以提供任何其他信息。

+1

是:什麼是編譯器錯誤信息? (具體來說,它指的是哪一行代碼?) –

回答

4

你的代碼創建一個名爲search非成員函數。您需要更改:

bool search(char * symbol) 

要:

bool ClassName::search(char * symbol) 

更換ClassName與類的名稱。

3

你的功能是靜態的,這就是爲什麼。靜態函數只能訪問類的靜態成員。

編輯:其實你必須明確你的問題是對方的回答可能是正確的也...

相關問題