c++ - 使用 STL 数据结构

标签 c++ stl g++

请帮忙:

g++ (海湾合作委员会) 3.4.4

我有两个“.hpp”文件:“UnionFind.hpp”和“Graph.hpp”。文件内容如下:

#ifndef UNIONFIND_HPP
#define UNIONFIND_HPP

#include <vector>

using std::vector;

class UnionFind
{
   public:
      UnionFind(uint32_t size);
      ~UnionFind();
      int find(uint32_t target);
      void join(uint32_t a, uint32_t b);
      void print();
   private:
      uint32_t size;
      uint32_t* index;
      vector<uint32_t>** sets;
};

#endif

还有一个:

#ifndef GRAPH_HPP
#define GRAPH_HPP

#include <set>

using std::set;

class Graph
{
   public:
      Graph(uint32_t width, uint32_t length, uint32_t startN, uint32_t startP, uint32_t endN, uint32_t endP);
      ~Graph();
      int cost(uint32_t a, uint32_t b);
      void set(uint32_t a, uint32_t b, uint32_t cost);
      void print();
      bool inPath(uint32_t node);
   private:
      int32_t** adjList;
      uint32_t startN;
      uint32_t startP;
      uint32_t endN;
      uint32_t endP;
      set<uint32_t>* path;
      const uint32_t width;
      const uint32_t length;
      const uint32_t size;
      const uint32_t listWidth;
};

#endif

出于某种原因,我收到以下错误:

./Graph.hpp:23: error: ISO C++ forbids declaration of `set' with no type
./Graph.hpp:23: error: expected `;' before '<' token

我之前遇到过在“UnionFind.hpp”中不包含“using std::vector”的问题,但将“using std::set”添加到“Graph.hpp”并不能解决问题。还, 我试过“使用 std::set”,但这会出现以下错误:

./Graph.hpp:6: error: a template-id may not appear in a using-declaration
./Graph.hpp:23: error: ISO C++ forbids declaration of `set' with no type
./Graph.hpp:23: error: expected `;' before '<' token

最佳答案

改变

set<uint32_t>* path;

std::set<uint32_t>* path;

编译器将 set 理解为您在类中声明的 set() 方法。

using ...; 放在头文件中是一种糟糕的风格,因为您将它强加给包含您的头文件的每个人。始终在头文件中使用显式命名空间。为源文件保存 using ...;

关于c++ - 使用 STL 数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13425656/

相关文章:

C++ 字符串覆盖链接错误

c++ - 转换为 clang 格式的 C++17 嵌套命名空间?

c++ - 转换字符串 vector : skip elements based on the previous value

c++ - 共享库中的外国库

gcc - 我可以确定用于创建仅二进制共享库的编译器/链接器标志吗?

c++ - 在构造函数中复制 __m256d 会导致段错误

c++ - 删除元素时使用 STL 映射的迭代器时遇到问题

c++ - 将宏设置为值,如果为空

c++ - next_permutation 返回奇怪的结果

c++ - 在容器中存储模板化对象