未定义的非成员函数引用 - C++

Nav*_*K N 6 c++ gcc linker-errors

我在头文件中有以下内容.

namespace silc{
   class pattern_token_map
   {
      /* Contents */
   };

   pattern_token_map* load_from_file(const char*);
}
Run Code Online (Sandbox Code Playgroud)

在CPP文件中(这有适当的包含)

pattern_token_map* load_from_file(const char* filename)
{
   // Implementation goes here
}
Run Code Online (Sandbox Code Playgroud)

在另一个CPP文件中.这有所有适当的包括.

void some_method()
{
    const char* filename = "sample.xml";
    pattern_token_map* map = load_from_file( filename ); // Linker complains about this.
}
Run Code Online (Sandbox Code Playgroud)

我收到一个链接器错误,说明未定义的引用load_from_file.我无法看到这里出了什么问题.

任何帮助,将不胜感激.

编译器:G ++ OS:Ubuntu 9.10

编辑

这是使用的链接器命令.

g++ -L/home/nkn/silc-project/third_party/UnitTest++ -o tests.out  src/phonetic_kit/pattern_token_map.o  tests/pattern_token_map_tests.o  tests/main.o -lUnitTest++
Run Code Online (Sandbox Code Playgroud)

错误来自pattern_token_map_tests.o,功能可用pattern_token_map.o.所以我猜连接的顺序并不是问题所在.(我已从命令中删除了一些文件以简化它)

小智 9

When you implement it, you have to make sure you implement the right function:

namespace silc {
pattern_token_map* load_from_file(const char* filename) {
   // Implementation goes here
}
}
Run Code Online (Sandbox Code Playgroud)

If you instead did this:

using namespace silc; // to get pattern_token_map
pattern_token_map* load_from_file(const char* filename) {
   // Implementation goes here
}
Run Code Online (Sandbox Code Playgroud)

Then you'd be defining a new function rather than silc::load_from_file.

Avoid using directives ("using namespace ...;") outside of function scope, as a general guideline:

using namespace silc; // outside function scope: avoid

silc::pattern_token_map*                      // qualify return type
random_function(silc::pattern_token_map* p) { // and parameters
  using namespace silc; // inside function scope: fine
  pattern_token_map* p2 = 0; // don't have to qualify inside the function
                             // if you want to use the using directive
  silc::pattern_token_map* p3 = 0; // but you can always do this
  return 0;
}
Run Code Online (Sandbox Code Playgroud)