1

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

这是我们继承的流类,以简化自定义流缓冲区的管理。它存在的主要原因是删除其析构函数中的自定义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 的东西。据我们所知,我们的代码并非如此。所以我不知道为什么会发生这种情况,或者如何解决它。任何见解将不胜感激。

4

1 回答 1

0

看起来这里有很多额外的不需要的信息,问题的关键是没有流操作符可以从 istream >> 到 ostream。

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

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

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

于 2010-11-17T19:39:08.987 回答