对象没有命名类型错误
Posted
技术标签:
【中文标题】对象没有命名类型错误【英文标题】:Object doesn't name a type error 【发布时间】:2012-07-31 10:03:08 【问题描述】:我收到了这个错误
当试图为类播放器制作一个对象时.....
main.cpp|24|error: 'pic' 没有命名类型
这里是代码
这是我的播放器.h
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
#include <allegro.h>
#include <iostream>
class player
public:
int upperraw;
int middlerraw;
int lowerraw;
int firstcolumn;
int seccolumn;
int thirdcolumn;
;
#endif // PLAYER_H_INCLUDED
我的 main.cpp
#include "bitmap.h"
#include "player.h"
#include <allegro.h>
#include <iostream>
int onbitmap=1;
bool turn=true;
BITMAP *my_pic1;
BITMAP *my_pic2;
BITMAP *my_pic3;
BITMAP *my_pic4;
BITMAP *my_pic5;
BITMAP *my_pic6;
BITMAP *my_pic7;
BITMAP *my_pic8;
BITMAP *my_pic9;
player *pic;
pic->upperrow=0;
player *one;
player *two;
static void nextturn()
if(turn==1)
turn=0;
else
turn=1;
static void drawsquares()
my_pic1 = NULL;
my_pic1 = load_bitmap("selected.bmp", NULL);
my_pic2 = NULL;
my_pic2 = load_bitmap("unselected.bmp", NULL);
my_pic3 = NULL;
my_pic3 = load_bitmap("unselected.bmp", NULL);
my_pic4 = NULL;
my_pic4 = load_bitmap("unselected.bmp", NULL);
my_pic5 = NULL;
my_pic5 = load_bitmap("unselected.bmp", NULL);
my_pic6 = NULL;
my_pic6 = load_bitmap("unselected.bmp", NULL);
my_pic7 = NULL;
my_pic7 = load_bitmap("unselected.bmp", NULL);
my_pic8 = NULL;
my_pic8 = load_bitmap("unselected.bmp", NULL);
my_pic9 = NULL;
my_pic9 = load_bitmap("unselected.bmp", NULL);
draw_sprite(screen, my_pic1, 218,146+65*1);
draw_sprite(screen, my_pic2, 218,146+65*2);
draw_sprite(screen, my_pic3, 218,146+65*3);
draw_sprite(screen, my_pic4, 218+65,146+65*1);
draw_sprite(screen, my_pic5, 218+65,146+65*2);
draw_sprite(screen, my_pic6, 218+65,146+65*3);
draw_sprite(screen, my_pic4, 218+65,146+65*1);
draw_sprite(screen, my_pic5, 218+65,146+65*2);
draw_sprite(screen, my_pic6, 218+65,146+65*3);
draw_sprite(screen, my_pic7, 218+(65*2),146+65*1);
draw_sprite(screen, my_pic8, 218+(65*2),146+65*2);
draw_sprite(screen, my_pic9, 218+(65*2),146+65*3);
static void replacebitmap(BITMAP *one,BITMAP *two)
static void readakey()
int val=readkey();
switch (val >> 8)
case KEY_LEFT:
switch (onbitmap)
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
onbitmap=1;
my_pic4 = load_bitmap("unselected.bmp", NULL);
draw_sprite(screen, my_pic4, 218,146+65*1);
my_pic1 = load_bitmap("selected.bmp", NULL);
draw_sprite(screen, my_pic1, 218,146+65*1);
clear_keybuf();
readakey();
case 5:
onbitmap=5;
my_pic5 = load_bitmap("unselected.bmp", NULL);
draw_sprite(screen, my_pic1, 218,146+65*1);
my_pic2 = load_bitmap("selected.bmp", NULL);
draw_sprite(screen, my_pic1, 218,146+65*1);
clear_keybuf();
readakey();
case 6:
onbitmap=6;
my_pic5 = load_bitmap("unselected.bmp", NULL);
draw_sprite(screen, my_pic1, 218,146+65*1);
my_pic2 = load_bitmap("selected.bmp", NULL);
draw_sprite(screen, my_pic1, 218,146+65*1);
clear_keybuf();
readakey();
case 7:
onbitmap=7;
my_pic7 = load_bitmap("unselected.bmp", NULL);
draw_sprite(screen, my_pic7, 218,146+65*1);
my_pic4 = load_bitmap("selected.bmp", NULL);
draw_sprite(screen, my_pic4, 218,146+65*1);
clear_keybuf();
readakey();
case 8:
onbitmap=8;
my_pic8 = load_bitmap("unselected.bmp", NULL);
draw_sprite(screen, my_pic8, 218,146+65*1);
my_pic5 = load_bitmap("selected.bmp", NULL);
draw_sprite(screen, my_pic5, 218,146+65*1);
clear_keybuf();
readakey();
int main(int argc, char *argv[])
allegro_init();
install_keyboard();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT, 640,480,0,0);
BITMAP *my_pic = NULL;
my_pic = load_bitmap("background.bmp", NULL);
draw_sprite(screen, my_pic, 0,0);
destroy_bitmap(my_pic);
drawsquares();
readakey();
END_OF_MAIN()
【问题讨论】:
这段代码几乎无法阅读。 发布一个说明问题的最小代码,拜托!一般来说,这段代码有很多地方可以改进,但这是一个不同的话题。 【参考方案1】:你不能在函数之外这样做:
pic->upperrow=0;
您可以声明(并初始化全局变量),但仅此而已。
【讨论】:
可以在函数之外进行,前提是他将内存分配给 pic 指向的位置。因为,upperrow 是公开的。 @amalantony - 真的吗?在 C++ 中?这对我来说是个新闻……(试试这个:ideone.com/5NwTg) 尼姆,对不起。这是我的一个误解。已投票删除我的答案。以上是关于对象没有命名类型错误的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 测试名称类型错误,如错误:'...'没有命名类型