【问题标题】:C++ I get error messages when adding comments in the header fileC++ 在头文件中添加注释时收到错误消息
【发布时间】:2014-07-14 01:08:11
【问题描述】:

我正在使用 Microsoft Visual Studio 2010 制作 C++ 程序。

当我在头文件中添加 cmets 时,我会收到关于未包含在注释中的部分代码的错误消息。如果我删除public 下的前两行cmets,GameBoard(int p, int t) 下的错误就会消失。如果我删除了public: 下的所有四个 cmets,那么private: 下的错误就会消失。

以下是我收到的错误消息:

  • GameBoard(int p, int t) 下我在int 下得到一个错误,上面写着Error: Expected an identifier. 我也在逗号下得到一个错误,上面写着Error: Expected a ')' 我得到的最后一个错误是在写着@ 的右括号下987654330@。但是,只要我删除下面显示的代码的前两行注释,这三个错误就会消失。

  • 我得到的另一个错误是在private: 下,也就是说,Error: expected a declaration. 一旦我删除了public: 下的所有四个注释行,这个错误就会消失

有谁知道为什么会这样,注释行不应该对代码产生任何影响。我试过使用另一种评论/* */,但同样的事情发生了。

using namespace std;
#include <string>
#include "Tile.h"
#include <iostream>

class GameBoard{
public:
    // p = The number of players
    // t = The number of tiles each player will play with.
    GameBoard(int p, int t);
    void init();
    // Scan the board and see if any tiles would be exiled.
    void checkExile();
    // Clear the board of any tiles.
    void clear();
private:
    // The Game Board.
    TileO board[12][12];
    // Detained tiles. The height is determined by the number of players. Each player can detain up to three tiles.
    TileO * detained[3];
    // These are the tiles that are not in the game.
    // All tiles start on the bench, where the player places them on the Home Base.
    TileO bench[4][2][4];
    int players; // Number of players
    int tilesPlaying; // Number of tiles 
};

【问题讨论】:

  • 如果Tile.h 是这样的标题,我首先要确保类声明all}; 结尾,而不仅仅是}。你的using namespace std; 的放置和使用是可怕的,尤其是如果这是一个头文件。 Read this.
  • 我原来的代码里有,只是忘了在这里加上。感谢您指出这一点,添加它。
  • 某处缺少分号或大括号...

标签: c++ comments


【解决方案1】:

同样的事情曾经发生在我身上,我试图将代码从 devC++ 复制/粘贴到 VS2008。也许粘贴一些东西会导致问题。

【讨论】:

    【解决方案2】:

    也许您的源代码中的某个地方有一个虚假字符,例如它中间的 UTF-8 BOM 或其他什么。

    尝试将您的代码从 Stack Overflow 中复制回来,然后将其粘贴到您的原始代码之上 - 这有什么不同吗? (或者甚至只是手动重新输入它 - 它只有几行。)

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 2020-07-27
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      相关资源
      最近更新 更多