Undefined reference to a function. (Using Code::Blocks if that helps anything)

Long story short, I'm making a program that's essentially a Text-Based Fire Emblem game; it runs calculations and rolls dice and has all sorts of Goodies. However, I have hit a block to the tune of
Undefined Reference
. Like a good little boy, I did a Google search to see if I could find help, but it was fruitless. This, I had no choice but to show you the relevant files:

Item.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef ITEM_H
#define ITEM_H


class Item
{
    public:
        inline Item();
        inline Item(int x);
        virtual ~Item();
        inline int getMaxDur();
        inline void setDur(int val);
        inline int getCurrDur();
        void Use();
    protected:
        int maxDur;
        int currDur;
};

#endif // ITEM_H 


Item.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Item.h"

Item::Item()
{
    currDur=maxDur=3;
}

Item::Item(int x){
    currDur=maxDur=x;
}

Item::~Item()
{
    //dtor
}
int Item::getMaxDur() {
    return maxDur;
}
void Item::setDur(int x){
    currDur=maxDur = x;
}
int Item:: getCurrDur() {
    return currDur;
}


Weapon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef WEAPON_H
#define WEAPON_H
#include <string>
#include "Item.h"
using namespace std;

class Weapon : public Item
{
      public:
             inline Weapon();
             inline Weapon(int,int,int,int,int,int,char);
             inline void setWeapon(int,int,int,int,int,int,char);
             inline int getMt();
             inline int getWt();
             inline int getHitR();
             inline int getCrit();
             inline int getType();
             inline int getTri();
             inline int getMaxDur();
             inline int getCurrDur();
             inline char getRank();
             inline string getName();
             inline void setMt(int);
             inline void setWt(int);
             inline void setHit(int);
             inline void setCrit(int);
             inline void setType(int);
             inline void setTri(int);
             inline void setRank(char);
             inline void setDur(int);
			 inline void display();
             inline void decreaseDur();
             inline void setEffective(int);
      private:
              int MT;
              int WT;
              int HITR;
              int CRIT;
              int Type;
              int EFFECTIVE;
              int Triangle;
              char Rank;
			  string Name;


      };
#endif 


Weapon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "Weapon.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

Weapon::Weapon() : Item(){
    setMt(0);
    setWt(0);
    setHit(0);
    setCrit(0);
    setTri(0);
    setDur(0);
    setRank(0);
    EFFECTIVE=0;
}
Weapon::Weapon(int M,int W,int H,int C,int T,int D,char R){
    setMt(M);
    setWt(W);
    setHit(H);
    setCrit(C);
    setTri(T);
    setDur(D);
    setRank(R);
    EFFECTIVE=0;
}
int Weapon::getMt(){
    return (MT);
}
int Weapon::getWt(){
    return (WT);
}
int Weapon::getHitR(){
    return (HITR);
}
int Weapon::getCrit(){
    return (CRIT);
}
int Weapon::getType(){
    return (Type);
}
int Weapon::getTri(){
    return (Triangle);
}
int Weapon::getMaxDur(){
    return (maxDur);
}
int Weapon::getCurrDur(){
    return (currDur);
}
char Weapon::getRank(){
    return (Rank);
}
string Weapon::getName(){
    return (Name);
}
void Weapon::setMt(int x){
    MT=x;
}
void Weapon::setWt(int x){
    WT=x;
}
void Weapon::setHit(int x){
    HITR=x;
}
void Weapon::setCrit(int x){
    CRIT=x;
}
void Weapon::setType(int x){
    Type=x;
}
void Weapon::setTri(int x){
    Triangle=x;
}
void Weapon::setDur(int x){
    Item::setDur(x);
}
void Weapon::setRank(char x){
    Rank=x;
}
void Weapon::display(){
    cout<<"[table]MGT|WGT|HIT|CRIT|Rank|Duration|Triangle|Type"<<endl<<getMt()<<"|"<<getWt()<<"|"<<getHitR()<<"|"<<getCrit()<<"|"<<getRank()<<"|"<<getCurrDur()<<"/"<<getMaxDur()<<"|"<<getTri()<<"|"<<getType()<<"[/table]"<<endl;
}
void Weapon::decreaseDur(){
    currDur-=1;
}
void Weapon::setEffective(int x){
    EFFECTIVE=x;
}


Sword.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef SWORD_H
#define SWORD_H
#include "Weapon.h"


class Sword : public Weapon{
    public:
        inline Sword();
    private:
        string Name;

};

#endif // SWORD_H_INCLUDED 


Sword.cpp
1
2
3
4
5
6
7
8
#include "Sword.h"

Sword::Sword()
{
    setTri(1);
    setType(1);
}


Battle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include "Unit.cpp"
#include "Sword.cpp"
using namespace std;

int main(){
        Unit Shannon(5,61,18,18,0,9,6,10,9,11,8,6,1,2,16);
        Shannon.display();
        Unit AxeZombie(5,0,16,16,7,0,5,6,6,7,4,8,3,1,1);
        AxeZombie.display();
        Sword IronSword;
        IronSword.display();
}


Up Until I called up a Sword object, it worked fine. But when I compiled it, I got an
Undefined Reference to Item::Item()
error in Line 8 of Weapon.cpp.

What did I do wrong?
"Undefined Reference" usually means you are calling a function but the linker can't find a body for that function. In this case... the function is Item::Item (ie: the constructor for your Item class).

Now since you clearly do have a body for that function... you might be confused by the error. But the zinger is that you are using the inline keyword for the constructor, which means it must be internally linked (ie: can't be in a separate cpp file).


So you have 2 options:

1) Move the bodies of your inline functions into the header.

or

2) Remove the inline keyword.
http://www.cplusplus.com/forum/general/113904/
If you chose `2)' make sure that you are linking the file.

You shouldn't #include *.cpp
Topic archived. No new replies allowed.