0

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
harsh82
  • 13
  • 4
  • How do you build? Do you remember to build with the `a.cpp` source file (or whatever it might be called)? – Some programmer dude Sep 27 '21 at 17:08
  • 1
    Now your edit made things very different... Now do you remember to build with your main source file (where the `main` function is defined)? And where do you define (implement) the `A` constructor that you have declared? – Some programmer dude Sep 27 '21 at 17:09
  • Does this answer your question? [C++ Singleton design pattern](https://stackoverflow.com/questions/1008019/c-singleton-design-pattern) – zerocukor287 Sep 27 '21 at 17:10
  • @Someprogrammerdude I don't 'build'. I just compile with "g++ a.cpp" and that's about it. I understand that even it was correct , to use it , I would need to create another .cpp file with the main function. – harsh82 Sep 27 '21 at 17:12
  • @zerocukor287 not really. I am more interested in finding out if I can use a class's member functions by defining macros in the class itself. – harsh82 Sep 27 '21 at 17:12
  • See https://stackoverflow.com/questions/1661529/is-meyers-implementation-of-the-singleton-pattern-thread-safe – Richard Critten Sep 27 '21 at 17:13
  • @RichardCritten I am not worried about the Singleton pattern itself. Will edit the question to make it easier for people to understand my concern. – harsh82 Sep 27 '21 at 17:14
  • I have almost never used macros tbh. @Someprogrammerdude – harsh82 Sep 27 '21 at 17:15
  • Your issue is completely unrelated to macros. You don't even use the macro. From my second comment (after your first edit): Where's the `main` function and where's the `A` constructor? – Some programmer dude Sep 27 '21 at 17:17
  • The edit I made removed the attempt at using the class or the macro. I will put it there again rn . – harsh82 Sep 27 '21 at 17:18
  • The error message "undefined reference to 'main'" means there's no `main` function. You must have a `main` function, that's where execution of your program begins. – Some programmer dude Sep 27 '21 at 17:20
  • The error message "undefined reference to 'A::A()'" means you don't have the `A` constructor implemented, which is needed to be able to create object of the `A` class. – Some programmer dude Sep 27 '21 at 17:21
  • I have updated the question to show the usage. Ultimately my question is, if I created a macro that referred to one of the member functions of the class in the header file of the class itself, is that a legal thing to do ? – harsh82 Sep 27 '21 at 17:23
  • The error is *still* unrelated to your use of the macro. That you get a *linker* error means the macro compiled fine. You still need the `A` constructor. – Some programmer dude Sep 27 '21 at 17:26
  • and that's exactly what I am trying to do through the static A::inst( ) function. @Someprogrammerdude. The macro has to compile anyway . There is nothing wrong with it. But can we define a macro in the header file of a class to use the member functions ? – harsh82 Sep 27 '21 at 17:28
  • @Someprogrammerdude Thank you for your time. I will just have to find another way to do what I intend to do. – harsh82 Sep 27 '21 at 17:29
  • Solutioon: In the header file change `A();` to `A() = default;`. – Some programmer dude Sep 27 '21 at 17:31
  • You declare the constructor `A`. That also means you have to implement it. If you don't write the constructor, there is no constructor, and you can't define `A` object (which you do with `static A a;`) By saying `A() = default;` you tell the compiler to create the constructor for you (which also happens if you remove the declaration `A();` altogether). As I said, the macro is fine and you use it fine. The error is totally unrelated to the macro, and also totally unrelated to `A` being a singleton, or the `inst()` function. – Some programmer dude Sep 27 '21 at 17:32
  • Greatly appreciated @Someprogrammerdude. It works! I don't understand what I did wrong though. I will try to look up the default keyword's usage in this context unless you have a short summary of it. – harsh82 Sep 27 '21 at 17:33
  • You could also solve it by removing the `A();` declaration. Or change it to `A() {}`. Or in a single source file add `A::A() {}`. You *do* know that you must have a constructor to be able to create objects of a class? That's what the error is telling you, that you don't have such a constructor. You also need to learn about the difference between *declarations* (telling the compiler something exists somewhere) and *definition* (implementing that something). – Some programmer dude Sep 27 '21 at 17:34
  • I see. So, the fact that I wasn't going for the constructor's definition in the header file itself was hindering it. Thanks again!! – harsh82 Sep 27 '21 at 17:36

0 Answers0