2012-12-04 83 views
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) 
{ 

} 
+2

'無效的ReadFile(FLIST,mList)'你忘類型。 – chris

+2

@chris:或者寧願忘記忽略返回類型。 – GManNickG

+0

@GManNickG,哎呀,我什至沒有發現。我從函數頭複製了它。 – chris

回答

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);  
} 

下面的代碼應b工作。請注意,我增強了一下,不確定這是你想要的嗎? 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) 
{ 
} 
+0

好的非常感謝,我總是想再次出於某種原因宣佈它們。它仍然沒有編譯..它仍然歸咎於我的第二個結構出於某種原因。 – user1828561

+0

錯誤信息是什麼? – billz

+0

1> c:\ users \ collin \ documents \ visual studio 2010 \ projects \ datingservice \ datingservice \ program6.cpp(62):錯誤C2236:意外的'struct''Fem'。你忘了';'了嗎? 1> c:\ users \ collin \ documents \ visual studio 2010 \ projects \ datingservice \ datingservice \ program6.cpp(62):錯誤C2143:語法錯誤:缺少';'之前'{' 1 0> c:\ users \ collin \ documents \ visual studio 2010 \ projects \ ?) – user1828561

3

擺脫主要功能 「空缺」 S,並且使用類型的函數定義