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 に答える