c++ - 指针传递和参数

标签 c++ pointers parameter-passing return-value

我现在的代码有问题。我似乎无法得到我需要的东西并返回我的结构“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/

相关文章:

c++ - WaitForMultipleObjects 失败 C++

c++ - 如何在C++程序的stacktrace中查看有用的信息(文件名,行号)

c++ - 如何在 MFC 应用程序中加载 VC++ CLR 库?

c - 错误: invalid type argument of 'unary *' (have 'int' )|

pointers - 结构指针方法中的指针可以重新分配给另一个实例吗?

c++ - 如何使用ifstream作为函数参数?

c++ - 在 '*' 标记之前缺少模板参数

c - 意外的 C 结构指针大小行为

sql - ms sql 存储过程返回数据没有输出

jsf - Icefaces Actionlistener 将参数传递到另一个页面