Ok, so I've tried to get more familiar with OOP in C++ and I got the following code:
//main.cpp
#include <iostream>
#include "ZooAnimal.h"
using namespace std;
int main()
{
ZooAnimal bozo;
bozo.set_name(12);
// bozo.Create("Bozo", 408, 1027, 400);
// cout << "This animal's name is " << bozo.reptName() << endl;
// bozo.Destroy();
cout << "Hello";
}
//ZooAnimal.h
#ifndef ZOOANIMAL_H
#define ZOOANIMAL_H
class ZooAnimal
{
private:
std::string name;
int cageNumber;
int weightDate;
int weight;
public:
void Create(std::string, int, int, int);
void Destroy();
std::string reptName();
int daysSinceLastWeighted(int today);
void set_name(int);
};
#endif //ZooAnimal
and lastly,
//ZooAnimal.cpp
#include "ZooAnimal.h"
#include <iostream>
void ZooAnimal::Create(std::string a, int b, int c, int d)
{
//This creates a ZooAnimal Object
name = a;
cageNumber = b;
weightDate = c;
weight = d;
}
int ZooAnimal::daysSinceLastWeighted(int today)
{
//calculate how many days have passed since last weight
return today - weightDate;
}
//clear up the memory
void ZooAnimal::Destroy()
{
delete &name;
}
// return the animal name
std::string ZooAnimal::reptName()
{
return name;
}
void ZooAnimal::set_name(int a)
{
cageNumber = a;
}
So, when I try to run this code(from main.cpp of course) it won't compile and I get the following message in the console
C:\Users\<user>\AppData\Local\Temp\ccOiWb7r.o:main.cpp:(.text+0x2e): undefined reference to `ZooAnimal::set_name(int)'
collect2.exe: error: ld returned 1 exit status
I'm using MinGW to compile, working on a Windows 10 machine. The weird thing is that when I try to run the same code on a cloud editor, like repl.it , it works just fine, and if I don't separate my code into multiple files, again, it runs fine. Any idea of what can I do?