У меня проблемы с конструкторами классов, которые я определяю. мои .cpp и .h компилируются там самостоятельно, но как только я пытаюсь использовать их в main и создать класс, он выдает ошибки.
Я читал о том, что неправильное определение созданного пользователем конструктора может вызвать ту же ошибку, но я уверен, что все правильно.
joust.cpp
#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;
joust. h
13 #ifndef JOUST_H
14 #define JOUST_H
15
16 #include <iostream>
17 #include <fstream>
18 #include <vector>
19
20 using namespace std;
21
22 class weapon {
23 public:
24 weapon(float = 1, float = 1, string = "base");
25 void set(float, float);
26 string display_wep();
27 private:
28 float effectivness;
29 float weight;
30 string name;
31 };
32
33 class knight {
34 public:
35 knight( string = "base");
36 void show_stats();
37 void show_wep();
38 void equip_weapon(weapon wep);
39 private:
40 weapon equipped_wep;
41 string name;
42 int stamina;
43 bool mounted;
44 };
45
46
47 #endif
test.cpp
#include "joust.h"
2
3 int main() {
4 //knight jim("Rob the Third");
5 //jim.show_stats();
6 weapon c(15,12,"jard");
7
8 return 0;
9 }
makefile
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