gpt4 book ai didi

c++ - cpp文件头混淆

转载 作者:行者123 更新时间:2023-11-28 03:03:38 24 4
gpt4 key购买 nike

对不起,我有一堆代码,写在大约 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 "Debug\Tetris.unsuccessfulbuild".
1>ClCompile:
1> All outputs are up-to-date.
1> shape.cpp
1>\\hart-server\users\admin\documents\school\tetris\tetris\shape.cpp(4): error C2146: syntax error : missing ')' before identifier 'name'
1>\\hart-server\users\admin\documents\school\tetris\tetris\shape.cpp(4): error C2146: syntax error : missing ';' before identifier 'name'
1>\\hart-server\users\admin\documents\school\tetris\tetris\shape.cpp(4): error C2059: syntax error : ')'
1>\\hart-server\users\admin\documents\school\tetris\tetris\shape.cpp(4): error C2470: 'name' : looks like a function definition, but there is no parameter list; skipping apparent body
1>\\hart-server\users\admin\documents\school\tetris\tetris\shape.cpp(4): error C2065: 'name' : undeclared identifier
1>\\hart-server\users\admin\documents\school\tetris\tetris\shape.cpp(9): error C2612: trailing 'type' illegal in base/member initializer list
1>\\hart-server\users\admin\documents\school\tetris\tetris\shape.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

关于c++ - cpp文件头混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20172261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com