Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Override

Ostatnio zmodyfikowano 2017-12-28 18:47
Autor Wiadomość
karol1996120
Temat założony przez niniejszego użytkownika
Override
» 2017-12-28 12:08:12
Witam!

Podczas kompilacji programu występuje błąd:

error C3668: 'Block::draw': method with override specifier 'override' did not override any base class methods

Jak rozwiązać następujący problem?

Z góry dziękuję za pomoc.
Pozdrawiam!
P-168171
Chowan
» 2017-12-28 13:08:33
próbujesz nadpisać nie istniejącą funkcje? czyli np zmieniłeś deklaracje metody w klasie bazowej i nie poprawiłeś w pochodnej, albo nie dałeś virtual przed metoda w klasie bazowej.
P-168172
karol1996120
Temat założony przez niniejszego użytkownika
» 2017-12-28 13:54:12
Problem powoduje linijka kodu:

virtual void draw( sf::RenderTarget & target, sf::RenderStates & states ) const override;

Wcześniej identycznie implementowana w innych klasach zachowuje się poprawnie
P-168174
michal11
» 2017-12-28 14:23:30
Bez kodu można tylko zgadywać.
P-168177
RazzorFlame
» 2017-12-28 15:24:25
virtual void draw( RenderTarget & target, RenderStates states ) const = 0;

github.com - Drawable.hpp
W dokumentacji jest błąd, RenderStates nie ma referencji.
W starej wersji SFML (2.0) RenderStates było przekazywane przez referencje, teraz jest przez wartość.
P-168180
karol1996120
Temat założony przez niniejszego użytkownika
» 2017-12-28 15:34:51
To dlaczego w poprzednich klasach wszystko było okej a gdy tworzę 3 na tej samej zasadzie występuje błąd?

Po usunięciu override występuje błąd:

error C2259: 'Block': cannot instantiate abstract class
P-168183
RazzorFlame
» 2017-12-28 15:45:44
Nie masz usunąć override, bo wtedy pozostawiasz czysto wirtualną metodę (draw(...) = 0) z klasy Drawable i przez to nie możesz stworzyć obiektu tej klasy, stąd ten błąd. Masz zmienić "RenderStates &" na "RenderStates".
Poza tym, skoro wcześniej Ci wszystko działało to jest szansa, że masz tą klasę źle zrobioną. Pokaż jej kod.
P-168185
karol1996120
Temat założony przez niniejszego użytkownika
» 2017-12-28 16:31:53
Plik .h
C/C++
#pragma once

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <SFML\Graphics.hpp>

class Block
    : public sf::Drawable
{
public:
    sf::RectangleShape shape;
   
    virtual void draw( sf::RenderTarget & target, sf::RenderStates & states ) const override;
   
   
   
    bool destroyed { false };
   
   
    Block( float t_X, float t_Y, float t_Width, float t_Height );
   
   
   
    void update();
    sf::Vector2f getPosition();
   
    float left();
    float right();
    float top();
    float bottom();
   
    bool isDestroyed();
    void destroy();
    sf::Vector2f getSize();
   
   
   
   
   
};


plik .cpp

C/C++
#include "Block.h"


void Block::draw( sf::RenderTarget & target, sf::RenderStates & states ) const
{
   
    target.draw( shape, states );
}



Block::Block( float t_X, float t_Y, float t_Width, float t_Height )
{
    shape.setPosition( t_X, t_Y );
    shape.setSize( { t_Width, t_Height } );
    shape.setFillColor( sf::Color::Yellow );
    shape.setOrigin( t_Width / 2.f, t_Height / 2.f );
   
}



float Block::left()
{
    return this->shape.getPosition().x - shape.getSize().x / 2.f;
}

float Block::right()
{
    return this->shape.getPosition().x + shape.getSize().x / 2.f;
}

float Block::top()
{
    return this->shape.getPosition().y - shape.getSize().y / 2.f;
}

float Block::bottom()
{
    return this->shape.getPosition().y + shape.getSize().y / 2.f;
}

bool Block::isDestroyed()
{
    return this->destroyed;
}

void Block::destroy()
{
    this->destroyed = true;
}

sf::Vector2f Block::getSize()
{
    return shape.getSize();
}

void Block::update()
{
}

sf::Vector2f Block::getPosition()
{
    return shape.getPosition();
}
P-168188
« 1 » 2
  Strona 1 z 2 Następna strona