c++嵌套头文件包括编译

C++ nested header file include compile

本文关键字:编译 文件包 嵌套 c++      更新时间:2023-10-16

嗨,我的主要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++库是这样声明变量的