Note that there are some explanatory texts on larger screens.

plurals
  1. POI'm in trouble with creating new object in C++
    primarykey
    data
    text
    <p>I'm new to C++ and I'm having a trouble with it. I've tried to create new object without (*), so <code>Animal a1;</code> but it didn't work. So now I'm using <code>Animal* a1;</code>, and it works, but the problem is that it says it has to be initialised. I made a default constructor, so doesn't it mean that it initialises automatically or should I initialise it again? These are my code:</p> <h2><code>Animal.cpp</code>:</h2> <pre><code>#include "Animal.h" using namespace std; Animal::Animal(int newid, double newweight, int yy, int mm, int dd, double newaccDose, char newsex) { id = newid; weight = newweight; yy = yy; mm = mm; dd = dd; dose = newaccDose; sex = newsex; } Animal::Animal() { id = 0; weight = 0; yy = 0; mm = 0; dd = 0; dose = 0; sex = ' '; } Animal::~Animal(){} double Animal::getDaysDifference(){ jdate dateOfBirth(dd,mm,yy); jdate now; double diff = now-dateOfBirth; return diff; } void Animal::addAnimal(){ int select=0; while(select==1 || select==2){ cout &lt;&lt; "1. Cattle 2. Sheep" &lt;&lt; endl; cout &lt;&lt; "Select a type of animal: "; cin &gt;&gt; select; cout &lt;&lt; "Invalid number please try again" &lt;&lt; endl; } } </code></pre> <h2><code>Animal.h</code></h2> <pre><code>#ifndef ANI_H #define ANI_H #include &lt;vector&gt; #include "Treatment.h" #include "jdate.h" class Animal{ protected: int id; double weight; int yy; int mm; int dd; double dose; char sex; //Treatment treatArray[]; public: Animal(); Animal(int newid, double newweight, int yy, int mm, int dd, double newdose, char newsex); ~Animal(); virtual double calcDose() = 0; void addAnimal(); void questions(); // ask questions like id,weight, and so on double getDaysDifference(); }; #endif </code></pre> <h2><code>DrugAdmin.cpp</code></h2> <pre><code>//Main function #include &lt;iostream&gt; #include "Animal.h" #include "Cattle.h" using namespace std; int main(){ Animal* a1; a1-&gt;addAnimal(); } </code></pre> <p>I just did it to check if the <code>addAnimal()</code> function is working but apparently it's not. Can you please give me a piece of advice? </p> <p>Cheers</p>
    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.
    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