所有信息都在标题文件中时未定义的参考函数

Vla*_*nus 1 c++ class undefined-reference

当我尝试编译以下代码时,我收到此错误:

obj\Debug\main.o||In function `main':|
C:\Users\dmarr\Documents\CSharper\Dallas\CPlusPlus\WHYGODWHYCB\main.cpp|16|undefined reference to `bag::bag()'|
||=== Build finished: 1 errors, 0 warnings ===|
Run Code Online (Sandbox Code Playgroud)

起初,我将bag.h和bag.cpp作为两个单独的文件 - 许多与"Undefined Reference"相关的答案建议将.cpp文件的内容移动到.h文件中 - 所以我做了.但是,编译时我仍然遇到上述错误.

aItem.cpp:

//aItem .cpp implementation file
#include "aItem.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

//setting this up default
aItem::aItem()
{
    m_itemName = "Default Name";
    m_itemType = "Default Type";
    m_damage     = 9001;
}





void aItem::setName(string name)
{
    m_itemName = name;
}

void aItem::setType(string type)
{
    m_itemType = type;
}

void aItem::setDamage(int damage)
{
    m_damage = damage;
}

string aItem::getName()
{
    return m_itemName;
}

string aItem::getType()
{
    return m_itemType;
}

int aItem::getDamage()
{
    return m_damage;
}
Run Code Online (Sandbox Code Playgroud)

aItem.h:

#ifndef AITEM_H_INCLUDED
#define AITEM_H_INCLUDED

#include <iostream>
#include <string>
#include <vector>
using namespace std;


class aItem
{
public:
    //constructor
    aItem();


    //Methods
    void ItemCreate(string itemName, string itemType, int damage);
    void setName(string name);
    void setType(string type);
    void setDamage(int damage);
    string getName();
    string getType();
    int getDamage();

private:

    string  m_itemName;
    string  m_itemType;
    int m_damage;


};

#endif // AITEM_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)

bag.h:

#ifndef BAG_H_INCLUDED
#define BAG_H_INCLUDED

#include "aItem.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class aItem;

class bag : public aItem
{
public:
    //constructor
    bag();

    //methods
    //void delItem(aItem aitem);

void addItem(aItem var)
    {
        m_items.push_back(var);
    }

void bagList()
    {
        for( vector<aItem>::size_type index = 0; index < m_items.size(); index++ )
            {
                //Makes a numerical list.
                cout << "Item " << index + 1 << ": " <<  m_items[index].m_itemName << endl;
                index+= 1;
            }
    }
private:
     vector<aItem> m_items;
};

#endif // BAG_H_INCLUDED
Run Code Online (Sandbox Code Playgroud)

......最后,main.cpp:

// WHYGODWHY.cpp : Defines the entry point for the console application.
//

#include "aItem.h"
#include "bag.h"
#include <iostream>
using namespace std;

int main()
{
    aItem woodSword;
    woodSword.setName("Wooden Sword");
    woodSword.setDamage(3);
    woodSword.setType("WPN");

    bag inventory;
    inventory.addItem(woodSword);
    inventory.bagList();


    cout << "Name: " << woodSword.getName() << endl;
    cout << "Type: " << woodSword.getType() << endl;
    cout << "Damage: " << woodSword.getDamage() << endl;
    //cout << "Inv: " <<
    int pause = 0;
    cout << "Pause!";
    cin >> pause;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Joh*_* S. 5

//constructor
bag();
Run Code Online (Sandbox Code Playgroud)

您缺少构造函数的定义.如果您不声明,编译器将仅自动生成它.一旦您这样做,您有责任提供实施.

注意:如果你这样做,你当然可以回去分离.h,并.cpp为好.