为什么我无法声明向量全局变量?

Sva*_*ana 0 c++ variables vector global-variables visual-c++

在我添加之前,所有内容都在编译 vector<Move> bestLine;

search.h

#ifndef SEARCH_H
#define SEARCH_H

#include <vector>
#include "types.h"
#include "position.h"
#include "move.h"
#include "moves.h"

U8 searchDepth;
U8 searchCount;
vector<Move> bestLine; // The compiler doesn't like this.

void searchPosition(Position &P, U8 depth);
void searchMove(Move &M);

#endif
Run Code Online (Sandbox Code Playgroud)

我收到的错误是:

1>d:\test\search.h(12): error C2143: syntax error : missing ';' before '<'
1>d:\test\search.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\test\search.cpp(30): error C2065: 'bestLine' : undeclared identifier
Run Code Online (Sandbox Code Playgroud)

似乎编译器没有识别Move,所以bestLine没有被声明.我认为这可能是一个循环依赖,并尝试声明一个不完整的类型Move,但它没有任何影响.有人可以解释我错过的东西吗?

Luc*_*ore 6

尽管有必要,但实际上排位是不够的:

std::vector<Move> bestLine;
Run Code Online (Sandbox Code Playgroud)

这也构成了一个定义,如果在标题中,您可能会遇到链接器错误.

您应该声明它extern并在单个实现文件中定义它:

//search.h
//...
extern std::vector<Move> bestLine;

//search.cpp
//...
std::vector<Move> bestLine;
Run Code Online (Sandbox Code Playgroud)