C程序中的C++头文件和实现代码

标签 c++ c

我有一个用 C++ 编写的 header 和实现代码,它们加载 Sprite 、设置位置、检测碰撞等,但是当我在 C 程序中调用它们时,编译器会向我抛出一堆相同的错误声明类时的头文件和.c文件中,声明类的实例时,错误是这样的:

error: expected  '=', ',', ';', 'asm' or '_attribute_' before Cframe, Csprite,
fnave, fmalo, nave, malo...

在我使用这个变量的函数中,也会向我发送错误

'***' undeclares( first use in this function)...

有人可以告诉我我犯的错误吗,还是你不能在 C 程序中使用 C++ 头文件和实现文件?

附注我把C代码放在最后,我不知道这是否有帮助xD

csprite.h

#ifndef CSPRITE_H_
#define CSPRITE_H_
#define TRUE 1
#define FALSE 0
// CFrame representa un frame independiente de un sprite.
class CFrame {
public:
SDL_Surface *img;
void load(char *path);
void unload();
};
// La clase CSprite está formada por un array de frames;
class CSprite {
private:
int posx,posy;
int estado;
int nframes;
int cont;
public:
CFrame *sprite;
CSprite(int nf);
CSprite();
void finalize();
void addframe(CFrame frame);
void selframe(int nf);
int frames() {return cont;}
void setx(int x) {posx=x;}
void sety(int y) {posy=y;}
void addx(int c) {posx+=c;}
void addy(int c) {posy+=c;}
int getx() {return posx;}
int gety() {return posy;}
int getw() {return sprite[estado].img->w;}
int geth() {return sprite[estado].img->h;}
void draw(SDL_Surface *superficie);
int colision(CSprite sp);
};
#endif /* CSPRITE_H_ */

csprite.cpp

#include <SDL.h>
#include "csprite.h"
// Sprite Class implementation
void CFrame::load( char *path) {
    img=SDL_LoadBMP(path);
    // Asignamos el color transparente al color rojo.
    SDL_SetColorKey(img,SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(img->format,255,0,0));
    img=SDL_DisplayFormat(img);
}
void CFrame::unload(){
    SDL_FreeSurface(img);
}
CSprite::CSprite(int nf) {
    sprite=new CFrame[nf];
    nframes=nf;
    cont=0;
}
CSprite::CSprite() {
    int nf=1;
    sprite=new CFrame[nf];
    nframes=nf;
    cont=0;
}
void CSprite::finalize() {
    int i;
    for (i=0 ; i<=nframes-1 ; i++)
        sprite[i].unload();
}
void CSprite::addframe(CFrame frame) {
    if (cont<nframes) {
        sprite[cont]=frame;
        cont++;
    }
}
void CSprite::selframe(int nf) {
    if (nf<=nframes) {
        estado=nf;
    }
}
void CSprite::draw(SDL_Surface *superficie) {
    SDL_Rect dest;
    dest.x=posx;
    dest.y=posy;
    SDL_BlitSurface(sprite[estado].img,NULL,superficie,&dest);
}
int CSprite::colision(CSprite sp) {
    int w1,h1,w2,h2,x1,y1,x2,y2;
    w1=getw(); // ancho del sprite1
    h1=geth(); // altura del sprite1
    w2=sp.getw(); // ancho del sprite2
    h2=sp.geth(); // alto del sprite2
    x1=getx(); // pos. X del sprite1
    y1=gety(); // pos. Y del sprite1
    x2=sp.getx(); // pos. X del sprite2
    y2=sp.gety(); // pos. Y del sprite2
    if (((x1+w1)>x2)&&((y1+h1)>y2)&&((x2+w2)>x1)&&((y2+h2)>y1)) {
        return TRUE;
    } else {
        return FALSE;
    }
}

游戏.c

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include "csprite.h"

SDL_Surface *screen;
CFrame fnave;
CFrame fmalo;
CSprite nave(1);
CSprite malo(1);
SDL_Rect rectangulo;
SDL_Joystick *joystick;
int joyx, joyy;
int done=0;
// estructura que contiene la información
// de nuestro avión
struct minave {
    int x,y;
} jugador;
// Estructura que contiene información
// del avión enemigo
struct naveenemiga {
    int x,y;
} enemigo;
// Dibuja los esprites en la pantalla
void DrawScene(SDL_Surface *screen) {
    // borramos el avión dibujado
    // en el frame anterior
    rectangulo.x=nave.getx();
    rectangulo.y=nave.gety();
    rectangulo.w=nave.getw();
    rectangulo.h=nave.geth();
    SDL_FillRect(screen,&rectangulo,SDL_MapRGB(screen->format,0,0,0));
    // dibuja avión
    nave.setx(jugador.x);
    nave.sety(jugador.y);
    nave.draw(screen);
    // Dibuja enemigo
    malo.setx(enemigo.x);
    malo.sety(enemigo.y);
    malo.draw(screen);
    // ¿ha colisionado con la nave?
    if (malo.colision(nave) == TRUE) {
        done=1;
    }
    // Mostramos todo el frame
    SDL_Flip(screen);
}
// Inicializamos estados
void inicializa() {
    jugador.x=300;
    jugador.y=300;
    enemigo.x=100;
    enemigo.y=100;
}
void finaliza() {
    // finalizamos los sprites
    nave.finalize();
    malo.finalize();
    // cerramos el joystick
    if (SDL_JoystickOpened(0)) {
        SDL_JoystickClose(joystick);
    }
}
// Preparamos los esprites
int InitSprites() {
    fnave.load("minave.bmp");
    nave.addframe(fnave);
    fmalo.load("nave.bmp");
    malo.addframe(fmalo);
    return 0;
}
int main(int argc, char *argv[]) {
    SDL_Event event;
    Uint8 *keys;
    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0) {
        printf("No se pudo iniciar SDL: %s\n",SDL_GetError());
        return 1;
    }
    screen = SDL_SetVideoMode(640,480,24,SDL_HWSURFACE);
    if (screen == NULL) {
        printf("No se puede inicializar el modo gráfico: %s\n",SDL_GetError());
        return 1;
    }
    atexit(SDL_Quit);
    inicializa();
    InitSprites();
    while (done == 0) {
        // dibujamos el frame
        DrawScene(screen);
        // consultamos el estado del teclado
        keys=SDL_GetKeyState(NULL);
        // consultamos el estado del joystick
        SDL_JoystickUpdate();
        joyx = SDL_JoystickGetAxis(joystick, 0);
        joyy = SDL_JoystickGetAxis(joystick, 1);
        if ((keys[SDLK_UP] || joyy < -10) && (jugador.y > 0)) 
            {jugador.y=jugador.y-(5);}
        if ((keys[SDLK_DOWN] || joyy > 10) && (jugador.y < 460)) 
            {jugador.y=jugador.y+(5);}
        if ((keys[SDLK_LEFT] || joyx < -10) && (jugador.x > 0)) 
            {jugador.x=jugador.x-(5);}
        if ((keys[SDLK_RIGHT] || joyx > 10) && (jugador.x < 620)) 
            {jugador.x=jugador.x+(5);}
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {done=1;}
            if (event.type == SDL_KEYDOWN || event.type == SDL_JOYBUTTONDOWN) {
                if (event.key.keysym.sym == SDLK_ESCAPE) {
                    done=1;
                }
            }
        }
     }
    finaliza();
    return 0;
}

最佳答案

不,您不能在 C 程序中使用 C++ 头文件,因为您的 C++ 头文件使用 C++ 语言功能(如类)。

当且仅当头文件仅使用 C 和 C++ 都支持的语言功能时,您才可以在 C 和 C++ 程序中使用头文件。

关于C程序中的C++头文件和实现代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8781539/

相关文章:

C++ 获取对返回对象的引用

c++ - 通过鼠标方向绘制一条长度恒定的线

c - 如何拆分字符串并将其存储在不同的变量中

c - 使用嵌套循环翻转C中的数组元素数据

C 用循环初始化结构

c++ - Arduino C 函数将字符串转换为十六进制?

C++ 采用华氏温度数组,将它们转换为摄氏温度,然后将摄氏温度添加到新数组中

c++ - 使用链表从双端队列中删除最后一个节点时避免内存泄漏

c - 例如,在 C 中定义变量时,该变量的内存地址存储在哪里?

c - C语言变量是如何存储的(在堆栈、列表、树中?)它们存储在哪里?(在ram/hdd中)?