簡體   English   中英

所有信息都在頭文件中時對函數的未定義引用

[英]Undefined Reference To Function When All Info Is In The Header File

當我嘗試編譯以下代碼時,出現此錯誤:

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 ===|

最初,我將bag.h和bag.cpp作為兩個單獨的文件-許多與“未定義引用”有關的答案都建議將.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;
}

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

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

...最后,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;
}
//constructor
bag();

您缺少構造函數的定義。 如果您不聲明它,編譯器只會自動生成它。 只要你這樣做,這是你的責任,以提供一個實現。

注意:如果這樣做,您當然也可以返回分隔.h.cpp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM