开发者

C++ linker error

开发者 https://www.devze.com 2022-12-28 23:10 出处:网络
I have a file A.cpp which has the following lines: #include\"B.h\" int main(int argc, char **argv) { ... char *input_file = *argv;

I have a file A.cpp which has the following lines:

#include"B.h"
int main(int argc, char **argv)
{
  ...
  char *input_file = *argv;  
  B *definition = new B(input_file);
  ...
}

In B.h, I have the following:

class B
{
  public:
   // Constructors
   B(void);
   B(const c开发者_如何学编程har *filename);
   ...
}

When I compile, I get the following error: undefined reference to 'B::B(char const*)'

Any suggestions on how to fix?

Thanks.


You need a definition for B::B(char const *). You have provided only a declaration for B::B(char const *), and the Linker is complaining that it can't actually find that function.


It seems like you specified the header for the function, but never actually wrote the body of the function.

You need to define the function I.E.

B::B(const char *filename){
  // Do stuff
}

in B.cpp.

Your problem doesn't have anything to do with const correctness.

0

精彩评论

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

关注公众号