[英]Using STL Data Structures
請幫忙:
g ++(GCC)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 <uint32_t>',但這會產生以下錯誤:
./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 ...;
在頭文件中,因為您強制包含頭文件的每個人。 始終在頭文件中使用顯式名稱空間。 using ...;
保存using ...;
用於源文件。
就像已經說過的那樣,多數情況下(至少在全局名稱空間中) using
標頭是不好的樣式。
或者,消除歧義:
::set<uint32_t>* path;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.