正在转换程序以返回具有结构的指针

Converting Program to return a Pointer with a structure

本文关键字:结构 指针 返回 转换 程序      更新时间:2023-10-16

有人能解释一下我需要做什么才能将这个程序转换为返回enterCubScouts函数中的指针吗?我已经尝试了我所知道的一切,但都不起作用。我读到一些关于使用->而不是正常的东西,但我有点困惑。您是将->与*同时使用,还是仅使用->。

 #include <iostream>
using namespace std;
struct CubScouts
{
    string name;
    int schoolGrade;
    string denName;
};
CubScouts *enterCubScouts(CubScouts *scouts[], int);
void printCubScouts(CubScouts scouts[], int);
int main()
{
    int numScouts;
    cout << "nnHow many cub scouts are in your pack?n";
    cin >> numScouts;
    cin.ignore();
    CubScouts scouts[numScouts];
    enterCubScouts(scouts, numScouts);

    printCubScouts(scouts, numScouts);
    return 0;
}
CubScouts *enterCubScouts(CubScouts *scouts[],int size)
{
    for(int x=0; x<size; x++)
    {
            cout << "nCUB SCOUT " << x+1 << ": n";
            cout << "NAME: ";
            getline(cin, scouts[x].name);
            cout << "nnGRADE (1-5): ";
            cin >> scouts[x].schoolGrade;
            cout << "nnDEN NAME: ";
            cin.ignore();
            getline(cin, scouts[x].denName);
            cin.sync();
    }   
    return *scouts; // This needs to be a pointer 
}

http://www.functionx.com/cpp/examples/returnpointer.htm

CubScouts * enterCubScouts(CubScouts *scouts[], int size)

也许吧?

我认为你只需要添加一个星号

将在c++编译器中尝试此操作;真不敢相信我这台机器上没有

这对我有效:

CubScouts * enterCubScouts(CubScouts scouts[], int size)
{
    for (int x = 0; x<size; x++)
    {
        cout << "nCUB SCOUT " << x + 1 << ": n";
        cout << "NAME: ";
        getline(cin, scouts[x].name);
        cout << "nnGRADE (1-5): ";
        cin >> scouts[x].schoolGrade;
        cout << "nnDEN NAME: ";
        cin.ignore();
        cin.sync();
    }
    return scouts; // This needs to be a pointer 
}
void printCubScouts(CubScouts scouts[], int size)
{
    for (int x = 0; x<size; x++)
    {
         cout << scouts[x].name << " " << scouts[x].denName << " " << scouts[x].schoolGrade;
    }
}

不确定它是否在做你想让它做的事情

您甚至不使用返回的值,因此不妨将其设为void

此行:

CubScouts *enterCubScouts(CubScouts *scouts[],int size)

应该是:

CubScouts *enterCubScouts(CubScouts scouts[],int size)

(并更新原型)。按照当前的方式,应该会生成编译器错误。此外,这一行:

CubScouts scouts[numScouts];

应该生成编译器错误,因为数组在C++中不能具有运行时大小。尝试在标准模式下调用编译器。如果它似乎对您有效,我仍然建议使用标准容器而不是编译器扩展,因为我们确实有一份文档准确描述了标准容器的工作方式。

不管怎样。。。从逻辑上讲,将指针返回给侦察员没有多大意义;因为您的函数没有检测输入失败的代码。main()将如何处理此指针?

如果你添加了一些错误检查,那么你可以返回一个指针,指示成功输入的侦察机列表的末尾,例如

return scouts + size;

这至少编译了——不确定打印应该做什么:

#include <iostream>
using namespace std;
struct CubScouts
{
string name;
int schoolGrade;
string denName;
};
CubScouts *enterCubScouts(int x);
//void printCubScouts(CubScouts*[], int);
int main()
{
int numScouts;
cout << "nnHow many cub scouts are in your pack?n";
cin >> numScouts;
cin.ignore();
CubScouts *scouts[numScouts];
for(int x=0; x<numScouts; x++){

scouts[x] = enterCubScouts(x);
}
//printCubScouts(scouts, numScouts);
return 0;
}
CubScouts *enterCubScouts(int x)
{
 CubScouts *scout = new CubScouts;
        cout << "nCUB SCOUT " << x+1 << ": n";
        cout << "NAME: ";
        getline(cin, scout->name);
        cout << "nnGRADE (1-5): ";
        cin >> scout->schoolGrade;
        cout << "nnDEN NAME: ";
        cin.ignore();
        getline(cin, scout->denName);
        cin.sync();
return scout; // This needs to be a pointer 
}