Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ How to correctly split a .cpp into a .cpp and a .h
    primarykey
    data
    text
    <p>I'm really new to splitting a .cpp into a .cpp and a .h.</p> <p>I've used .h files before, but never really split a .cpp into a .cpp and a .h.</p> <p>I know .h files are only for declarations and .cpp are for definitions, and I attempted to split a .cpp into a .cpp and a .h but I get a lot of errors, so I was wondering if someone can help me with my code.</p> <p>The following class is the class that has NOT yet been split into a .cpp and a .h yet, just to show you guys the "before version".</p> <p>TicketOrder.cpp</p> <pre><code>#include &lt;iostream&gt; using namespace std; class TicketOrder { private : char type; int quantity; public : friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, TicketOrder const&amp; order) { os &lt;&lt; " Type: " &lt;&lt; order.type &lt;&lt; ", Quantity: " &lt;&lt; order.quantity; return os; } //Getters int getQuantity() const; { return quantity; } char getType() const; { return type; } //Setters void setQuantity (int x) { quantity =x; } void setType(char y) { type =y; } }; </code></pre> <p>Now, I will split that above class into a .cpp and a .h</p> <p>TicketOrder.cpp</p> <pre><code>#include &lt;iostream&gt; #include "TicketOrder.h" using namespace std; class TicketOrder { //Getters int getQuantity() const { return quantity; } char getType() const { return type; } //Setters void setQuantity (int x) { quantity =x; } void setType(char y) { type =y; } }; </code></pre> <p>TicketOrder.h</p> <pre><code>#include &lt;iostream&gt; using namespace std; class TicketOrder { private : char type; int quantity; public : friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, TicketOrder const&amp; order) { os &lt;&lt; " Type: " &lt;&lt; order.type &lt;&lt; ", Quantity: " &lt;&lt; order.quantity; return os; } //Getters int getQuantity() const; char getType() const; //Setters void setQuantity (int x); void setType(char y); }; </code></pre> <p>I have one other class that is used to contain the main class that I will not include in here because it's long and I do not think it's important because I know I'm doing the .h and .cpp wrong. </p> <p>When I try to compile the main, it gives me this error:</p> <pre><code>Undefined first referenced symbol in file TicketOrder::getQuantity() const /var/tmp//ccaSflFG.o TicketOrder::setType(char) /var/tmp//ccaSflFG.o TicketOrder::setQuantity(int) /var/tmp//ccaSflFG.o ld: fatal: Symbol referencing errors. No output written to a.out collect2: ld returned 1 exit status </code></pre> <p>Can anyone help me with splitting the .h and the .cpp? Or give me suggestions? I did a lot of research and can't figure out what's wrong.</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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