我正在尝试通过自己的游戏引擎来创建游戏引擎,其中每个基本引擎都是一个GameObject
,其中将包含一个更新功能和其他通知程序。
到目前为止,这是我的代码:
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "AssetManager.hpp"
class GameObject : public sf::Drawable, public sf::Transformable
{
public:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const{
target.draw(*this, states);
};
};
class Sprite : public GameObject, public sf::Sprite {
public:
Sprite(AssetManager assetManager, std::string assetName) : sf::Sprite(){
setTexture(assetManager.getTextureNamed(assetName));
}
};
int main(int argc, char const** argv)
{
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "");
AssetManager assetManager = AssetManager("/Users/iProgram/Desktop/My Game Engine/My Game Engine");
assetManager.loadTextureWithName("tank", "tank.png", sf::Vector2f(16,16));
Sprite tank = Sprite(assetManager, "tank");
while(window.isOpen()){
window.clear();
window.draw(tank);
window.display();
}
return EXIT_SUCCESS;
}
请注意,资产管理器只是将纹理存储在内存中,并在需要时以给定大小返回纹理。
问题在线上:window.draw(tank);
,我收到了错误消息
从派生类'const Sprite'到基类'const sf :: Drawable'的不明确转换:
这是为什么,因为我在GameObject
类中实现了draw函数,我该如何解决?
答案 0 :(得分:1)
您的Sprite
类继承自GameObject
,而sf::Drawable
继承自sf::Sprite
。您的课程还继承自sf::Drawable
,而 也继承自Sprite
。现在,您的sf::Drawable
类具有2个继承的from tensorflow.keras.models import Sequential
import tensorflow_probability as tfp
import tensorflow as tf
def train_BNN(training_data, training_labels, test_data, test_labels, layers, epochs):
bayesian_nn = Sequential()
nbr_samples, dim = training_data.shape
training_labels = training_labels.reshape((nbr_samples, 1))
training_data = tf.convert_to_tensor(training_data, tf.float32)
training_labels = tf.convert_to_tensor(training_labels, tf.float32)
for i in range(0, len(layers)):
bayesian_nn.add(tfp.layers.DenseFlipout(layers[i], activation='relu'))
bayesian_nn.add(tfp.layers.DenseFlipout(1, activation='sigmoid'))
# Define loss function
logits = bayesian_nn(training_data)
neg_log_likelihood = tf.losses.sigmoid_cross_entropy(training_labels, logits=logits)
kl = sum(bayesian_nn.losses)
loss = neg_log_likelihood + kl
# Define optimizer
train_op = tf.train.AdamOptimizer().minimize(loss)
bayesian_nn.compile(train_op, loss=loss, metrics=['accuracy'])
子对象,编译器无法选择。
要解决此问题,请重新考虑您的设计或虚拟继承。
答案 1 :(得分:0)
要解决此问题,我不必像某些建议那样使用私有继承,我只需要基类(GameObject)即可不继承任何内容。然后,我的Sprite类将继承sf::Sprite
,如下所示:
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include "AssetManager.hpp"
class GameObject{
public:
virtual void update(){}
};
class Sprite : public GameObject, public sf::Sprite {
public:
Sprite(AssetManager* assetManager, std::string assetName) : sf::Sprite(assetManager->getTextureNamed(assetName)){
}
};
class Tank: public Sprite{
public:
Tank(AssetManager* assetManager) : Sprite(assetManager, "tank"){
setScale(3, 3);
};
virtual void update(){
move(1,0);
}
};
int main(int argc, char const** argv)
{
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "");
AssetManager* assetManager = new AssetManager("/Users/iProgram/Desktop/My Game Engine/My Game Engine");
assetManager->loadTextureWithName("tank", "tank.png", sf::Vector2f(16,16));
Tank tank = Tank(assetManager);
while(window.isOpen()){
sf::Event event;
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed){
window.close();
}
}
tank.update();
window.clear();
window.draw(tank);
window.display();
}
return EXIT_SUCCESS;
}