C ++嵌套头文件包括compile

时间:2013-06-17 00:54:43

标签: c++ header

您好我的主要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("%d\t", _node.state[i]);

     }*/

    //printf("\nmove down\n");
    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("%d\t", _node.state[i]);

     }
     printf("\nmove up\n");*/

    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("%d\t", _node.state[i]);

     }
     printf("\nmove right\n");
     */

    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("%d\t", _node.state[i]);

     }
     printf("\nmove left\n");*/

    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

1 个答案:

答案 0 :(得分:2)

这些静态函数在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 ++库以这种方式声明变量。