0

我不明白为什么在类构造函数中我可以调用这个函数,但是在测试函数中调用时,它会出错

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++ 文件确保其正常工作。

4

1 回答 1

3

这看起来不像 Boost Test本身的问题,但它看起来像addobjecttoengine...

std::auto_ptr<FakeSettableFeaturePlugin> plugin (new FakeSettableFeaturePlugin);

...应该...

std::auto_ptr<FakeFeature> plugin(new FakeSettableFeaturePlugin);

FakeApplication...与的构造函数中的相同。然后没有转换可以在CheckAddonEngine.cpp:64.

于 2011-12-31T08:00:42.577 回答