2013-11-24 170 views
0

对不起,但我有一堆代码,我写了大约9个文件,我不能摆脱之间我认为是头文件和cpp文件之间的一个错误....标题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 ========== 

回答

1

你忘了构造的身体括号:

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

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

+0

谢谢你是我看不见的主要东西 – hartk1213

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); 

仅仅是完全错误的,我不知道你正试图在这里做。

+0

是的,一些关于作者的C++基础知识的好书根本不会受到伤害。 ':)' –

+0

即时制作一个俄罗斯方块游戏,这是绘制形状...每个形状是由4个不同形状(L,J,S,Z,O,I,T)绘制的方形帮我画他们....有没有更好的方法来做到这一点?如果有即时消息的所有耳朵帮忙,我还是一个noob im抱歉 – hartk1213

+0

@ user1822409我曾经陷入这些困惑,但是因为你要求一个更好的方法来实现一个俄罗斯方块游戏,试试[this](http:// javilop。 COM/gamedev /俄罗斯方块教程在-C-平台无关聚焦-在游戏逻辑换初学者/)。它是一个非常好的教程。 – mr5