2011-11-28 78 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

你的功能是静态的,这就是为什么。静态函数只能访问类的静态成员。

编辑:其实你必须明确你的问题是对方的回答可能是正确的也...

相关问题