使用带有模糊过载错误的Ogre3d自定义流/ streambuf

时间:2010-11-15 05:04:42

标签: c++ iostream std ogre3d

目前正在尝试使用Ogre和STL创建XML系统。作为概念证明,我们试图让它将XML文件的内容输出到我们的日志文件中。可悲的是,我们当前的代码不会编译,我们不知道为什么。相关代码如下:

这是我们继承的流类,以简化自定义streambuf的管理。它存在的主要原因是删除了它的析构函数中的自定义streambuf。

class ResourceInputStream : public std::istream
{
    private:
        internal::OgreDataStreamBuf* OgreBuffer;
        ResourceManager* Manager;

        /// @internal
        /// @brief Called by the constructors to actually construct this class
        /// @param InputBuffer A pointer to an internal::OgreDataStreamBuf. A buffer to read from the disk access subsystem (which happens to be part of Ogre). This Stream will assume ownership of this buffer and will handle deleting it.
        /// @param ResourceManager_ Currently unused, future functionality may tuse this.
        void Construct(std::streambuf *InputBuffer, ResourceManager* ResourceManager_);

    protected:

    public:
        /// @brief Descriptive Constructor
        /// @param InputBuffer A pointer to an internal::OgreDataStreamBuf. A buffer to read from the disk access subsystem (which happens to be part of Ogre). This Stream will assume ownership of this buffer and will handle deleting it.
        /// @param ResourceManager_ Currently unused, future functionality may tuse this.
        /// @warning Do not delete the InputBuffer you pass in, this class will assume owner ship and delete it on it's own
        ResourceInputStream(std::streambuf *InputBuffer, ResourceManager* ResourceManager_) :
            std::istream(InputBuffer)
            { this->Construct(InputBuffer, ResourceManager_); }

        /// @brief Tears down the Stream, and Delete the Buffer Passed in.
        virtual ~ResourceInputStream();

};

以下是自定义streambuf类的定义。它使用ogreDatastreambuf来读取由Ogre资源库管理的压缩文件:

 class OgreDataStreamBuf : public std::streambuf
        {
            protected:
                /// @brief a shard_ptr to the internal Ogre Datastream
                Ogre::DataStreamPtr OgreStream;

            public:

                /// @brief constructor
                /// @param Datum A pointer to the Ogre Datastream that this stream will use
                OgreDataStreamBuf(const Ogre::DataStreamPtr& Datum) : OgreStream(Datum)
                {
                    #ifdef PHYSDEBUG
                    World::GetWorldPointer()->Log("Entering/Exiting OgreDataStreamBuf Constructor");
                    #endif
                }

                /// @brief Should get the amount of characters left in the sequence
                /// @returns -1 if no estimate could be made, other wise this returns an estimate of the amount of bytes in the buffer
                std::streamsize showmanyc();

                /// @brief Gets a sequence of characters
                /// @param s a Pointer to where the characters should go
                /// @param n How many characters
                /// @return This returns the amount of characters retrieved
                std::streamsize xsgetn(char* s, std::streamsize n);

                /// @brief puts a sequence of characters in
                /// @param s a Pointer to the characters
                /// @param n How many characters
                /// @return This returns the amount of characters inserted
                /// @detail currently unimplimented
                std::streamsize xsputn(const char_type*, std::streamsize n);
        };
    }

以下是尝试使用此流类的代码段。 TheWorld-> LogStream是一个std :: stringstream。

ResourceInputStream* XMLptr = TheWorld->GetResourceManager()->GetResourceStream("test.xml");
std::stringstream XMLStringStream;
(*XMLptr) >> XMLStringStream;
String ShouldHaveXML(XMLStringStream.str());
TheWorld->LogStream << "ShouldHaveXML: " << ShouldHaveXML << endl << "End XML Logging" <<endl;
TheWorld->Log("Delete XML Stream");
delete XMLptr;

尝试编译会产生此错误:

error: ambiguous overload for 'operator>>' in '* XMLptr >> XMLStringStream'

我已经研究过这个错误,我唯一能找到的就是这个......这是因为某些东西被声明为const而不应该。据我们所知,我们的代码并非如此。所以我不知道为什么会发生这种情况,或者如何解决这个问题。任何见解将不胜感激。

1 个答案:

答案 0 :(得分:0)

这里似乎有很多额外不需要的信息,问题的关键在于没有流媒体运营商从istream获得&gt;&gt;到了ostream。

所有Ogre和自定义类的东西根本不是必需的,同时我们设法使用简单的解决方法将数据从类中取出:

ResourceInputStream* XMLptr = TheWorld->GetResourceManager()->GetResourceStream("test.xml");
char chararray[401];
chararray[400]='\0';
XMLptr->read(chararray, 400);
String ShouldHaveXML( chararray );

这只是测试代码,我建议您在假设读取所请求的数据量之前使用istream :: gcount。