在私有成员函数中非法引用非静态成员
Illegal Reference to Non-static Member in Private Member Function
我想写一个程序,但我不知道为什么我的私人成员函数不能访问我的私人数据成员。有人来帮忙吗?这是我的函数。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
如果我能提供更多的信息,请告诉我
代码创建了一个名为search
的非成员函数。您需要更改:
bool search(char * symbol)
:
bool ClassName::search(char * symbol)
将ClassName
替换为类名
函数是静态的,这就是为什么。静态函数只能访问类的静态成员。
编辑:实际上你必须澄清你的问题,因为其他答案也可能是正确的…
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 指向引用的指针是非法的
- 结构内部的结构:"对非静态成员的非法引用"错误
- 错误 C2234:引用数组是非法的
- C2597 非法引用非静态成员
- 为什么 如果类的成员是引用,则复制对象是非法的
- 非法引用非静态成员或未解析的外部符号
- 指向非法引用的成员的指针
- 在私有成员函数中非法引用非静态成员
- Cuda错误,在devicesync和cudamemcopy中引用了非法内存