标题为cpp文件混乱

时间:2013-11-24 07:36:02

标签: c++

对不起文字的墙,但我有一堆代码,我在大约9个文件中编写,我无法摆脱我认为是头文件和cpp文件之间的一个错误.... / p>

这是头文件

#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 ==========

2 个答案:

答案 0 :(得分:1)

你忘记了构造函数体的大括号:

Shape::Shape(Name name): name_(name),
    angle_(0),
    x_(3),
    y_(0)
{} // <---

你也拼错了它:Shape::Name。它应该是Shape::Shape

答案 1 :(得分:1)

我看到了几个问题。首先,请勿使用#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);

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