头到cpp文件混淆
Header to cpp file confusion
很抱歉出现了文本墙,但我在大约9个文件中写了一堆代码,我无法消除我认为是头文件和cpp文件之间的一个错误。。。。
这是头文件
#pragma once
class Draw;
class Shape {
public:
enum Direction {LEFT = -1, RIGHT = 1};
enum Name{I,J,L,O,S,Z,T};
Shape(Name);
void draw(Draw &) const;
void move(int dx, int dy);
void rotate(Direction);
bool map(int x, int y) const;
int x() const {
return x_;
}
int y() const {
return y_;
}
private:
Name name_;
int angle_;
int x_;
int y_;
};
这是与头文件一起使用的cpp文件
#include "shape.h"
#include "draw.h"
Shape::Name(Name name): name_(name),
angle_(0),
x_(3),
y_(0)
void Shape::draw(draw &p) const {
p.setColor(static_cast<draw::Color(name_));
for(int y = 0; y < 4; y++)
for(int x = 0; x < 4; x++)
if( map(x,y))
p.rect(x + x_) * 8 + 1,
p.rect(y + y_) * 8 + 1,
p.rect(x + x_ + 1) * 8 - 1,
p.rect(y + y_ + 1) * 8 - 1);
}
bool Shape::map(int x, int y) const {
static const char *SHAPES[] =
{
" 8 " // I
" 8 "
" 8 "
" 8 ",
" 8 " // J
" 8 "
" 88 "
" ",
" 8 " // L
" 8 "
" 88 "
" ",
" " // O
" 88 "
" 88 "
" ",
" 8 " // S
" 88 "
" 8 "
" ",
" 8 " // Z
" 88 "
" 8 "
" ",
" " // T
" 888"
" 8 "
" "
};
static const struct {
int x;
int y;
}
ROTATE[][16] = {
{
{ 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
{ 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
{ 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 },
{ 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }
},
{
{ 3, 0 }, { 2, 0 }, { 1, 0 }, { 0, 0 },
{ 3, 1 }, { 2, 1 }, { 1, 1 }, { 0, 1 },
{ 3, 2 }, { 2, 2 }, { 1, 2 }, { 0, 2 },
{ 3, 3 }, { 2, 3 }, { 1, 3 }, { 0, 3 }
},
{
{ 3, 3 }, { 3, 2 }, { 3, 1 }, { 3, 0 },
{ 2, 3 }, { 2, 2 }, { 2, 1 }, { 2, 0 },
{ 1, 3 }, { 1, 2 }, { 1, 1 }, { 1, 0 },
{ 0, 3 }, { 0, 2 }, { 0, 1 }, { 0, 0 }
},
{
{ 0, 3 }, { 1, 3 }, { 2, 3 }, { 3, 3 },
{ 0, 2 }, { 1, 2 }, { 2, 2 }, { 3, 2 },
{ 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 },
{ 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 }
}
};
return SHAPES[name_]
[ROTATE[angle_][y * 4 + x].y * 4 + ROTATE[angle_][y * 4 + x].x != ' ';
}
void Shape::move(int dx, int dy) {
x_ += dx;
y_ += dy;
}
void Shape::rotate(Direction d) {
angle_ = (angle_ + d + 4) % 4;
}
这是我得到的错误:
1>------ Build started: Project: Tetris, Configuration: Debug Win32 ------
1>Build started 11/23/2013 11:21:58 PM.
1>InitializeBuildStatus:
1> Touching "DebugTetris.unsuccessfulbuild".
1>ClCompile:
1> All outputs are up-to-date.
1> shape.cpp
1>\hart-serverusersadmindocumentsschooltetristetrisshape.cpp(4): error C2146: syntax error : missing ')' before identifier 'name'
1>\hart-serverusersadmindocumentsschooltetristetrisshape.cpp(4): error C2146: syntax error : missing ';' before identifier 'name'
1>\hart-serverusersadmindocumentsschooltetristetrisshape.cpp(4): error C2059: syntax error : ')'
1>\hart-serverusersadmindocumentsschooltetristetrisshape.cpp(4): error C2470: 'name' : looks like a function definition, but there is no parameter list; skipping apparent body
1>\hart-serverusersadmindocumentsschooltetristetrisshape.cpp(4): error C2065: 'name' : undeclared identifier
1>\hart-serverusersadmindocumentsschooltetristetrisshape.cpp(9): error C2612: trailing 'type' illegal in base/member initializer list
1>\hart-serverusersadmindocumentsschooltetristetrisshape.cpp(110): fatal error C1004: unexpected end-of-file found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.17
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
您忘记了构造函数主体的大括号:
Shape::Shape(Name name): name_(name),
angle_(0),
x_(3),
y_(0)
{} // <---
您还拼错了它:Shape::Name
。它应该是Shape::Shape
。
我看到您有几个问题。首先,不要使用#pragma once
,使用
#ifndef HEADER_FILE
#define HEADER_FILE
class definition
#endif
从技术上讲,这不是一个错误,但这是一个最佳实践。
其次,正如@Haroogan所指出的,您缺少构造函数定义。
第三,
void Shape::draw(draw &p) const {
应该是
void Shape::draw(Draw &p) const {
第四,
p.setColor(static_cast<draw::Color(name_));
应该是
p.setColor(static_cast<Draw::Color>(name_));
这个
if( map(x,y))
p.rect(x + x_) * 8 + 1,
p.rect(y + y_) * 8 + 1,
p.rect(x + x_ + 1) * 8 - 1,
p.rect(y + y_ + 1) * 8 - 1);
完全错了,我不知道你在这里想做什么。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 为什么我的主文件.cpp不打印头文件中的任何内容
- 生成文件错误 - 找不到文件 - *.cpp
- 如何在文件.cpp gtkmm中声明小部件
- 没有实现文件(.cpp)的派生类
- 如何使用"CMakeLists.txt"中的add_library将整个文件(.cpp,.h等)包含在目录中
- 如何在Linux / Windows操作系统上使用文件*.cpp一步编译下面的代码
- 关于 Linux .so 文件无法链接到主文件.cpp文件
- 文件.cpp从Windows到Linux
- Ubuntu C++ 编译器错误: g++: 错误: 文件.cpp: 没有这样的文件或目录
- 如何将文件.cpp编译为本机编译
- 使用模板类时,似乎无法包含除 main 以外的任何 cpp 文件.cpp
- 无法编译C++文件.cpp。C++98模式
- 为什么C++头文件不需要包含实现文件 (.cpp)
- 如何访问位于独立文件(.cpp)中的非成员函数
- 如何在Visual Studio Code中通过键盘快捷键切换头文件/cpp文件
- 如何将数据从stringstream写入文件(CPP)
- 为什么filestream不把空白写入文件cpp ?