gpt4 book ai didi

C++ 错误非静态成员引用必须相对于特定对象

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:56 24 4
gpt4 key购买 nike

我正在尝试调用对象上的函数,但出现此错误。我有两个类(class):一个数独类(class)和一个拼图类(class)。 Sudoku 类继承了 Puzzle 类。 Sudoku 类具有 protected 变量,一个 Puzzle 对象的二维数组 Puzzle libraryOfPuzzles[4][10]; 当我尝试调用 libraryOfPuzzles[][]; ,它不起作用。但是,当我在函数本身内部创建的 Puzzle test[4][10]; 上调用它时,它起作用了。为什么是这样?

void Sudoku::createLibraries(string name) {
Puzzle test[4][10]; //added for demonstration purposes
string fileName = "";
int i = 0;
for (int k = 0; k < 40; k++) {
fileName = name + to_string(k) + ".txt";
string aWord;
ifstream fin;
fin.open(fileName);
i = 0;
int value = 0;
while (!fin.eof()) {
fin >> aWord;
int value = stringToInt(aWord);
libraryOfPuzzles[k / 10][k % 10].setSolution(i / 9, i % 9, value); //this line gives error
test[k / 10][k % 10].setSolution(i / 9, i % 9, value); //this line is fine
i++;
}
fin.close();
}
}

最佳答案

createLibraries 不能定义为静态。

关于C++ 错误非静态成员引用必须相对于特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50439154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com