0

Im constantly getting a redefinition of default argument parameter 1 error and its preventing from building the project successfully to test code as I include it. Im relatively new to C++ and this form of building a game, having multiple header and cpp files referencing eachother.

Texture2D.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;
    }
}

Texture2D.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

Commons.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
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Jack Wilkes
  • 29
  • 1
  • 2
  • Which line has the error? – drescherjm Apr 18 '19 at 20:47
  • 3
    Simply remove the default specification here: `void Texture2D::Render(Vector2D newPosition, SDL_RendererFlip flip, double angle = 0.0f)` As the compiler tells you, that was already given in the function declaration. – πάντα ῥεῖ Apr 18 '19 at 20:54
  • The line void Texture2D::Render(Vector2D newPosition, SDL_RendererFlip flip, double angle = 0.0f) – Jack Wilkes Apr 18 '19 at 20:55
  • 1
    Interesting fun fact: `_TEXTURE2D_H` and `_COMMONS_H` are illegal identifiers outside of the Standard Library implementation. It is rare that breaking this rule hurts you, but when it does, the results are truly inexplicable... Until you realize you've broken [the rules about using an underscore in a C++ identifier](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – user4581301 Apr 18 '19 at 21:53

2 Answers2

4

When giving a default parameter, you should only add the default in the header declaration. So, changing:

void Texture2D::Render(Vector2D newPosition, SDL_RendererFlip flip, double angle = 0.0f)

to:

void Texture2D::Render(Vector2D newPosition, SDL_RendererFlip flip, double angle)

should fix the error.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Privatized
  • 73
  • 9
1

Simply remove the default specification here: void Texture2D::Render(Vector2D newPosition, SDL_RendererFlip flip, double angle = 0.0f) As the compiler tells you, that was already given in the function declaration. – πάντα ῥεῖ 47 mins ago

Jack Wilkes
  • 29
  • 1
  • 2