2

我的编译器抱怨并抛出关于此代码的错误(见标题):

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
4

1 回答 1

7

MAX_LEVEL从宏中删除尾随分号:

#define MAX_LEVEL 99;

当宏在for循环中被替换时,它变成:

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

这是非法的语法。

于 2012-10-03T08:06:46.053 回答