【发布时间】:2015-03-22 15:57:50
【问题描述】:
我的代码现在有问题。我似乎无法得到我需要的东西并返回我的结构“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
如果你能指出我正确的方向,那就太好了!
【问题讨论】:
-
你不需要“return & b;”中的 &这只是创建一个指向您的指针的指针。试试“return b;”
-
@jamolnng 修复了第三个错误。前两个是怎么回事?
-
好吧,在第一个中,您再次创建了指向指针的指针,所以这个 readFile(arraySize, notFound, &b);您应该在 b 之前删除 &。
-
请让你的标题描述问题,而不是仅仅列出两个广泛的编程主题。
标签: c++ pointers parameter-passing return-value