gpt4 book ai didi

C++如何修复多声明编译器错误

转载 作者:搜寻专家 更新时间:2023-10-31 00:30:02 25 4
gpt4 key购买 nike

我有 2 个标题和 1 个 cpp 文件。 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 给我错误:错误 C2874:使用声明导致“storage::Block”的多重声明

是否可以在不移动 using 指令并将“Block.h”包含到 header 中的情况下避免此错误?

最佳答案

问题是您声明了两个“ block ”。一个在命名空间“存储”内,另一个在全局命名空间中。试试这个:

namespace storage {
class Block; // <<< Forward declaration of Block
}
class PerformanceWriteTest {
vector<storage::Block*> blocks_;
public:

关于C++如何修复多声明编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38513815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com