包含数组的结构数组。不会编译
Arrays of Structs with arrays in it. Won't compile
我在C++中制作结构数组时遇到问题。 出于某种原因, 这不会编译。 错误指向 Fem 结构,但将其归咎于缺少 ';"' 和缺少函数的标头。这个想法是获取一个结构数组,每个结构都有一个数组和一个字符串。 对此的任何帮助将不胜感激。
/* Global headers and namespace */
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
/* ================================================================ */
/* Global named constants */
const int NUM_ANSWERS = 10;
const int NUM_STRUCTS = 15;
/* ================================================================ */
/* Global type definitions */
// Two struct types, Male and Female
struct Male
{
int maleAnswers [NUM_ANSWERS];
string name;
};
struct Fem
{
int femAnswers [NUM_ANSWERS];
string name;
};
// Arrays of structs
typedef Fem FemList [NUM_STRUCTS];
typedef Male MaleList [NUM_STRUCTS];
/* ================================================================ */
/* global function prototypes */
void OutputHeader ();
void ReadFile (FemList, MaleList);
/* ================================================================ */
int main ()
{
FemList fList; // Array of Fem structs
MaleList mList; // Array of Male structs
void OutputHeader ();
void ReadFile (fList, mList);
}
void OutputHeader ()
{
cout << "==================================================================" << endl;
cout << "Welcome to the Cheap & Ineffective Dating Service of Tallahassee!" << endl << endl;
cout << " eDisHarmony.com" << endl;
cout << "==================================================================" << endl << endl;
cout << "<><><> Initial Client Data as Input <><><>" << endl << endl;
cout << setw(11) << "Sex" << setw(31) << "Answers" << "Name" << endl;
cout << setw(11) << "---" << setw(24) << "-------------------" << "--------------------" << endl;
}
void ReadFile (fList, mList)
{
}
去掉主函数中的"void",使用类型进行函数定义
void ReadFile (fList, mList);
应该是
void ReadFile (FemList fList, MaleList mList);
和
int main ()
{
FemList fList; // Array of Fem structs
MaleList mList; // Array of Male structs
void OutputHeader ();
void ReadFile (fList, mList);
}
应该是
int main ()
{
FemList fList; // Array of Fem structs
MaleList mList; // Array of Male structs
OutputHeader ();
ReadFile (fList, mList);
}
下面的代码应该可以工作。注意,我稍微增强了一下,不确定这是你想要的吗?1. 为 std 中的标识符提供完整的命名空间2. 通过引用将参数传递给读取文件。你想把文件读到FemList和MaleList中吗,这是你的意图吗?
/* Global named constants */
const int NUM_ANSWERS = 10;
const int NUM_STRUCTS = 15;
/* Global type definitions */
// Two struct types, Male and Female
struct Male
{
int maleAnswers [NUM_ANSWERS];
std::string name;
};
struct Fem
{
int femAnswers [NUM_ANSWERS];
std::string name;
};
// Arrays of structs
typedef Fem FemList [NUM_STRUCTS];
typedef Male MaleList [NUM_STRUCTS];
/* global function prototypes */
void OutputHeader ();
void ReadFile(FemList&, MaleList&);
int main ()
{
FemList fList; // Array of Fem structs
MaleList mList; // Array of Male structs
OutputHeader();
ReadFile(fList, mList);
}
void OutputHeader ()
{
std::cout << "==================================================================" << std::endl;
std::cout << "Welcome to the Cheap & Ineffective Dating Service of Tallahassee!" << std::endl;
std::cout << " eDisHarmony.com" << std::endl;
std::cout << "==================================================================" << std::endl;
std::cout << "<><><> Initial Client Data as Input <><><>" << std::endl;
std::cout << std::setw(11) << "Sex" << std::setw(31) << "Answers" << "Name" << std::endl;
std::cout << std::setw(11) << "---" << std::setw(24) << "-------------------" << "--------------------" << std::endl;
}
void ReadFile (FemList &fList, MaleList &mList)
{
}