无法调用非静态成员,加上static后又说无法识别

flames60 2016-12-16 08:30:10

template<class T>
class Graph {
public:
int vertexNum;
int edgeNum;
int *Mark; //就是这里!****!!*******!!!
Graph(int verticesNum) {
vertexNum = verticesNum;
edgeNum = 0;
Mark = new int[vertexNum];
for (int i = 0; i < vertexNum; i++) {
Mark[i] = UNVISITED;
}
}
~Graph() {
delete[] Mark;
//delete[] Indegree;
}
virtual Edge<T> FirstEdge(int oneEdge) = 0;
virtual Edge<T> NextEdge(Edge<T> oneEdge) = 0;
static int VerticesNum() { return vertexNum; }
static int EdgesNum() { return edgeNum; }
bool IsEdge(Edge<T> oneEdge) {
if (oneEdge.weight > 0 && oneEdge.weight < INFINITY&&oneEdge.end >= 0)
return true;
else
return false;
}
static void visit(int v) { Mark[v] = VISITED; }
static int StartVertex(Edge<T> oneEdge) { return oneEdge.start; }
static int EndVertex(Edge<T> oneEdge) { return oneEdge.end; }
T Weight(Edge<T> oneEdge) { return oneEdge.weight; }
virtual void setEdge(int start, int end, int weight) = 0;
virtual void delEdge(int start, int end) = 0;
void DFS(int v);
void DFSTraverse();
void BFS(int v);
void BFSTraverse();

};
template<class T>
void Graph<T>::DFS(int v) {
Mark[v] = VISITED;
visit(v);
for (Edge<T> e = FirstEdge(v); IsEdge(e); e = NextEdge(e)) {
if (Mark[EndVertex(e)] == UNVISITED)
DFS(EndVertex(e));
}
}

——代码是没加static的,错误是这样的:
严重性 代码 说明
错误 C2597 对非静态成员“Graph<T>::Mark”的非法引用
——在int *Mark;前加上static后:
严重性 代码 说明
错误 LNK2001 无法解析的外部符号 "public: static int * Graph<int>::Mark" (?Mark@?$Graph@H@@2PAHA)
...全文
178 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
AlbertS 2016-12-19
  • 打赏
  • 举报
回复
引用 5 楼 GKatHere 的回复:
[quote=引用 4 楼 flames60 的回复:] [quote=引用 3 楼 lunat 的回复:]

static void visit(int v) { Mark[v] = VISITED; }
改成:

void visit(int v) { Mark[v] = VISITED; }
。。。能说说为什么吗 (握草,为什么这么吊。。[/quote] 静态成员函数不能调用非静态成员 变量或函数 呵呵,非静态成员全可[/quote] 赞一个
GKatHere 2016-12-16
  • 打赏
  • 举报
回复
引用 4 楼 flames60 的回复:
[quote=引用 3 楼 lunat 的回复:]

static void visit(int v) { Mark[v] = VISITED; }
改成:

void visit(int v) { Mark[v] = VISITED; }
。。。能说说为什么吗 (握草,为什么这么吊。。[/quote] 静态成员函数不能调用非静态成员 变量或函数 呵呵,非静态成员全可
flames60 2016-12-16
  • 打赏
  • 举报
回复
引用 3 楼 lunat 的回复:

static void visit(int v) { Mark[v] = VISITED; }
改成:

void visit(int v) { Mark[v] = VISITED; }
。。。能说说为什么吗 (握草,为什么这么吊。。
lunat 2016-12-16
  • 打赏
  • 举报
回复

static void visit(int v) { Mark[v] = VISITED; }
改成:

void visit(int v) { Mark[v] = VISITED; }
ipqtjmqj 2016-12-16
  • 打赏
  • 举报
回复
静态函数不能引用非静态数据
ipqtjmqj 2016-12-16
  • 打赏
  • 举报
回复
静态成员需要在类外初始化

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧