转换程序以返回带结构的指针

时间:2014-04-30 01:41:08

标签: c++

有人可以解释我需要做什么才能将此程序转换为在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 << "\n\nHow 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 << "\n\nGRADE (1-5): ";
            cin >> scouts[x].schoolGrade;

            cout << "\n\nDEN NAME: ";
            cin.ignore();
            getline(cin, scouts[x].denName);
            cin.sync();
    }   
    return *scouts; // This needs to be a pointer 
}

3 个答案:

答案 0 :(得分:0)

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 << "\n\nGRADE (1-5): ";
        cin >> scouts[x].schoolGrade;

        cout << "\n\nDEN 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;
    }
}

不确定它是否正在按照你的意愿

答案 1 :(得分:0)

您甚至不使用返回的值,因此您也可以使用void

这一行:

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

应该是:

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

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

CubScouts scouts[numScouts];

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

无论如何......从逻辑上讲,将指针返回给侦察员并没有多大意义;因为你的函数没有代码来检测输入失败。 main()对此指针的作用是什么?

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

return scouts + size;

答案 2 :(得分:0)

至少编译 - 不确定打印/应该做什么:

#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 
}