2017-04-23 30 views
0
enum state{ 
state_Mmenu, // State 0 
state_settingmenu,  // State 1 
state_gamemenu,  // State 2 
state_savegamemenu,  // State 3 
state_loadgamemenu,  // State 4; 
state_Exit,   // State 5; 
state_other 
}; 

state stateMmenu(); 
state statesettingmenu(string, string); 
state stategamemenu(); 
state statesavegamemenu(string, string, string, ofstream&); 
state stateloadgamemenu(string, string, string, ifstream&); 
state stateExit(); 



int main() 
{ 
state curr=state_other; 

string filename= "PlayerOne.jak"; 
string fname="Player"; 
string lname="One"; 
ifstream inFS; 
ofstream outFS; 



while(curr != state_Exit) 
{ 
    switch(curr) 
    { 
     case state_Mmenu: 
     curr = stateMmenu(); 
     break; 

     case state_settingmenu: 
     curr = statesettingmenu(fname, lname); 
     break; 

     case state_gamemenu: 
     curr = stategamemenu(); 
     break; 

     case state_savegamemenu: 
     curr = statesavegamemenu(fname, lname, filename, outFS); 
     break; 

     case state_loadgamemenu: 
     curr = stateloadgamemenu(fname, lname, filename, inFS); 
     break; 

     default: 
      cout << "Don't Tip the Vending Machine"<<endl; 
      curr = state_Mmenu; 
     break; 
    } 
} 


return 0; 

} 


state statesavegamemenu(string fname, string lname, string filename, 
ofstream outFS) 
{ 
state next=state_savegamemenu; 
int choice4=0; 
cout<<" -Save Game Menu-----------------"<<endl; 
cout<<"1. File Name - "<<filename<<endl; 
cout<<"2. Save" <<endl; 
cout<<"3. Game Menu" <<endl; 
cout<<"----------------------------"<<endl; 
    do 
{ 

    cout << "Choice? "<< endl; 
    cin >> choice4; 
    cin.clear(); 
    cin.ignore(10000,'\n'); 

}while(!cin.good()); 

switch(choice4) 
{ 
    case 1: 
    { 
     cout<<"What would you like to save this file as? "; 
     cin>>filename; 
     cout<<endl; 
     next = state_savegamemenu; 
    } 
    break; 
    case 2: 
    { 
     outFS.open(filename.c_str()); 
     if(!outFS.is_open()) 
     { 
      next = state_savegamemenu; 
      break; 
     } 
     else 
     { 
      outFS<<fname<<endl; 
      outFS<<lname<<endl; 
     } 
     outFS.close(); 
     next = state_savegamemenu; 
    } 

    break; 
    case 3: 
     next = state_gamemenu; 
    break; 
    default: 
    cout << "Bad Input!"<<endl; 
     next = state_savegamemenu; 
    break; 
} 
cout <<endl; 
return next; 
} 

state stateloadgamemenu(string fname, string lname, string filename, 
ifstream inFS) 
{ 
state next=state_loadgamemenu; 
int choice5=0; 
cout<<" -Save Game Menu-----------------"<<endl; 
cout<<"1. File Name - "<<filename<<endl; 
cout<<"2. Load" <<endl; 
cout<<"3. Main Menu" <<endl; 
cout<<"----------------------------"<<endl; 
string filecontent; 
    do 
{ 

    cout << "Choice? "<< endl; 
    cin >> choice5; 
    cin.clear(); 
    cin.ignore(10000,'\n'); 

}while(!cin.good()); 

switch(choice5) 
{ 
    case 1: 
    { 
     cout<<"What file would you like to load? "; 
     cin>>filename; 
     cout<<endl; 
     next = state_loadgamemenu; 
    } 
    break; 
    case 2: 
    { 
     inFS.open(filename.c_str()); 
     if(!inFS.is_open()) 
     { 
      next = state_savegamemenu; 
      break; 
     } 
     else 
     { 
      while(!inFS.eof()) 
      { 
       inFS >> filecontent; 
       if(inFS.good()) 
       { 
        cout << filecontent << endl; 
       } 
      } 
     } 
     inFS.close(); 
     next = state_loadgamemenu; 
    } 

    break; 
    case 3: 
     next = state_Mmenu; 
    break; 
    default: 
    cout << "Bad Input!"<<endl; 
     next = state_loadgamemenu; 
    break; 
} 
cout <<endl; 
return next; 
} 

我得到的錯誤是stateavegamemenu()和stateloadgamemenu()的未定義的引用。據我所知,這樣的鏈接錯誤與程序無法找到有問題的函數的定義有關,但我非常清楚地實現了它們,所以我很困惑,爲什麼會發生這種情況,以及任何幫助將不勝感激。即使我有所有定義的函數獲取未定義的引用到錯誤在C++中

在保存遊戲,加載遊戲
+0

可能想修剪不會導致錯誤的代碼 – Isaac

+0

爲什麼你不能包含可能會編譯的頭文件? –

回答

0

你忘了做,所以它不是調用匹配

state statesavegamemenu(string fname, string lname, string filename, 
ofstream outFS) 
{ 

state statesavegamemenu(string fname, string lname, string filename, 
ofstream& outFS) 
{ 

同樣與其他功能,但ifstream的而不是ifstream的&

+0

這是問題,非常感謝!我很愚蠢 – guccimane69

+0

大聲笑我犯了同樣的錯誤幾十次身份證欣賞它,如果你檢查了我的答案雖然:) –

+0

即使我能做到theres最小時間限制!一個簡單的問題,你可以看看我的stateavegame,並在哪裏我使用cin >>文件名,這應該從用戶採取一個文件名,並取代我以前的一個,但由於某種原因,它不會覆蓋它,你看到任何明顯的問題? – guccimane69