我最近开始学习C ++,而且我已经暂时停留在这个错误上了一段时间。寻找类似的问题并没有给我一个答案。
main.cpp的一部分:
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <string>
#include "spritesheet.h"
using namespace std;
spriteSheet charimage("../img/char.png");
spriteSheet bgimage("../img/bg.png");
class playerSprite
{
public:
playerSprite(int xpos, int ypos, spriteSheet sheet);
int pos[2]; // player postition
spriteSheet image;
};
playerSprite::playerSprite(int xpos, int ypos, spriteSheet sheet)
{
pos[0] = xpos;
pos[1] = ypos;
image = sheet;
}
playerSprite player(0, 0, charimage);
spritesheet.h:
#ifndef LTEXTURE_H
#define LTEXTURE_H
using namespace std;
extern SDL_Renderer* screen;
class spriteSheet //Texture wrapper for all game objects
{
SDL_Texture* sheet;
SDL_Rect ssize;
public:
spriteSheet(string path);
~spriteSheet();
void setClip(int x = 0, int y = 0, int w = 0, int h = 0);
void render(int x = 0, int y = 0);
void free();
SDL_Rect sclip;
};
#endif // LTEXTURE_H
spritesheet.cpp的一部分:
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <string>
#include "spritesheet.h"
using namespace std;
spriteSheet::spriteSheet(string path)
{
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if(loadedSurface == NULL)
{
cout << IMG_GetError() << endl;
}
else
{
newTexture = SDL_CreateTextureFromSurface(screen, loadedSurface);
if(newTexture == NULL)
{
cout << SDL_GetError() << endl;
}
else
{
ssize.w, sclip.w = loadedSurface->w;
ssize.h, sclip.h = loadedSurface->h;
}
SDL_FreeSurface(loadedSurface);
}
sheet = newTexture;
}
当我构造我的playerSprite对象时,我想为它传入一个spriteSheet对象。我已经通过使用像playerSprite :: setSheet()和spriteSheet :: setImage()这样的函数来编译这些代码,但是我无法让它与构造函数一起工作。每次我尝试编译时都会收到此错误:
C:\Users\Platino\Documents\C++\Dragorogue\main.cpp In constructor 'playerSprite::playerSprite(int, int, spriteSheet)':
20 65 C:\Users\Platino\Documents\C++\Dragorogue\main.cpp [Error] no matching function for call to 'spriteSheet::spriteSheet()'
20 65 C:\Users\Platino\Documents\C++\Dragorogue\main.cpp [Note] candidates are:
5 0 C:\Users\Platino\Documents\C++\Dragorogue\main.cpp In file included from main.cpp
13 3 C:\Users\Platino\Documents\C++\Dragorogue\spritesheet.h [Note] spriteSheet::spriteSheet(std::string)
13 3 C:\Users\Platino\Documents\C++\Dragorogue\spritesheet.h [Note] candidate expects 1 argument, 0 provided
7 7 C:\Users\Platino\Documents\C++\Dragorogue\spritesheet.h [Note] spriteSheet::spriteSheet(const spriteSheet&)
7 7 C:\Users\Platino\Documents\C++\Dragorogue\spritesheet.h [Note] candidate expects 1 argument, 0 provided
编辑:
LogicStuff建议的方法会强制我在每次构造一个新的playerSprite对象时初始化一个新的SpriteSheet对象。我想创建一个SpriteSheet对象,让所有的playerSprite对象都使用它。
答案 0 :(得分:0)
由于您的spriteSheet
对象没有默认构造函数,因此问题是spriteSheet
类中有一个playerSprite
成员,需要使用一个参数构造。该成员是image
。
class spriteSheet
{
spriteSheet(string path); // <-- This is your constructor
};
class playerSprite
{
public:
playerSprite(int xpos, int ypos, spriteSheet sheet);
spriteSheet image; // <-- This member has to be initialized on construction.
};
构建playerSprite
时,必须初始化spriteSheet
成员。初始化image
成员的唯一方法是在成员初始化列表中使用一个参数构造它:
playerSprite::playerSprite(int xpos, int ypos, spriteSheet sheet) :
image("somestring")
{
//...
}
注意 - 构造函数体内的代码({ }
之间)是不是初始化。这是任务 - 一个很大的不同。要初始化,必须使用成员初始化列表。
此外,您应该通过const引用传递spriteSheet
,而不是通过值传递:
playerSprite::playerSprite(int xpos, int ypos, const spriteSheet& sheet) :
image("somestring")
{
image = sheet; // now we can assign.
}
进入构造函数后,您就可以对成员进行分配: