我是C ++的初学者,正在尝试做蛇,但是有问题。
错误行:
C:\Users\Kcper\Desktop\Snej w allegro\main.cpp||In function 'void display_game()':|
C:\Users\Kcper\Desktop\Snej w allegro\main.cpp|58|warning: left operand of comma operator has no effect [-Wunused-value]|
C:\Users\Kcper\Desktop\Snej w allegro\main.cpp|58|error: conversion from 'int' to non-scalar type 'const Vec2' requested|
这是一个代码:
#include <allegro.h>
#include <winalleg.h>
#define TICS_PER_SECOND 50
#define MAX_FRAMESKIP 10
#define MAX_MENU_STATES 4
const int SKIP_TICKS = 1000 / TICS_PER_SECOND;
BITMAP * buffer;
struct Vec2
{
int x;
int y;
};
enum States
{
state_menu,
state_game,
state_continue,
state_exit,
state_leaderboard,
};
enum Positions
{
pos_game=30,
pos_continue=50,
pos_exit=70,
pos_leaderboard=90,
};
States menu(int *state)
{
int value;
if (keypressed())
{
value=readkey();
switch(value>>8)
{
case KEY_DOWN:
if(*state<MAX_MENU_STATES)(*state)++;
break;
case KEY_UP:
if(*state>1)(*state)--;
break;
case KEY_ENTER:
return States(*state);
}
}
return state_menu;
}
void display_game()
{
int color_zielony=makecol(0,255,0);
int color_czerwony=makecol(255,0,0);
const Vec2 ARENA_POSITION=(0,50); //<---- There is a problem
rect(buffer,ARENA_POSITION.x,ARENA_POSITION.y,SCREEN_H-1,SCREEN_W-1,color_czerwony);
textout_centre_ex(buffer,font,"Carrotz: ",SCREEN_W/2,SCREEN_H/2,color_zielony,0);
}
void display_menu(States state)
{
int color_bialy=makecol(255,255,255);
int color_czerwony=makecol(255,0,0);
int color_niebieski=makecol(0,0,255);
int color_zielony=makecol(0,255,0);
textout_centre_ex(buffer,font,"SNAKE by Kacper",SCREEN_W/2,10,color_niebieski,0);
textout_centre_ex(buffer,font,"New Game",SCREEN_W/2,30,color_bialy,2);
textout_centre_ex(buffer,font,"Leadrboard",SCREEN_W/2,70,color_bialy,2);
textout_centre_ex(buffer,font,"Continue",SCREEN_W/2,50,color_bialy,2);
textout_centre_ex(buffer,font,"Exit",SCREEN_W/2,90,color_czerwony,2);
int position;
switch(state)
{
case state_game:
position=pos_game;
break;
case state_continue:
position=pos_continue;
break;
case state_leaderboard:
position=pos_leaderboard;
break;
case state_exit:
position=pos_exit;
break;
}
rectfill(buffer,SCREEN_W/2-100,position,SCREEN_H-180,position+10,color_zielony);
}
inline void init()
{
allegro_init();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED,760,480,0,0);
install_timer();
install_keyboard();
install_mouse();
}
inline void deinit()
{
destroy_bitmap(buffer);
clear_keybuf();
allegro_exit();
}
int main()
{
init();
States currentState=state_menu;
int currentMenuState=1;
int next_game_tick = GetTickCount();
int loops;
float interpolation;
buffer=create_bitmap (SCREEN_W,SCREEN_H);
while (!key[KEY_ESC])
{
loops=0;
while (GetTickCount()>next_game_tick&&loops<MAX_FRAMESKIP)
{
loops++;
next_game_tick+=SKIP_TICKS;
switch (currentState)
{
case state_menu:
currentState=menu(¤tMenuState);
break;
case state_game:
break;
}
}
clear(buffer);
switch (currentState)
{
case state_menu:
display_menu(States(currentMenuState));
break;
case state_game:
display_game();
break;
}
blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
}
deinit();
return 0;
}
END_OF_MAIN()
那我该如何解决这个问题呢?
最佳答案
const Vec2 ARENA_POSITION = (0, 50);
不是aggregate initialization的语法。而是用括号括起来的comma operator,它丢弃
0
(警告逗号运算符的操作数无效)并尝试执行const Vec2 ARENA_POSITION = 50;
正确的语法是以下两种之一:
const Vec2 ARENA_POSITION = {0, 50};
const Vec2 ARENA_POSITION{0, 50};
关于c++ - 从'int'转换为非标量类型常量'Vec2',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35767242/