Edit : I am not looking for clarifications on the Singleton pattern.
I am trying to use a singleton class by defining a macro of it in the header file of the class itself. Experiencing an error. I have no idea whether what I am doing has some syntactical mistakes or it's just not possible in C++
Here's what I have :
Header File:
#define ALERT(c) A::inst().write(c)
class A
{
public:
static A& inst();
void write(int c);
private:
A();
int add(int a,int b);
};
.cpp file
#include<iostream>
#include "a.h"
#include <fstream>
A& A::inst()
{
static A a;
return a;
}
int A::add(int a,int b)
{
return a+b;
}
void A::write(int c)
{
std::ofstream ofs ("test.txt",std::ofstream::out);
int d = add(c,c);
ofs << c << d;
ofs.close();
}
The error that I experience:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-
gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
/usr/bin/ld: /tmp/ccWAoPQ2.o: in function `A::inst()':
a.cpp:(.text+0x44): undefined reference to `A::A()'
collect2: error: ld returned 1 exit status
Attempt at using the class/macro:
#include<iostream>
#include "a.h"
int main()
{
ALERT(6);
return 0;
}
Error upon trying to use the class/macro:
/usr/bin/ld: /tmp/cc9BpGLW.o: in function `A::inst()':
a.cpp:(.text+0x44): undefined reference to `A::A()'
collect2: error: ld returned 1 exit status