c++ - 'class' 类型重定义,已经包含 ifndef/define/endif

标签 c++

C++ 新手,一头雾水。我查了几十个类似的问题,每个解决方案都是添加 #pragma once#ifndef/#define/#endif,但它似乎不是我的情况。我有一个井字棋盘源文件 tttBoard.cpp,如下所示:

#include "stdafx.h"
#include "tttBoard.h"
#include <string>

class tttBoard {

    enum sVal {sEmp,sX,sO};
    sVal gBoard[3][3];
    bool isX;

    tttBoard::tttBoard() {
    }

    void Draw() {
    }

    void Move(int x, int y) {
    }

    char* getValue(int x, int y) {}

};

和各自的头文件,tttBoard.h:

#ifndef tttBoard_h
#define tttBoard_h

class tttBoard {
    public:
        tttBoard();
        void            Draw();
        void            Move(int x, int y);
        char*           getValue(int x, int y);
    private:
        enum sVal {
        sEmp,
        sX,
        sO
        };

        sVal            gBoard[3][3];
        bool            isX;
};

#endif

但是在编译时我遇到了以下错误:

tttboard.cpp(5): error C2011: 'tttBoard' : 'class' type redefinition
tttboard.h(4) : see declaration of 'tttBoard'

有什么想法吗?

最佳答案

要实现该类,您无需重新定义它。在您的 .cpp 文件中尝试如下操作。

#include "tttBoard.h"

tttBoard::tttBoard() {
}

void tttBoard::Draw() {
}

void tttBoard::Move(int x, int y) {
}

char* tttBoard::getValue(int x, int y) {}

您也不需要重新声明您的 enum sVal 或任何 tttBoard 成员变量。它们应该可以从头文件中的原始声明中获得。 :-)

另外,确保 getValue 被实现以实际返回一些东西。在完成这些功能之前,您将无法编译。

阅读有关 C++ 类的更多信息 here .

关于c++ - 'class' 类型重定义,已经包含 ifndef/define/endif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12380915/

相关文章:

c++ - fstream C++ 的相对路径

c++ - 如何确定 2 个路径是否在可移植 C++ 中引用同一个文件

C++对象映射本地拷贝

c++ - C++中的重复循环

c++ - 使用C++-17,如何从字符串 vector 填充元组

c++ - bool 值的左移和右移

c++ - 如何创建可以同时处理多个客户端的 Boost.Asio 服务器?

c++ - 用于 HTML 图像标签的 QRegExp

python - 带有嵌入式 python 模块的 C++ 多线程

c++ - 当 C++ 全局变量未显式初始化时发出警告?