Domanda

I have 2 classes that are derived and I want them to have their own constructors, not the base classes'. Here are the header and .cpp with the other member functions stripped.

Header:

#pragma once
#include <SFML/Graphics.hpp>

class TextWrap : public sf::Text
{
public:
    TextWrap() {}
};

class CircleWrap : public sf::CircleShape
{
public:
    CircleWrap() {}
};

Here is the .cpp:

#pragma once
#include "wrappers.h"
#include "stdafx.h"
#include <SFML/Graphics.hpp>

TextWrap::TextWrap(sf::Font font)
{
    setFont(font);
    setCharacterSize(30);
    setStyle(sf::Text::Bold);
    setColor(sf::Color::White);
}

CircleWrap::CircleWrap(void)
{
    setFillColor(sf::Color::Blue);
    setRadius(25);
    setPointCount(100);
}

When this is built I get the following error in Visual C++ 2010:

1>c:\users\joe\documents\visual studio 2010\projects\mouseavoider\mouseavoider\wrappers.cpp(14): error C2511: 'TextWrap::TextWrap(sf::Font)' : overloaded member function not found in 'TextWrap'
1>          c:\users\joe\documents\visual studio 2010\projects\mouseavoider\mouseavoider\wrappers.h(5) : see declaration of 'TextWrap'
1>c:\users\joe\documents\visual studio 2010\projects\mouseavoider\mouseavoider\wrappers.cpp(38): error C2084: function 'CircleWrap::CircleWrap(void)' already has a body
1>          c:\users\joe\documents\visual studio 2010\projects\mouseavoider\mouseavoider\wrappers.h(16) : see previous definition of '{ctor}'

I have a feeling that my syntax is wrong with making the constructor... But I don't know why. I've looked through tutorials and examples online and I don't know why this doesn't work.

EDIT: It's been fixed! I had mixed up the declarations and definitions and needed to fix my declarations of the variables themselves.

È stato utile?

Soluzione

The error in the first constructor is that you're declaring a default constructor, instead of a constructor with an sf::Font as argument.

In the second constructor, you are putting {}, meaning that you're defining the constructor with no content. If you want to define the body of the constructor later you must put a ; at the end.

It should be:

class TextWrap : public sf::Text
{
public:
    TextWrap(sf::Font font);
};

class CircleWrap : public sf::CircleShape
{
public:
    CircleWrap();
};

Altri suggerimenti

You have overloaded the default constructor, but appear to be accessing a non default version.

The header file should contain declarations instead of definitions. TextWrap() {} is defining a constructor that takes no arguments and has an empty body - the braces are the empty body. Your class declaration should look more like the following:

class TextWrap : public sf::Text {
public:
    TextWrap(sf:Font font);
}

The parameters in the declaration/prototype have to match those in the definition (e.g., source file) and the declaration should not include the empty body.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top