C ++错误:类没有名为的成员

时间:2013-06-07 09:06:09

标签: c++ dtn

我有这个问题

MemoryBundleStorage.cpp: In member function 'virtual void dtn::storage::MemoryBundleStorage::store(const dtn::data::Bundle&)':
MemoryBundleStorage.cpp:146:67: error: 'const class dtn::data::Bundle' has no member named 'getClass'
MemoryBundleStorage.cpp:150:19: error: 'const class dtn::data::Bundle' has no member named 'getClass'

在Eclipse中编译此函数时

void MemoryBundleStorage::store(const dtn::data::Bundle &bundle)
        {
            std::cout << "MemoryBundleStorage store" << std::endl;
            ibrcommon::MutexLock l(_bundleslock);
            std::cout << "Storing bundle(Memory). His class is " << bundle.getClass() << std::endl; //<<Here ERROR
}

部分Bundle.h文件

namespace dtn
{
    namespace security
    {
        class StrictSerializer;
        class MutualSerializer;
    }

    namespace data
    {
        class CustodySignalBlock;
        class StatusReportBlock;

        class Bundle : public PrimaryBlock
        {
            friend class DefaultSerializer;
            friend class DefaultDeserializer;
            friend class dtn::security::StrictSerializer;
            friend class dtn::security::MutualSerializer;

        public:

            int getClass() const;

...
}

方法getClass()的实现如下(Bundle.cpp)

namespace dtn
{
    namespace data
    {
        Bundle::Bundle()
        {
            // if the timestamp is not set, add a ageblock
            if (_timestamp == 0)
            {
                // add a new ageblock
                push_front<dtn::data::AgeBlock>();
            }
        }

        int Bundle::getClass() const{
                 whatever
                }

}

我做错了什么?也许打电话做得不好?

谢谢!

Edit1:完成bundle.h

#ifndef BUNDLE_H_
#define BUNDLE_H_

#include "ibrdtn/data/Dictionary.h"
#include "ibrdtn/data/PrimaryBlock.h"
#include "ibrdtn/data/Block.h"
#include "ibrdtn/data/PayloadBlock.h"
#include "ibrdtn/data/EID.h"
#include "ibrdtn/data/ExtensionBlock.h"
#include "ibrcommon/refcnt_ptr.h"
#include <ostream>
#ifdef __DEVELOPMENT_ASSERTIONS__
#include <cassert>
#endif
#include <set>
#include <map>
#include <typeinfo>

namespace dtn
{
    namespace security
    {
        class StrictSerializer;
        class MutualSerializer;
    }

    namespace data
    {
        class CustodySignalBlock;
        class StatusReportBlock;

        class Bundle : public PrimaryBlock
        {
            friend class DefaultSerializer;
            friend class DefaultDeserializer;
            friend class dtn::security::StrictSerializer;
            friend class dtn::security::MutualSerializer;

        public:

            int getClass() const;

            class NoSuchBlockFoundException : public ibrcommon::Exception
            {
                public:
                    NoSuchBlockFoundException() : ibrcommon::Exception("No block found with this Block ID.")
                    {
                    };
            };

                        enum BUNDLE_CLASS
                        {

                        };


            class BlockList
            {
                friend class DefaultSerializer;
                friend class DefaultDeserializer;
                friend class dtn::security::StrictSerializer;
                friend class dtn::security::MutualSerializer;

            public:
                BlockList();
                virtual ~BlockList();

                BlockList& operator=(const BlockList &ref);

                void push_front(Block *block);
                void push_back(Block *block);
                void insert(Block *block, const Block *before);
                void remove(const Block *block);
                void clear();

                const std::set<dtn::data::EID> getEIDs() const;

                Block& get(int index);
                const Block& get(int index) const;
                template<class T> T& get();
                template<class T> const T& get() const;

                template<class T>
                const std::list<const T*> getList() const;

                const std::list<const Block*> getList() const;

                size_t size() const;

            private:
                std::list<refcnt_ptr<Block> > _blocks;
            };

            Bundle();
            virtual ~Bundle();

            bool operator==(const Bundle& other) const;
            bool operator!=(const Bundle& other) const;
            bool operator<(const Bundle& other) const;
            bool operator>(const Bundle& other) const;

            const std::list<const dtn::data::Block*> getBlocks() const;

            dtn::data::Block& getBlock(int index);
            const dtn::data::Block& getBlock(int index) const;

            template<class T>
            T& getBlock();

            template<class T>
            const T& getBlock() const;

            template<class T>
            const std::list<const T*> getBlocks() const;

            template<class T>
            T& push_front();

            template<class T>
            T& push_back();

            template<class T>
            T& insert(const dtn::data::Block &before);

            dtn::data::PayloadBlock& push_front(ibrcommon::BLOB::Reference &ref);
            dtn::data::PayloadBlock& push_back(ibrcommon::BLOB::Reference &ref);
            dtn::data::PayloadBlock& insert(const dtn::data::Block &before, ibrcommon::BLOB::Reference &ref);

            dtn::data::Block& push_front(dtn::data::ExtensionBlock::Factory &factory);
            dtn::data::Block& push_back(dtn::data::ExtensionBlock::Factory &factory);
            dtn::data::Block& insert(dtn::data::ExtensionBlock::Factory &factory, const dtn::data::Block &before);

            void remove(const dtn::data::Block &block);
            void clearBlocks();

            string toString() const;

            size_t blockCount() const;

        private:
            BlockList _blocks;
        };

        template<class T>
        const std::list<const T*> Bundle::getBlocks() const
        {
            return _blocks.getList<T>();
        }

        template<class T>
        T& Bundle::getBlock()
        {
            return _blocks.get<T>();
        }

        template<class T>
        const T& Bundle::getBlock() const
        {
            return _blocks.get<T>();
        }

        template<>
        CustodySignalBlock& Bundle::BlockList::get<CustodySignalBlock>();

        template<>
        const CustodySignalBlock& Bundle::BlockList::get<const CustodySignalBlock>() const;

        template<>
        StatusReportBlock& Bundle::BlockList::get<StatusReportBlock> ();

        template<>
        const StatusReportBlock& Bundle::BlockList::get<const StatusReportBlock>() const;

        template<class T>
        const T& Bundle::BlockList::get() const
        {
            try {
                // copy all blocks to the list
                for (std::list<refcnt_ptr<Block> >::const_iterator iter = _blocks.begin(); iter != _blocks.end(); iter++)
                {
                    if ((*iter)->getType() == T::BLOCK_TYPE)
                    {
                        const Block *b = (*iter).getPointer();
                        return dynamic_cast<const T&>(*b);
                    }
                }
            } catch (const std::bad_cast&) {

            }

            throw NoSuchBlockFoundException();
        }

        template<class T>
        T& Bundle::BlockList::get()
        {
            try {
                // copy all blocks to the list
                for (std::list<refcnt_ptr<Block> >::iterator iter = _blocks.begin(); iter != _blocks.end(); iter++)
                {
                    if ((*iter)->getType() == T::BLOCK_TYPE)
                    {
                        Block *b = (*iter).getPointer();
                        return dynamic_cast<T&>(*b);
                    }
                }
            } catch (const std::bad_cast&) {

            }

            throw NoSuchBlockFoundException();
        }

        template<class T>
        const std::list<const T*> Bundle::BlockList::getList() const
        {
            // create a list of blocks
            std::list<const T*> ret;

            // copy all blocks to the list
            for (std::list<refcnt_ptr<Block> >::const_iterator iter = _blocks.begin(); iter != _blocks.end(); iter++)
            {
                if ((*(*iter)).getType() == T::BLOCK_TYPE)
                {
                    const T* obj = dynamic_cast<const T*>((*iter).getPointer());

                    if (obj != NULL)
                    {
                        ret.push_back( obj );
                    }
                }
            }

            return ret;
        }

        template<class T>
        T& Bundle::push_front()
        {
            T *tmpblock = new T();
            dtn::data::Block *block = dynamic_cast<dtn::data::Block*>(tmpblock);

#ifdef __DEVELOPMENT_ASSERTIONS__
            assert(block != NULL);
#endif

            _blocks.push_front(block);
            return (*tmpblock);
        }

        template<class T>
        T& Bundle::push_back()
        {
            T *tmpblock = new T();
            dtn::data::Block *block = dynamic_cast<dtn::data::Block*>(tmpblock);

#ifdef __DEVELOPMENT_ASSERTIONS__
            assert(block != NULL);
#endif

            _blocks.push_back(block);
            return (*tmpblock);
        }

        template<class T>
        T& Bundle::insert(const dtn::data::Block &before)
        {
            T *tmpblock = new T();
            dtn::data::Block *block = dynamic_cast<dtn::data::Block*>(tmpblock);

#ifdef __DEVELOPMENT_ASSERTIONS__
            assert(block != NULL);
#endif

            _blocks.insert(block, &before);
            return (*tmpblock);
        }
    }
}

#endif /* BUNDLE_H_ */

2 个答案:

答案 0 :(得分:1)

MemoryBundleStorage.cpp 是否包含 Bundle.h ?如果要调用成员函数,则前向声明是不够的。

答案 1 :(得分:0)

在这种情况下,首先要做的是编译请求预处理器输出。然后在该文件中查找问题,并将其追溯回原始源。常见原因包括从其他目录中选取的宏或包含文件。