Note that there are some explanatory texts on larger screens.

plurals
  1. POUndefined Reference To Function When All Info Is In The Header File
    primarykey
    data
    text
    <p>When I attempt to compile the following code, I get this error:</p> <pre><code>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 ===| </code></pre> <p>At first, I had bag.h and bag.cpp as two separate files- many of the answers relating to "Undefined Reference" here suggested to move the contents of the .cpp file into the .h file- so I did. However, I am still getting the aforementioned error when compiling.</p> <p>aItem.cpp:</p> <pre><code>//aItem .cpp implementation file #include "aItem.h" #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; 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; } </code></pre> <p>aItem.h:</p> <pre><code>#ifndef AITEM_H_INCLUDED #define AITEM_H_INCLUDED #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; 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 </code></pre> <p>bag.h:</p> <pre><code>#ifndef BAG_H_INCLUDED #define BAG_H_INCLUDED #include "aItem.h" #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; 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&lt;aItem&gt;::size_type index = 0; index &lt; m_items.size(); index++ ) { //Makes a numerical list. cout &lt;&lt; "Item " &lt;&lt; index + 1 &lt;&lt; ": " &lt;&lt; m_items[index].m_itemName &lt;&lt; endl; index+= 1; } } private: vector&lt;aItem&gt; m_items; }; #endif // BAG_H_INCLUDED </code></pre> <p>...and finally, main.cpp:</p> <pre><code>// WHYGODWHY.cpp : Defines the entry point for the console application. // #include "aItem.h" #include "bag.h" #include &lt;iostream&gt; 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 &lt;&lt; "Name: " &lt;&lt; woodSword.getName() &lt;&lt; endl; cout &lt;&lt; "Type: " &lt;&lt; woodSword.getType() &lt;&lt; endl; cout &lt;&lt; "Damage: " &lt;&lt; woodSword.getDamage() &lt;&lt; endl; //cout &lt;&lt; "Inv: " &lt;&lt; int pause = 0; cout &lt;&lt; "Pause!"; cin &gt;&gt; pause; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload