c++嵌套头文件包括编译
C++ nested header file include compile
嗨,我的主要cpp包括以下标题:
#include "Graphnode.h"
#include "move.h"
那么我的move.h使用的是Graphnode.h,所以我的move.h是这样的:
#ifndef move_H
#define move_H
#include "Graphnode.h"
//using namespace std;
class move {
public:
static Graphnode moveDown(Graphnode _node);
static Graphnode moveUp(Graphnode _node);
static Graphnode moveRight(Graphnode _node);
static Graphnode moveLeft(Graphnode _node);
};
#endif
这是我的move.cpp文件
#include "move.h"
//1 = up,2 = left, 3 = left, 4 = right
Graphnode move::moveDown(Graphnode _node)
{
/**
*This is the function to move the blank down for 1 step to create a new state.
*/
//printf("down");
if(_node.direction == 1)
{
_node.direction = 0;
return _node;
}
char temp;
temp = _node.state[_node.x+4];
_node.state[_node.x+4]= 0;
_node.state[_node.x]= temp;
_node.x = _node.x+4;
/**
*to create the new state by exchanging the puzzle with the blank puzzle.
*/
_node.depth++;
_node.direction = 2;
/**
* to increase the depth by 1. Because we have made one move.
*/
/*
*this is used to print out the current state to make sure we have exchanged.
for (i = 0; i < 16; i++) {
if (i % 4 == 0) {
printf("n");
}
printf("%dt", _node.state[i]);
}*/
//printf("nmove downn");
return _node;
}
Graphnode move::moveUp(Graphnode _node)
{
//printf("up");
/**
*This is the function to move the blank up for 1 step to create a new state.
*/
if(_node.direction == 2)
{
_node.direction = 0;
return _node;
}
char temp;
temp = _node.state[_node.x-4];
_node.state[_node.x-4] = 0;
_node.state[_node.x]= temp;
_node.x = _node.x-4;
/**
*to create the new state by exchanging the puzzle with the blank puzzle.
*/
_node.depth++;
_node.direction = 1;
/**
* to increase the depth by 1. Because we have made one move.
*/
/*
*this is used to print out the current state to make sure we have exchanged.
for (i = 0; i < 16; i++) {
if (i % 4 == 0) {
printf("n");
}
printf("%dt", _node.state[i]);
}
printf("nmove upn");*/
return _node;
}
Graphnode move::moveRight(Graphnode _node)
{
//printf("right");
/**
*This is the function to move the blank right for 1 step to create a new state.
*/
if(_node.direction == 3)
{
_node.direction = 0;
return _node;
}
char temp;
temp = _node.state[_node.x+1];
_node.state[_node.x+1]= 0;
_node.state[_node.x]= temp;
_node.x = _node.x+1;
/**
*to create the new state by exchanging the puzzle with the blank puzzle.
*/
_node.depth++;
_node.direction = 4;
/**
* to increase the depth by 1. Because we have made one move.
*/
/*
*this is used to print out the current state to make sure we have exchanged.
for (i = 0; i < 16; i++) {
if (i % 4 == 0) {
printf("n");
}
printf("%dt", _node.state[i]);
}
printf("nmove rightn");
*/
return _node;
}
Graphnode move::moveLeft(Graphnode _node)
{
//printf("left");
/**
*This is the function to move the blank left for 1 step to create a new state.
*/
if(_node.direction == 4)
{
_node.direction = 0;
return _node;
}
char temp;
temp = _node.state[_node.x-1];
_node.state[_node.x-1] = 0;
_node.state[_node.x] = temp;
_node.x = _node.x-1;
/**
*to create the new state by exchanging the puzzle with the blank puzzle.
*/
_node.depth++;
_node.direction = 3;
/**
* to increase the depth by 1. Because we have made one move.
*/
/*
*this is used to print out the current state to make sure we have exchanged.
for (i = 0; i < 16; i++) {
if (i % 4 == 0) {
printf("n");
}
printf("%dt", _node.state[i]);
}
printf("nmove leftn");*/
return _node;
}
但是当我编译。我认为move.cpp不构建…
mpic++ -o local ods_v3.cpp Graphnode.cpp move.cpp -L/opt/local/lib/ -lboost_iostreams-mt -lz -I/opt/local/include
Undefined symbols for architecture x86_64:
"move::moveUp(Graphnode)", referenced from:
bfs(Graphnode, std::tr1::array<char, 16ul>, char, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >&) in ods_v3-wnv7P8.o
dfs(Graphnode, std::tr1::array<char, 16ul>, char, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >, std::tr1::array<std::tr1::unordered_map<std::string, char, std::tr1::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, char> >, false>, 2000ul>) in ods_v3-wnv7P8.o
"move::moveDown(Graphnode)", referenced from:
bfs(Graphnode, std::tr1::array<char, 16ul>, char, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >&) in ods_v3-wnv7P8.o
dfs(Graphnode, std::tr1::array<char, 16ul>, char, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >, std::tr1::array<std::tr1::unordered_map<std::string, char, std::tr1::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, char> >, false>, 2000ul>) in ods_v3-wnv7P8.o
"move::moveLeft(Graphnode)", referenced from:
bfs(Graphnode, std::tr1::array<char, 16ul>, char, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >&) in ods_v3-wnv7P8.o
dfs(Graphnode, std::tr1::array<char, 16ul>, char, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >, std::tr1::array<std::tr1::unordered_map<std::string, char, std::tr1::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, char> >, false>, 2000ul>) in ods_v3-wnv7P8.o
"move::moveRight(Graphnode)", referenced from:
bfs(Graphnode, std::tr1::array<char, 16ul>, char, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >&) in ods_v3-wnv7P8.o
dfs(Graphnode, std::tr1::array<char, 16ul>, char, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >, std::queue<Graphnode, std::deque<Graphnode, std::allocator<Graphnode> > >, std::tr1::array<std::tr1::unordered_map<std::string, char, std::tr1::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, char> >, false>, 2000ul>) in ods_v3-wnv7P8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [local] Error 1
这些静态函数是在move class
范围内声明的,你需要在move.cpp
中实现这些函数,像这样:
Graphnode move::moveUp(Graphnode _node)
{
/// do something
}
建议:
由于move class
只包含静态函数,更实用的方法是将move
声明为命名空间而不是具有静态成员的类:
move.h
namespace move
{
Graphnode moveDown(Graphnode node);
} // namespace move
move.cpp
namespace move
{
Graphnode moveDown(Graphnode node)
{
/// do something
}
}
另外,不要让变量名以下划线开头,c++库是这样声明变量的
相关文章:
- 使用 MEX 编译C++文件
- 获取 clang++:错误:在编译文件系统库的代码时
- 如何交叉编译C++文件
- 使用 MinGW 在 Windows 上的命令行编译C++文件
- 选择特定版本的 Visual Studio 命令行工具包,并根据特定版本的C++运行时环境编译文件
- 解析模板和范围后获取编译文件?
- wcslen() 在使用编译指示包时返回不正确的结果
- 没有相应 POP 的编译指示包(推送)会导致堆栈粉碎
- 我的 PowerShell 脚本无法按预期工作(用于编译C++文件)
- 如何在不制作Visual Studio 2017项目的情况下编译C++文件?
- 'void *' 'void (*) (bool)' 指针使用 c ++ 编译器在 C 中编译文件的错误
- 如何使用 PIP 交叉编译 Python 包
- 在没有节点GYP的目标平台上编译C/C 文件
- 生成文件似乎以错误的顺序编译文件
- 我怎样才能找到已编译的包
- 无法编译 R 包
- 在编译指示包中包含标准地图会导致崩溃
- 编译 C++ 文件失败,因为找不到"ssize_t"的定义
- NeatBeans Makefile:麻烦编译.H文件
- 我无法编译文件系统库