不同文件c++中的派生类

derived clases in different files c++

本文关键字:派生 c++ 文件      更新时间:2023-10-16

如何在file_x.h和file_x.cpp中创建一个主类,然后从file_x、file_y.h和file_y.cpp 中创建的类派生类

我尝试了很多东西,但都没有得到任何好的结果。你能给我一个例子,说明这两个类中的构造函数、变量和正则函数吗?

我在窗口中有这个。h(主类标题):

#ifndef WINDOW_H_
#define WINDOW_H_
#include <string>
#include <vector>
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "../../utils/event.h"
#include "../../utils/button.h"
using namespace std;
class bwindow{
    protected:
        SDL_Surface* up_bar;
        SDL_Surface* content;
        SDL_Surface* to_blit;
        SDL_Surface* text;
        vector <button*> botns;
        string name;
        int x;
        int y;
        bool pressed;
        int sx;
        int sy;
    public:
        bwindow(string,int,int,int,int,int*,int*);
        void graphic_update(SDL_Surface*);
        void logic_update(events*);
        ~bwindow();
        string get_name();
};
#endif /* WINDOW_H_ */

这在window.cpp(主类文件)中:

#include <string>
#include <vector>
#include <iostream>
#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "SDL/SDL_ttf.h"
#include "../../utils/SDL_functions.h"
#include "../../utils/event.h"
#include "../../extra_data/extra_data.h"
#include "window.h"
using namespace std;
bwindow::bwindow(string title,int xp,int yp,int w,int h,int a[], int b[]){
    //seting variables
    x = xp;
    y = yp;
    sx = x;
    sy = y;
    name = title;
    pressed = false;

    //seting masks
    Uint32 rmask = 0x00000000;
    Uint32 gmask = 0x00000000;
    Uint32 bmask = 0x00000000;
    Uint32 amask = 0x00000000;
    //creating up_bar
    up_bar = SDL_CreateRGBSurface(SDL_SWSURFACE, w, 20, 32,rmask, gmask, bmask, amask);
    SDL_FillRect( up_bar, &up_bar->clip_rect, SDL_MapRGB( up_bar->format, 0xFF, 0xFF, 0xFF ) );
    boxRGBA(up_bar   ,0, 0, w,20,a[0],a[1],a[2],a[3]);
    //creating content area
    content = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h-30, 32,rmask, gmask, bmask, amask);
    boxRGBA(content,0, 0,w, h - 25,b[0],b[1],b[2], b[3]);
    //adding borders
    sdlf::add_border(up_bar,1,1,1,1,0,0,0,255);
    sdlf::add_border(content,1,1,1,1,0,0,0,255);
    //adding the up-text
    SDL_Color textColor = { 0 , 0 ,0  };
    TTF_Font *font;
    font =  fonts::load_john_han(15);
    text = TTF_RenderText_Solid(font,title.c_str(),textColor);
    apply_surface(20,5,text,up_bar);
    //creating final surface
    to_blit = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,rmask, gmask, bmask, amask);
}
void bwindow::graphic_update(SDL_Surface* screen){
    SDL_FillRect( to_blit, &to_blit->clip_rect, SDL_MapRGB( to_blit->format, 0xFF, 0xFF, 0xFF ) );


    apply_surface(0,0, up_bar, to_blit);
    apply_surface(0,20,content,to_blit);
    apply_surface(x,y,to_blit,screen);
}
void bwindow::logic_update(events* evs){
    mouse m = evs->get_mouse();
    bool condition1 = m.x > x and m.x < x + up_bar->w;
    bool condition2 = m.y > y and m.y < y + up_bar->h;
    bool condition3 = m.left;
    if (condition1 and condition2){
        if (condition3 and pressed == false){
            pressed = true;
            sx = m.x - x;
            sy = m.y - y;
        }else if (not(condition3)){
            pressed = false;
        }
    }
    if (condition3 and pressed == true){
        x =   (m.x - sx) ;
        y =   (m.y - sy) ;
    }
}
string bwindow::get_name(){
    return name;
}
bwindow::~bwindow(){
}

window_types.h(派生类头)中的this:

#ifndef WINDOW_TYPES_H_
#define WINDOW_TYPES_H_
#include <string>
#include <vector>
#include "window.h"
using namespace std;
namespace windows{
    class message_window : public bwindow{
        private:
            vector <string> message;
            string title;
        public:
            message_window();
            void start(string,vector<string>,int);
            void test();
    };
}
#endif /* WINDOW_TYPES_H_ */

在window_types.cpp中(我得到一个错误"调用'bwindow::bwindow()没有匹配的函数"):

#include <string>
#include <vector>
#include "../../utils/utilsf.h"
#include "window_types.h"

using namespace std;
//HERE THE ERROR
windows::message_window::message_window(){
}
void windows::message_window::start(string title,vector <string> content,int s){
    int yp = 200;
    int xp = 300;
    int w = 100;
    int h = 50;
    int a[4];
    int b[4];
    a[0] = utilsf::randrange(0,256,s);
    a[1] = utilsf::randrange(0,256,s);
    a[2] = 200;
    a[3] = 255;
    b[0] = 200;
    b[1] = utilsf::randrange(0,256,s);
    b[2] = utilsf::randrange(0,256,s);
    b[3] = 255;
    bwindow(title,xp,yp,w,h,a,b);
}
void windows::message_window::test(){
}

您的#includes很好。但是,您没有正确初始化对象实例。你必须在派生类的构造函数中初始化基类,而不是在其他地方!此外,不能在派生构造函数的主体中调用基构造函数。C++使用一个称为"初始化列表"的特殊构造来实现这一点。它在参数之后、构造函数主体之前用分号表示。请参阅C++构造函数名称后面的冒号有什么作用?了解更多详细信息。

由于您没有在message_window的构造函数中初始化bwindow,C++编译器插入了对默认构造函数(即bwindow::bwindow())的隐式调用。但由于不存在这样的构造函数,您会得到一个编译器错误。

message_window构造函数应该如下所示:

void windows::message_window::message_window(string title,
                                    vector <string> content,
                                    int s, int a[], int b[])
: bwindow(title, 300, 200, 100, 50, a, b),
  name(title),
  contents(content)
{
   // the constructor body is empty in this case
}

请注意,不能像以前那样初始化ab阵列。message_window的调用方可能已经执行了该操作。

类似地,bwindow构造函数应该使用如下初始化列表:

bwindow::bwindow(string title,int xp,int yp,int w,int h,int a[], int b[])
: x(xp), y(yp), sx(x), sy(y), name(title), pressed(false)
{
    //setting masks
    Uint32 rmask = 0x00000000;
    Uint32 gmask = 0x00000000;
    Uint32 bmask = 0x00000000;
    Uint32 amask = 0x00000000;
    //rest of the of the code omitted
}

旁注:您应该将析构函数声明为虚拟的。