简体   繁体   English

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

[英]C ++ error: class has no member named

I have this issue 我有这个问题

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'

when compiling this function in Eclipse 在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
}

Partial Bundle.h file 部分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;

...
}

And the implementation of the method getClass() is the following (Bundle.cpp) 而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
                }

}

What am I doing wrong? 我究竟做错了什么? Maybe the calling is not well done? 也许打电话做得不好?

Thanks! 谢谢!

Edit1: complete bundle.h 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_ */

Does MemoryBundleStorage.cpp include Bundle.h ? MemoryBundleStorage.cpp是否包含Bundle.h Forward declaration is not enough if you want to call member functions. 如果要调用成员函数,则前向声明是不够的。

In cases like that first thing to do is compile asking for the preprocessor output. 在这种情况下,首先要做的是编译请求预处理器输出。 Then look in that file for the problem, and trace it back to original source. 然后在该文件中查找问题,并将其追溯回原始源。 Common causes include macros or include files picked from other directory. 常见原因包括从其他目录中挑选的宏或包含文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM