C++ C+;中用户定义构造函数的问题+;

C++ C+;中用户定义构造函数的问题+;,c++,class,object,C++,Class,Object,我对我定义的类构造函数有问题。我的.cpp和.h是自己编译的,但一旦我试图在main中使用它们并构造一个类,它就会抛出错误 我读过关于不正确定义用户创建的构造函数可能会导致相同的错误,但我确信一切都是正确的 格斗 #include "joust.h" 2 //create class construct for knight class 3 //set his name stam and put him on a horse 4 knight::knight (string n) :

我对我定义的类构造函数有问题。我的.cpp和.h是自己编译的,但一旦我试图在main中使用它们并构造一个类,它就会抛出错误

我读过关于不正确定义用户创建的构造函数可能会导致相同的错误,但我确信一切都是正确的

格斗

#include "joust.h"
  2 //create class construct for knight class
  3 //set his name stam and put him on a horse
  4 knight::knight (string n) :  equipped_wep(5,5,"Basic Blade"){
  5   name = n;
  6   stamina = 100;
  7   mounted = true;
  8 }
  9 
 10 void knight::show_stats() {
 11   if (stamina) {
 12     cout << name << " is not exhausted (stamina="<< stamina<< ") and is moun    ted" << endl;
 13   } else {
 14       cout << name <<  " has passed out from exhaustion" << endl;
 15   }
 16 }
 17 void knight::show_wep() {
 18   cout << name << " is using " << equipped_wep.display_wep() << endl;
 19 }
 20 
 21 void knight::equip_weapon(weapon wep) {
 22   equipped_wep = wep;
生成文件

 test: test.o joust.o
  2         g++ -std=c++11 joust.o test.o -o test
  3 test.o: test.cpp joust.h
  4         g++ -std=c++11 test.cpp
  5 joust.o: joust.cpp joust.h
  6         g++ -std=c++11 -c joust.cpp
  7 clean:
  8         rm -f joust.o                                                                               


它应该只是创建一个武器对象,而不是抛出这个错误

make
g++ -std=c++11 test.cpp
Undefined symbols for architecture x86_64:
  "weapon::weapon(float, float, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in test-501f47.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: *** [test.o] Error 1
make
g++-std=c++11 test.cpp
架构x86_64的未定义符号:
“武器::武器(浮动,浮动,标准::_1::基本字符串)”,引用自:
_测试-501f47.o中的干管
ld:找不到架构x86_64的符号
叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用)
make:**[test.o]错误1
您有两个问题:

  • 首先,您需要一起构建所有源文件,或者从每个源文件创建对象文件,然后链接到一起

  • 第二个错误是没有在任何地方定义(实现)
    武器
    构造函数

  • 第一个问题通过在构建源文件时添加
    -c
    标志来解决。
    -c
    标志告诉GCC创建对象文件(文件以
    .o
    结尾)。您在构建
    joust.cpp
    源文件时拥有它,但在
    test.cpp
    源文件中似乎忘记了它


    第二个问题应该很明显如何解决:实现构造函数功能。

    为什么编译
    测试时不使用
    -c
    ?请不要在测试中包含行号,一个很好的例子应该是复制粘贴,以便我们在不需要修改的情况下尝试复制您的问题。如果你需要标出一个特定的行,那么在该行添加注释。旁注:对于这个ctor
    武器(float=1,float=1,string=“base”)给参数命名,可能不同于类成员
    武器(float effect=1,float wgh=1,string theName=“base”):当编写调用该ctor/方法的代码时,它将简化您的生活。对成员变量使用一些合适的命名约定,常见的选择是使用尾随符号,例如
    weight
     test: test.o joust.o
      2         g++ -std=c++11 joust.o test.o -o test
      3 test.o: test.cpp joust.h
      4         g++ -std=c++11 test.cpp
      5 joust.o: joust.cpp joust.h
      6         g++ -std=c++11 -c joust.cpp
      7 clean:
      8         rm -f joust.o                                                                               
    
    
    
    make
    g++ -std=c++11 test.cpp
    Undefined symbols for architecture x86_64:
      "weapon::weapon(float, float, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
          _main in test-501f47.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: *** [test.o] Error 1