0

嗨,我的主要 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
4

1 回答 1

2

这些静态函数是在move class范围内声明的,您需要move.cpp像这样实现这些函数:

Graphnode move::moveUp(Graphnode _node)
{
  /// do something
}

建议:

由于您move class只保留静态函数,因此更实用的方法是声明move为命名空间而不是具有静态成员的类:

移动.h

namespace move
{
   Graphnode moveDown(Graphnode node);
} // namespace move

移动.cpp

namespace move
{
  Graphnode moveDown(Graphnode node)
  {
    /// do something
  }
}

另外,不要让变量名以下划线开头,C++ 库以这种方式声明变量。

于 2013-06-17T00:59:11.273 回答