0

我在用 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)
{

}
4

2 回答 2

3

去掉 main 函数中的“void”,并为函数定义使用类型

于 2012-12-05T02:09:30.893 回答
1
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. 通过引用 ReadFile 传递参数。您想将文件读入 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)
{
}
于 2012-12-04T02:21:59.300 回答