jeudi 21 juillet 2016

C++ How to fix multiple declaration compiler error

I have 2 headers and one cpp file. Block.h:

    #ifndef BLOCK_H
    #define BLOCK_H

    namespace storage {

    class Block {
    };

    } // namespace storage

    #endif // BLOCK_H

PerformanceWriteTest.h

    #ifndef _PERFORMANCE_WRITE_TEST_
    #define _PERFORMANCE_WRITE_TEST_

    #include <string>
    #include <vector>

    using std::vector;

    class Block;  // <<< Forward declaration of Block

    class PerformanceWriteTest {
      vector<Block*> blocks_;
    public:
      virtual ~PerformanceWriteTest();
    };

    #endif

PerformanceWriteTest.cpp

    #include "Block.h"
    #include "PerformanceWriteTest.h"

    using storage::Block; // <<< Use the scope storage::Block. Error!

    PerformanceWriteTest::~PerformanceWriteTest() {
      for (Block* block : blocks_) {
        delete block;
      }
    }

Visual Studio 2012 gives me the error: error C2874: using-declaration causes a multiple declaration of 'storage::Block'

Is it possible to avoid this error without moving the using directive and include "Block.h" to the header?

Aucun commentaire:

Enregistrer un commentaire