C++简单的类程序无法编译。我搞砸了包含标题吗? 'Error redefinition of a class'
C++ Simple class program wont compile. Did i mess up the include headers? 'Error redefinition of a class'
我只有一个简单的程序,其中dice.cpp和dice.h通过game.cpp运行,到目前为止,只计算两次掷骰子的总和。
当我试图运行程序时,显然我正在重新定义Dice类,这就是我的错误告诉我的
这是我的三份文件。
game.cpp
#include "Dice.h"
#include <iostream>
using namespace std;
int main()
{ int sum;
Dice dice1;
Dice dice2;
dice1.roll();
dice2.roll();
sum = dice1.getFace() + dice2.getFace();
cout << sum;
return 0;
}
骰子.cpp
#ifndef DICE_H
#define DICE_H
#include "Dice.h"
using namespace std;
// g++ -c Dice.cpp
// default constructor: initializes the face of a new
// Dice object to 1
Dice::Dice()
{
//cout << "Default constructor " << endl;
face = 1; // not redeclaring the data member face
}
// specific constructor: initializes the face of a new
// Dice object to newFace
// Pre-condition: newFace is a valid number
// call setFace function inside Dice(int newFace)
Dice::Dice(int newFace)
{
//cout << "Specific constructor " << endl;
setFace(newFace);
}
// Sets face to the value in otherFace
// Pre-condition: otherFace is valid
void Dice::setFace(int otherFace)
{
assert(otherFace >= 1 && otherFace <= 6);
face = otherFace;
}
// Changes the value of face to a random value between 1 and 6
void Dice::roll()
{
face = rand()%6 +1;
}
// returns the face value of a Dice object
int Dice::getFace() const
{
return face;
}
// displays the face value of a Dice object
void Dice::display() const
{
cout << "This dice has " << face << " on top" << endl;
}
#endif
骰子
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <ctime>
// definition of class Dice
class Dice
{
private:
int face; // can only take values between 1 and 6
public:
// default constructor: initializes the face of a new
// Dice object to 1
Dice();
// specific constructor: initializes the face of a new
// Dice object to newFace
// Pre-condition: newFace is a valid number
// call setFace function inside Dice(int newFace)
Dice(int newFace);
// Sets face to the value in otherFace
// Pre-condition: otherFace is valid
void setFace(int otherFace);
// Changes the value of face to a random value between 1 and 6
void roll();
// returns the face value of a Dice object
int getFace() const;
// displays the face value of a Dice object
void display() const;
};
这是错误的照片
在"dice.cpp"文件中,删除第三行"#include"dice.h。您已经定义了dice类,因此不需要#include语句
您显示的代码是骗人的。错误似乎是说game.cpp
包括Dice.h
,而Dice.h
包括Dice.cpp
,后者包括Dice.h
。因此,您的Dice.h
头文件被包含两次,如果头文件中没有头包含保护,则类将被定义两次。
简单的解决方案?不要包含源文件。不过,在头文件中仍然应该有头包含保护。
相关文章:
- "error: no matching function for call to"构造函数错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- C++错误:"error: int aaa::bbb is protected within this context"
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 对于C++,方括号中的标题必须总是在引号中的标题之上吗
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- 使用 cmake 的 LLVM 构建在 tsan_libdispatch_mac.cc 期间失败; "Error: conflicting types for ..."
- C++ "error: invalid use of void expression"
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 为什么我不应该把所有东西都放在标题中?
- 从不同目录执行时"symbol lookup error:",否则从构建目录执行时按预期工作
- 对象声明中的模板"error: expected expression"
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- "Error: 'dungeon_t' was not declared in this scope"但包含在标题中
- C++简单的类程序无法编译。我搞砸了包含标题吗? 'Error redefinition of a class'