开发者

Undefined reference to non-member function - C++

开发者 https://www.devze.com 2022-12-17 03:37 出处:网络
I have the following in header file. namespace silc{ class pattern_token_map { /* Contents */ }; pattern_token_map* load_from_file(const char*);

I have the following in header file.

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

   pattern_token_map* load_from_file(const char*);
}

In the CPP file (this has got proper includes)

pattern_token_map* load_from_file(const char* filename)
{
   // Implementation goes here
}

In another CPP file. This has got all proper includes.

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

I am getting a linker error saying that undefined reference to load_from_file. I am not able to see what is going wrong here.

Any help would be appreciated.

Compiler : G++ OS : Ubuntu 9.10

Edit

Here is the linker command used.

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++

Error is from pattern_token_map_tests.o and the function is available in pattern_token_map.o. So I gues开发者_C百科s the order of linking is not making the problem. (I have removed some files from the command to simplify it)


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
}
}

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
}

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;
}


In your link phase, are you linking in the object file created by compiling the first cpp file? Linker errors like these occur when one object references a symbol that is not included in the objects being linked.

Edit: I'm still reasonably confident that this is the problem. In the first file, is there a preprocessor symbol redefining load_from_file?

0

精彩评论

暂无评论...
验证码 换一张
取 消