2012-10-03 204 views
2

我的編譯器的抱怨和這個代碼拋出了一個錯誤(見標題):(C++)錯誤:在';'之前預期的primary-expression令牌

int toLevel(int xp) 
{ 
    int points = 0; 
    int level = 1; 
    for(; level < MAX_LEVEL; level++) 
    { 
     points += floor(level + 300*pow(2, level/7.)); 
     if(xp < points) 
     { 
      break; 
     } 
    } 
    return level; 
} 

的錯誤是在for(; level < MAX_LEVEL; level++)線,以及完整的錯誤日誌樣子(這是第50行,僅供參考) :

In function 'int toLevel(int)': 
50 error: expected primary-expression before ';' token 
50 error: expected ')' before ';' token 
50 error: expected ';' before ')' token 
48 warning: unused variable 'points' 
59 error: expected '}' at end of input 
59 warning: no return statement in function returning non-void 
=== Build finished: 4 errors, 2 warnings === 

任何想法?我假設我沒有關閉支架,但我找不到它。 正如我懷疑這可能是這種情況,這裏是該文件的全碼:

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <vector> 
#include <math.h> 

using namespace std; 

#define MAX_LEVEL 99; 

#include "main.h" 

int main() 
{ 
    Player player; 
    loadSkills(player); 
    if(!loadGame(player)) 
    { 
     cout << "It looks like its your first time playing." << endl; 
     cout << "What's your name?: "; 
     cin >> player.name; 
    } 
    cout << "Hello " << player.name << ", you're level " << toLevel(player.xp) << endl; 
    //TODO 
    return 0; 
} 

//Returns true if loaded, false otherwise 
bool loadGame(Player player) 
{ 
    //TODO 
    return false; 
} 

void loadSkills(Player player) 
{ 
    vector<Skill> skills; 
    skills.push_back((Skill){"Melee"}); 
    skills.push_back((Skill){"Woodcutting"}); 
    skills.push_back((Skill){"Firemaking"}); 
    skills.push_back((Skill){"Fishing"}); 
    skills.push_back((Skill){"Cooking"}); 
    player.skills = skills; 
} 

int toLevel(int xp) 
{ 
    int points = 0; 
    int level = 1; 
    for(; level < MAX_LEVEL; level++) 
    { 
     points += floor(level + 300*pow(2, level/7.)); 
     if(xp < points) 
     { 
      break; 
     } 
    } 
    return level; 
} 

和main.h它引用:

#ifndef MAIN_H_INCLUDED 
#define MAIN_H_INCLUDED 

vector<string> &split(const string &s, char delim, vector<string> &elems) { 
    stringstream ss(s); 
    string item; 
    while(getline(ss, item, delim)) { 
     elems.push_back(item); 
    } 
    return elems; 
} 

//Ease of use method 
vector<string> split(const string &s, char delim) { 
    vector<string> elems; 
    return split(s, delim, elems); 
} 

struct Skill 
{ 
    string name; //The name of the skill 
}; 

struct Player 
{ 
    int health;    //The player's current health 
    int xp;     //The player's xp 
    int maxhealth;   //The player's max health 
    vector<Skill> skills; //The skills available to the player 
    string name;   //The player's name 
}; 

class InteractOption 
{ 
    public: 
    void doAction();//What should happen 
    bool succeeded; //If true, they get the xp for it. 
    int xp;   //Amount of xp gained for doing 
    Skill skill; //Skill to gain xp in 
    string name; //"Chop"/"Attack"/etc 
}; 

class WorldObject 
{ 
    public: 
    InteractOption interactOption; //The option for interacting with the object. 
            //Can be null (i.e. can't interact) 
    string name;     //"Tree"/"Goblin"/etc 
}; 

bool loadGame(Player); 
void loadSkills(Player); 
int toLevel(int); 

#endif // MAIN_H_INCLUDED 

回答

7

MAX_LEVEL取出後分號宏:

#define MAX_LEVEL 99; 

作爲當宏在for環被替換變得:

for(; level < 99;; level++) 

這是非法的語法。

+0

當然!那麼,我現在感到很傻。 –

+0

我忘了說,謝謝! :d –

相關問題