我现在的代码有问题。我似乎无法得到我需要的东西并返回我的结构“b”的地址,以便其他函数可以使用它。如果你能帮助我,那就太好了!
这是我需要的代码:
int notFound = 0;
int choiceNumber;
int arraySize;
Basketball * b;
b = readFile(arraySize, notFound, &b);
这是我遇到问题的功能:
Basketball * readFile(int & arraySize, int & notFound, Basketball * &b)
{
ifstream inputFile;
inputFile.open("games.txt");
if(inputFile.fail())
{
cout << "The file name \"games.txt\" was not found!" << endl;
notFound = 1;
}
else
{
inputFile >> arraySize;
b = new Basketball [arraySize];
for (int i = 0; i < arraySize; i++)
{
inputFile >> b[i].visitTeam >> b[i].homeScore >> b[i].visitScore;
}
return & b;
}
}
我的构建错误是这些:
Error: invalid intialization of non-const reference of type basketball*& from an rvalue of type Basketball**
Error: In passing arguement 3 of Basketball* readfile(int&,int&, Basketball*&)
Error: Cannot convert Basketball** to Basketball* in return
如果您能为我指出正确的方向,那就太好了!
最佳答案
变量 b
已经是一个指针,使用 &b
将创建一个指向该指针的指针。删除运算符的地址 &
。
错误消息非常清楚,就像你返回一个指向指针的指针一样,当你声明只返回一个指针时。
关于c++ - 指针传递和参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29196524/