Another problem with singleton...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Manuel

    #1

    Another problem with singleton...

    This is the singleton I'm trying to write:

    global.h
    ----------------------------------------
    class Global
    {
    int i;
    static Global glob;
    //Global(int x): i(x) { } ;
    Global();
    Global& operator=(Globa l&); // Disallowed
    Global(const Global&); // Disallowed

    //some global variables
    int myVar;

    public:
    static Global& instance() { return glob; }

    //some set variables
    void setmyVar(int);

    //some get functions
    int getmyVar();

    };
    -----------------------------------------

    global.cpp
    -----------------------------------------
    #include "global.h"

    //Set functions
    void Global::setmyVa r(int i){myVar = i;}

    //Get functions
    int Global::getmyVa r(){return myVar;}
    -----------------------------------------


    I'm trying to use the default constructor. But using it,
    with this init in application code:
    --------------------------------------------
    Global Global::glob;
    Global& GlobalVariables = Global::instanc e();
    --------------------------------------------

    The compiler return a linker error:

    [Linker error] undefined reference to `Global::Global ()'


    If I modify the global.h, using a constructor with arguments, like

    Global(int x): i(x) { } ;

    instead

    Global();

    and in application code write this init:

    Global Global::glob(1) ;
    Global& GlobalVariables = Global::instanc e();

    it work...

    Why???

    thx,

    Manuel

  • Thomas Jakob

    #2
    Re: Another problem with singleton...

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Manuel wrote:[color=blue]
    > This is the singleton I'm trying to write:
    >
    > global.h
    > ----------------------------------------
    > class Global
    > {
    > int i;
    > static Global glob;
    > //Global(int x): i(x) { } ;
    > Global();
    > Global& operator=(Globa l&); // Disallowed
    > Global(const Global&); // Disallowed
    >
    > //some global variables
    > int myVar;
    >
    > public:
    > static Global& instance() { return glob; }
    >
    > //some set variables
    > void setmyVar(int);
    >
    > //some get functions
    > int getmyVar();
    >
    > };
    > -----------------------------------------[/color]

    <snip>

    Here you declare a constructor Global::Global( ). It will be called to
    instance the static member 'glob', but you haven't defined it.



    - --
    Greetings, Thomas Jakob
    quicix (at) gmail (dot) com
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (MingW32)

    iD8DBQFDwB9jbpG yJtILqbcRArIRAJ 9d6hg96uQmwccrZ wLocCLN3/BuPgCgxl2+
    judyXBGi+qo4gKc TN/yaD88=
    =0Yj9
    -----END PGP SIGNATURE-----

    Comment

    • Manuel

      #3
      Re: Another problem with singleton...

      Thomas Jakob wrote:
      [color=blue]
      >
      > Here you declare a constructor Global::Global( ). It will be called to
      > instance the static member 'glob', but you haven't defined it.[/color]

      To make the experiment I've defined it in header file:

      ------------------------------------
      class Global
      {
      int i;
      static Global glob;
      //Global(int x): i(x) { } ;
      Global();
      Global& operator=(Globa l&); // Disallowed
      Global(const Global&); // Disallowed

      //some global variables
      int myVar;

      public:
      static Global& instance() { return glob; }

      //some set variables
      void setmyVar(int);

      //some get functions
      int getmyVar();

      };

      Global::Global( ){};
      -------------------------------------

      calling it in main code with:

      Global Global::glob;

      the compiler return various errors (global.cpp was the same as previous
      post):

      global.o(.text+ 0x0):global.cpp : multiple definition of `Global::Global ()'
      main.o(.text+0x 168):main.cpp: first defined here
      global.o(.text+ 0x6):global.cpp : multiple definition of `Global::Global ()'
      main.o(.text+0x 16e):main.cpp: first defined here
      collect2: ld returned 1 exit status

      Where is the error :-( ??

      Comment

      • Manuel

        #4
        Re: Another problem with singleton...

        Manuel wrote:
        [color=blue]
        > calling it in main code with:
        >
        > Global Global::glob;
        >
        > the compiler return various errors (global.cpp was the same as previous
        > post):
        >
        > global.o(.text+ 0x0):global.cpp : multiple definition of `Global::Global ()'
        > main.o(.text+0x 168):main.cpp: first defined here
        > global.o(.text+ 0x6):global.cpp : multiple definition of `Global::Global ()'
        > main.o(.text+0x 16e):main.cpp: first defined here
        > collect2: ld returned 1 exit status
        >
        > Where is the error :-( ??[/color]


        Please, help!!!

        Comment

        • Rennie deGraaf

          #5
          Re: Another problem with singleton...

          Manuel wrote:[color=blue]
          > Thomas Jakob wrote:
          >[color=green]
          >>
          >> Here you declare a constructor Global::Global( ). It will be called to
          >> instance the static member 'glob', but you haven't defined it.[/color]
          >
          >
          > To make the experiment I've defined it in header file:
          >
          > ------------------------------------
          > class Global
          > {
          > int i;
          > static Global glob;
          > //Global(int x): i(x) { } ;
          > Global();
          > Global& operator=(Globa l&); // Disallowed
          > Global(const Global&); // Disallowed
          >
          > //some global variables
          > int myVar;
          >
          > public:
          > static Global& instance() { return glob; }
          >
          > //some set variables
          > void setmyVar(int);
          >
          > //some get functions
          > int getmyVar();
          >
          > };
          >
          > Global::Global( ){};[/color]

          Is this line in the header file? If so, then it will be defined in
          every .cpp file that includes the header. Try putting it in a .cpp file
          instead, and see if that fixes anything.

          Rennie deGraaf

          Comment

          • Manuel

            #6
            Re: Another problem with singleton...

            Rennie deGraaf wrote:
            [color=blue][color=green]
            >>Global::Globa l(){};[/color]
            >
            >
            > Is this line in the header file? If so, then it will be defined in
            > every .cpp file that includes the header. Try putting it in a .cpp file
            > instead, and see if that fixes anything.[/color]

            It work. THANKS!

            But there is another thing I dont understand.
            If define into header is an error, why the code below work (in the
            header file)?

            Global::Global( ){};

            It's a definition, and it's into header too...

            thx,

            Manuel



            Comment

            • Manuel

              #7
              Re: Another problem with singleton...

              Manuel wrote:[color=blue]
              > Rennie deGraaf wrote:
              >[color=green][color=darkred]
              >>> Global::Global( ){};[/color]
              >>
              >>
              >>
              >> Is this line in the header file? If so, then it will be defined in
              >> every .cpp file that includes the header. Try putting it in a .cpp file
              >> instead, and see if that fixes anything.[/color]
              >
              >
              > It work. THANKS!
              >
              > But there is another thing I dont understand.
              > If define into header is an error, why the code below work (in the
              > header file)?
              >
              > Global::Global( ){};
              >
              > It's a definition, and it's into header too...
              >[/color]

              help!

              Comment

              • pavel.turbin@gmail.com

                #8
                Re: Another problem with singleton...

                I think it is because you have declared, but not defined ctor
                Global::Global( ) which is expected to create object Global
                Global::glob;

                You should define at least some constructor or remove its declaration.

                --------------
                class Global
                {
                int i;
                static Global glob;
                //Global(int x): i(x) { } ;
                Global() // <----
                {
                i = 0;
                }
                ----------

                Comment

                • Jim Langston

                  #9
                  Re: Another problem with singleton...


                  "Manuel" <manuelbastioni REMOVETHIS@tin. it> wrote in message
                  news:43c0efbc$0 $1076$4fafbaef@ reader1.news.ti n.it...[color=blue]
                  > Rennie deGraaf wrote:
                  >[color=green][color=darkred]
                  >>>Global::Glob al(){};[/color]
                  >>
                  >>
                  >> Is this line in the header file? If so, then it will be defined in
                  >> every .cpp file that includes the header. Try putting it in a .cpp file
                  >> instead, and see if that fixes anything.[/color]
                  >
                  > It work. THANKS!
                  >
                  > But there is another thing I dont understand.
                  > If define into header is an error, why the code below work (in the header
                  > file)?
                  >
                  > Global::Global( ){};
                  >
                  > It's a definition, and it's into header too...
                  >
                  > thx,
                  >
                  > Manuel[/color]

                  Because that's the way C++ works. It's allowed in the class definition (?
                  is it the definition? I think so).


                  Comment

                  • Shezan Baig

                    #10
                    Re: Another problem with singleton...

                    Manuel wrote:[color=blue]
                    > But there is another thing I dont understand.
                    > If define into header is an error, why the code below work (in the
                    > header file)?
                    >
                    > Global::Global( ){};[/color]


                    It will work only if it is included in 1 translation unit (i.e., from
                    within one cpp file). Otherwise you will need to make it inline. To
                    do that, check out this FAQ, and the one after it:



                    Hope this helps,
                    -shez-

                    Comment

                    • Shezan Baig

                      #11
                      Re: Another problem with singleton...

                      Manuel wrote:[color=blue]
                      > But there is another thing I dont understand.
                      > If define into header is an error, why the code below work (in the
                      > header file)?
                      >
                      > Global::Global( ){};[/color]


                      It will work only if it is included in 1 translation unit (i.e., from
                      within one cpp file). Otherwise you will need to make it inline. To
                      do that, check out this FAQ, and the one after it:



                      Hope this helps,
                      -shez-

                      Comment

                      • Thomas Tutone

                        #12
                        Re: Another problem with singleton...

                        Manuel wrote:[color=blue]
                        > Manuel wrote:[color=green]
                        > > Rennie deGraaf wrote:
                        > >[color=darkred]
                        > >>> Global::Global( ){};[/color][/color][/color]
                        [color=blue][color=green][color=darkred]
                        > >> Is this line in the header file? If so, then it will be defined in
                        > >> every .cpp file that includes the header. Try putting it in a .cpp file
                        > >> instead, and see if that fixes anything.[/color][/color][/color]
                        [color=blue][color=green]
                        > > It work. THANKS!
                        > >
                        > > But there is another thing I dont understand.
                        > > If define into header is an error, why the code below work (in the
                        > > header file)?
                        > >
                        > > Global::Global( ){};
                        > >
                        > > It's a definition, and it's into header too...
                        > >[/color]
                        >
                        > help![/color]

                        I suspect you will find that the following link will help you solve (or
                        get help solving) your problem:



                        It's no longer clear what your problem is. You need to restate it in
                        full so that folks have an opportunity to understand what you are
                        asking.

                        Best regards,

                        Tom

                        Comment

                        Working...