正在转换程序以返回具有结构的指针
Converting Program to return a Pointer with a structure
有人能解释一下我需要做什么才能将这个程序转换为返回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
}
相关文章:
- 如何在 C# 中映射双 C 结构指针?
- C++:映射结构中的结构指针
- C++:添加新结构时,结构指针向量中的所有元素都会更新
- 类的静态结构指针声明在C++
- 在两个.cpp文件之间定义全局类/结构指针
- 返回对常量结构(指针类型)成员的引用:明显的左值到右值转换
- 是否可以将无符号字符数组reinterpret_cast到仅包含C++中无符号字符成员的结构指针
- 如何在地图中使用结构指针
- 结构指针在"新"调用上给出已分配的地址?
- 铸造结构指针指向C 的工会指针
- c++ 结构指针在初始化为 NULL 时无法读取内存
- 函数中的结构指针的地址为0x1
- C 用结构指针初始化结构
- C 结构指针铸成数组Interop C#
- 方法中不允许访问结构指针
- 如何打印出结构指针
- MPI_Op_create:候选功能不可行.自定义结构指针不能解释为空指针
- 尝试将结构指针传递给类时出错
- 初始化结构指针的值
- 使用 pybind11 将自定义结构(指针向量)从 C++ 传递到 python 的方法是什么?