我无法理解为什么在类构造函数中我可以调用此函数但是在测试函数中调用它时会出错
E:\Projects\NasuTek-Plugin-Engine\tests\CheckAddonEngine.cpp:64: error: conversion from 'std::auto_ptr<FakeSettableFeaturePlugin>' to 'std::auto_ptr<FakeFeature>' is ambiguous
C ++文件
#include <ObjectEngine.h>
#include <memory>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
class FakeFeature : public PObject {
public:
inline virtual const char *returnSomthingCool() { return "Somthing Cool"; }
};
class FakeFeaturePlugin : public FakeFeature {
public:
inline const char *returnSomthingCool() { return "Somthing Cool From a Plugin Object"; }
inline std::string getName() { return "Fake Feature Plugin Object"; }
};
class FakeSettableFeaturePlugin : public FakeFeature {
public:
inline const char *returnSomthingCool() { return _value; }
inline char *setSomthingCool(const char *value) { _value = const_cast<char *>(value); }
inline std::string getName() { return "Fake Feature Settable Plugin Object"; }
private:
char *_value;
};
typedef ObjectEngine<FakeFeature> FakeFeatureEngine;
class FakeApplication {
public:
inline FakeApplication(){
_engine = new FakeFeatureEngine();
std::auto_ptr<FakeFeature> pluginToAdd (new FakeFeaturePlugin);
_engine->addObject(pluginToAdd); // Essencially what im doing where it errors, both classes inherit FakeFeature
}
inline ~FakeApplication() {
delete _engine;
}
FakeFeatureEngine *_engine;
};
BOOST_AUTO_TEST_CASE(getengineobject) {
FakeApplication *application = new FakeApplication();
FakeFeature &feature = application->_engine->getObject("Fake Feature Plugin Object");
BOOST_CHECK_EQUAL(feature.getName(), "Fake Feature Plugin Object");
BOOST_CHECK_EQUAL(feature.returnSomthingCool(), "Somthing Cool From a Plugin Object");
delete application;
}
BOOST_AUTO_TEST_CASE(addobjecttoengine) {
FakeApplication *application = new FakeApplication();
std::auto_ptr<FakeSettableFeaturePlugin> plugin (new FakeSettableFeaturePlugin);
plugin.get()->setSomthingCool("Bro I Set this to this value ^_^");
application->_engine->addObject(plugin); // This is the line that fails
FakeFeature &feature = application->_engine->getObject("Fake Feature Settable Plugin Object");
BOOST_CHECK_EQUAL(feature.getName(), "Fake Feature Settable Plugin Object");
BOOST_CHECK_EQUAL(feature.returnSomthingCool(), "Bro I Set this to this value ^_^");
delete application;
}
.h文件
/**
@file ObjectEngine.h
@brief Header File with Plugin Engine Templates to make adding a plugin interface to your app easier
*/
#ifndef NASUTEKPLUGINENGINE_H
#define NASUTEKPLUGINENGINE_H
#include <boost/ptr_container/ptr_list.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <stdio.h>
#include <string>
class PObject {
public:
inline PObject() {}
virtual std::string getName() = 0;
};
template<typename TObjectType>
class ObjectEngine {
public:
ObjectEngine() {}
~ObjectEngine() {
_objects.clear();
}
void addObject(std::auto_ptr<TObjectType> obj) {
if(boost::is_base_of<PObject, TObjectType>::value) {
_objects.push_back(obj.release());
}
}
TObjectType &getObject(std::string objectName) {
typename boost::ptr_list<TObjectType>::iterator i;
for(i = _objects.begin(); i != _objects.end(); i++) {
if((*i).getName() == objectName) {
return *i;
}
}
throw "Object does not exist.";
}
bool objectExist(std::string objectName) {
typename boost::ptr_list<TObjectType *>::iterator i;
for(i = _objects.begin(); i != _objects.end(); i++) {
if((*i).getName() == objectName) {
return true;
}
}
return false;
}
private:
boost::ptr_list<TObjectType> _objects;
};
#endif // NASUTEKPLUGINENGINE_H
我要做的是为我的项目创建一个插件引擎,同时使其可重用,并且C ++文件确保其工作正常。
答案 0 :(得分:3)
这似乎不是Boost测试的问题,本身,但它看起来像addobjecttoengine
中的一行......
std::auto_ptr<FakeSettableFeaturePlugin> plugin (new FakeSettableFeaturePlugin);
......应该......
std::auto_ptr<FakeFeature> plugin(new FakeSettableFeaturePlugin);
...与FakeApplication
的构造函数相同。然后,CheckAddonEngine.cpp:64
无法执行转换。