头到cpp文件混淆

Header to cpp file confusion

本文关键字:文件 cpp 头到      更新时间:2023-10-16

很抱歉出现了文本墙,但我在大约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);

完全错了,我不知道你在这里想做什么。