C++ 默认参数的重新定义:参数 1 (矢量 2D)
C++ Redefinition of Default Argument: Parameter 1 (Vector2D)
我不断重新定义默认参数参数 1 错误,并阻止成功构建项目以测试我包含的代码。我对C++和这种构建游戏的形式相对较新,有多个标头和 cpp 文件相互引用。
纹理2D.cpp:
#include <iostream>
#include <SDL_image.h>
#include <string>
#include "Texture2D.h"
#include "Constants.h"
#include "Commons.h"
using namespace::std;
Texture2D::Texture2D(SDL_Renderer* renderer)
{
SDL_Renderer* mRenderer = NULL;
SDL_Texture* mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
Texture2D::~Texture2D()
{
Free();
mRenderer = NULL;
}
bool Texture2D::LoadFromFile(string path)
{
//remove the memory used for a previous texture
Free();
SDL_Texture* mTexture = NULL;
//load the image
SDL_Surface* pSurface = IMG_Load(path.c_str());
mWidth = pSurface->w;
mHeight = pSurface->h;
if (pSurface != NULL)
{
mTexture = SDL_CreateTextureFromSurface(mRenderer, pSurface);
if (mTexture == NULL)
{
cout << "Unable to create texture from surface. Error: " << SDL_GetError() << endl;
}
//Color key the image - The color to be transparent
SDL_SetColorKey(pSurface, SDL_TRUE, SDL_MapRGB(pSurface->format, 0, 0xFF, 0xFF));
SDL_FreeSurface(pSurface);
return mTexture;
}
else
{
cout << "Unable to create texture from surface. Error: " << IMG_GetError() << endl;
}
}
void Texture2D::Render(Vector2D newPosition, SDL_RendererFlip flip, double angle = 0.0f)
{
//clear the screen
SDL_SetRenderDrawColor(mRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(mRenderer);
//set where to render the texture
SDL_Rect renderLocation = { 0, 0, mWidth, mHeight };
//render to screen
SDL_RenderCopyEx(mRenderer, mTexture, NULL, &renderLocation, 0, NULL, SDL_FLIP_NONE);
SDL_RenderPresent(mRenderer);
}
void Texture2D::Free()
{
if (mTexture != NULL)
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
纹理2D.h:
#pragma once
#ifndef _TEXTURE2D_H
#define _TEXTURE2D_H
#include <SDL.h>
#include <SDL_image.h>
#include <map>
#include <string>
#include "Commons.h"
class Texture2D
{
public:
Texture2D(SDL_Renderer* renderer);
~Texture2D();
bool LoadFromFile(std::string path);
void Free();
void Render(Vector2D newPosition, SDL_RendererFlip flip, double angle = 0.0f);
int GetWidth() { return mWidth; }
int GetHeight() { return mHeight; }
private:
SDL_Renderer* mRenderer;
SDL_Texture* mTexture;
int mWidth;
int mHeight;
};
#endif //_TEXTURE2D_H
共享资源:
#pragma once
#ifndef _COMMONS_H
#define _COMMONS_H
struct Vector2D
{
Vector2D()
{
x = 0.0f;
y = 0.0f;
}
float x;
float y;
};
struct InitialVector2D
{
InitialVector2D()
{
initialx = 0.0f;
initialy = 0.0f;
}
float initialx;
float initialy;
};
enum SCREENS
{
SCREEN_INTRO = 0,
SCREEN_MENU,
SCREEN_LEVEL1,
SCREEN_LEVEL2,
SCREEN_GAMEOVER,
SCREEN_HIGHSCORES
};
#endif //_COMMONS_H
给出默认参数时,只应在标头声明中添加默认值。因此,更改:
void Texture2D::Render(Vector2D newPosition, SDL_RendererFlip flip, double angle = 0.0f)
自:
void Texture2D::Render(Vector2D newPosition, SDL_RendererFlip flip, double angle)
应该修复错误。
只需在此处删除默认规范:void 纹理2D::渲染(矢量2D newPosition,SDL_RendererFlip翻转,双角度= 0.0f( 正如编译器告诉您的那样,这已经在函数声明中给出。 – πάντα ῥεῖ 47分钟前
相关文章:
- 何时应通过引用传递矢量参数而不是按值传递矢量参数?
- 以矢量作为参数的地图
- 将矢量设置为常规参数
- 如果模板模板参数是矢量,则需要不同的行为
- std::具有两个输入参数的矢量构造函数
- C++ 和 Lua 函数之间的交互与 3D 矢量参数
- 如何通过对象参数从指针矢量中删除对象和指针
- 如何声明一个接受任何大小的 2D 矢量参数?
- 是否可以使用模板非类型参数使用#if指令?(矢量多合一类)
- C 初始化矢量每个元素,带有另一个向量的参数
- C++ 默认参数的重新定义:参数 1 (矢量 2D)
- 作为矢量构造函数的实际参数的初始值设定项
- 矢量模板参数无效
- 将模板类型参数的映射存储到C++中的矢量中:Visual Studio
- 具有参数化构造器的2D矢量
- 如何将字符串类型的矢量元素作为参数传递给pthread_create()
- C++ std::括号中的矢量参数 - 解释
- 将参数包转换为矢量
- 提升program_options:在 3D 矢量中读取作为命令行参数
- 为什么在unordered_map emplace中使用矢量构造函数仅占参数的最后值