Integer printer won't compile

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

    #1

    Integer printer won't compile

    Can you help me figure out why this integer printing program won't
    compile? I've looked far and wide in my diagnostics to figure out why,
    but I'm lost. Here's the code:

    #include <iostream>
    #include <cstdlib>
    using namespace std;

    template<int I>
    class _name
    {
    public:
    static void f()
    {
    static int i=0;
    cout << i << endl;
    i++;
    _name<go?(I-1):0>::f();
    }
    private:
    enum {go=(I-1)!=0};
    };

    // Specialization provides base case for
    // recursion
    template<>
    class _name<0>
    {
    public:
    static void f(int i){return;}
    };

    int main()
    {
    // Equivalent loop code
    _name<5>::f();
    system("PAUSE") ;
    return 0;
    }

    Any help, please? Thanks!!!!!

  • Protoman

    #2
    Re: Integer printer won't compile

    OK, I fixed the compiling prob, but now, instead of printing 0-5, it
    prints all 0s. And it's static !!!!!

    Comment

    • Andre Kostur

      #3
      Re: Integer printer won't compile

      "Protoman" <Protoman2050@g mail.com> wrote in news:1140576150 .527670.67460
      @f14g2000cwb.go oglegroups.com:
      [color=blue]
      > OK, I fixed the compiling prob, but now, instead of printing 0-5, it
      > prints all 0s. And it's static !!!!!
      >[/color]

      a) Include the text that you are referring to... makes it hard to quote
      properly.
      [color=blue]
      > #include <iostream>
      > #include <cstdlib>
      > using namespace std;
      >
      > template<int I>
      > class _name
      > {
      > public:
      > static void f()
      > {
      > static int i=0;
      > cout << i << endl;
      > i++;
      > _name<go?(I-1):0>::f();
      > }
      > private:
      > enum {go=(I-1)!=0};
      > };
      >
      > // Specialization provides base case for
      > // recursion
      > template<>
      > class _name<0>
      > {
      > public:
      > static void f(int i){return;}
      > };
      >
      > int main()
      > {
      > // Equivalent loop code
      > _name<5>::f();
      > system("PAUSE") ;
      > return 0;
      > }[/color]

      b) Because it's static, you have 5 instances of the local variable i,
      let's name them as follows:

      _name<5>::f::i
      _name<4>::f::i
      _name<3>::f::i
      _name<2>::f::i
      _name<1>::f::i

      Each one is initialized to zero, and is incremented to 1, but you never
      see it.

      c) Why aren't you just using a template function instead of an entire
      class?

      d) Post compilable, running code. Yours doesn't. There is no function
      named f in the _name<0> instance.

      e) What is "go" contributing to this problem? It does not appear to do
      anything useful.

      f) And if you fix all of these, you'll get 0-4.

      Comment

      • Andre Kostur

        #4
        Re: Integer printer won't compile

        Andre Kostur <nntpspam@kostu r.net> wrote in
        news:Xns9771C43 F7B9Dnntpspamko sturnet@207.35. 177.134:
        [color=blue]
        > "Protoman" <Protoman2050@g mail.com> wrote in[/color]
        news:1140576150 .527670.67460[color=blue]
        > @f14g2000cwb.go oglegroups.com:[color=green]
        >> // Specialization provides base case for
        >> // recursion
        >> template<>
        >> class _name<0>
        >> {
        >> public:
        >> static void f(int i){return;}
        >> };
        >>
        >> int main()
        >> {
        >> // Equivalent loop code
        >> _name<5>::f();
        >> system("PAUSE") ;
        >> return 0;
        >> }[/color]
        > d) Post compilable, running code. Yours doesn't. There is no function
        > named f in the _name<0> instance.[/color]

        Err... I meant "no function named f *taking no parameters* in the _name
        <0> instance".

        Comment

        • Protoman

          #5
          Re: Integer printer won't compile

          OK, I fixed that, but how do I initialize my static var? I have no clue
          on how to do it for a template.

          Code:

          template<int I>
          class _name
          {
          public:
          static void f()
          {
          cout << i << endl;
          _name<go?(I-1):0>::f();
          i++;
          }
          private:
          enum {go=(I-1)!=0};
          static int i;
          };

          // Specialization provides base case for
          // recursion
          template<>
          class _name<0>
          {
          public:
          static void f(){return;}
          };
          template<I> int _name<int I>::i=0;

          int main()
          {
          // Equivalent loop code
          _name<5>::f();
          system("PAUSE") ;
          return 0;
          }

          Help, please. Thanks!!!!!

          Comment

          • Protoman

            #6
            Re: Integer printer won't compile

            OK, I inited the static var, but I still get 0s. Why the hell won't it
            increment?!!!!! !!!!!!!!!?

            Code:

            //supplies static var i to _name template
            struct var
            {
            protected:
            static int i;
            };

            template<int I>
            class _name:protected var
            {
            public:
            static void f()
            {
            static int j=i;
            cout << j << endl;
            j++;
            _name<go?(I-1):0>::f();
            }
            private:
            enum {go=(I-1)!=0};
            };
            int var::i=0;
            // Specialization provides base case for
            // recursion
            template<>
            class _name<0>
            {
            public:
            static void f(){return;}
            };


            int main()
            {
            // Equivalent loop code
            _name<5>::f();
            system("PAUSE") ;
            return 0;
            }

            Help, please!!!!!!! Thanks!!!!!!!!! !!!!!!!!!!!

            Comment

            • Andre Kostur

              #7
              Re: Integer printer won't compile

              "Protoman" <Protoman2050@g mail.com> wrote in news:1140587166 .821389.21660
              @o13g2000cwo.go oglegroups.com:
              [color=blue]
              > OK, I fixed that, but how do I initialize my static var? I have no clue
              > on how to do it for a template.
              >
              > Code:
              >
              > template<int I>
              > class _name
              > {
              > public:
              > static void f()
              > {
              > cout << i << endl;
              > _name<go?(I-1):0>::f();
              > i++;
              > }
              > private:
              > enum {go=(I-1)!=0};
              > static int i;
              > };
              >
              > // Specialization provides base case for
              > // recursion
              > template<>
              > class _name<0>
              > {
              > public:
              > static void f(){return;}
              > };
              > template<I> int _name<int I>::i=0;
              >
              > int main()
              > {
              > // Equivalent loop code
              > _name<5>::f();
              > system("PAUSE") ;
              > return 0;
              > }
              >
              > Help, please. Thanks!!!!![/color]

              You're back in the same place you just left. You still have 5 independant
              instances of i. Presumably you're trying to only have one instance of i
              shared among the 5 completely unrelated classes. There's a couple of ways
              of doing it.

              1) a simple global variable (ie: int i = 0;).
              2) a common base class with a static protected variable, and the template
              inherits from that base class.

              I guess a more basic question... what are you actually trying to
              accomplish?

              Comment

              • Protoman

                #8
                Re: Integer printer won't compile


                Andre Kostur wrote:[color=blue]
                > "Protoman" <Protoman2050@g mail.com> wrote in news:1140587166 .821389.21660
                > @o13g2000cwo.go oglegroups.com:
                >[color=green]
                > > OK, I fixed that, but how do I initialize my static var? I have no clue
                > > on how to do it for a template.
                > >
                > > Code:
                > >
                > > template<int I>
                > > class _name
                > > {
                > > public:
                > > static void f()
                > > {
                > > cout << i << endl;
                > > _name<go?(I-1):0>::f();
                > > i++;
                > > }
                > > private:
                > > enum {go=(I-1)!=0};
                > > static int i;
                > > };
                > >
                > > // Specialization provides base case for
                > > // recursion
                > > template<>
                > > class _name<0>
                > > {
                > > public:
                > > static void f(){return;}
                > > };
                > > template<I> int _name<int I>::i=0;
                > >
                > > int main()
                > > {
                > > // Equivalent loop code
                > > _name<5>::f();
                > > system("PAUSE") ;
                > > return 0;
                > > }
                > >
                > > Help, please. Thanks!!!!![/color]
                >
                > You're back in the same place you just left. You still have 5 independant
                > instances of i. Presumably you're trying to only have one instance of i
                > shared among the 5 completely unrelated classes. There's a couple of ways
                > of doing it.
                >
                > 1) a simple global variable (ie: int i = 0;).
                > 2) a common base class with a static protected variable, and the template
                > inherits from that base class.
                >
                > I guess a more basic question... what are you actually trying to
                > accomplish?[/color]

                Print 0-5!!!!! And did you read my last post?!? I made _name
                protectedly inherit from the struct var, which contains the static var
                i. And its still not working!!!!! And globals are evil!!!!

                Comment

                • Andre Kostur

                  #9
                  Re: Integer printer won't compile

                  "Protoman" <Protoman2050@g mail.com> wrote in
                  news:1140592978 .708305.99030@z 14g2000cwz.goog legroups.com:
                  [color=blue]
                  >
                  > Andre Kostur wrote:[color=green]
                  >> "Protoman" <Protoman2050@g mail.com> wrote in
                  >> news:1140587166 .821389.21660 @o13g2000cwo.go oglegroups.com:
                  >>[color=darkred]
                  >> > OK, I fixed that, but how do I initialize my static var? I have no
                  >> > clue on how to do it for a template.
                  >> >
                  >> > Code:
                  >> >
                  >> > template<int I>
                  >> > class _name
                  >> > {
                  >> > public:
                  >> > static void f()
                  >> > {
                  >> > cout << i << endl;
                  >> > _name<go?(I-1):0>::f();
                  >> > i++;
                  >> > }
                  >> > private:
                  >> > enum {go=(I-1)!=0};
                  >> > static int i;
                  >> > };
                  >> >
                  >> > // Specialization provides base case for
                  >> > // recursion
                  >> > template<>
                  >> > class _name<0>
                  >> > {
                  >> > public:
                  >> > static void f(){return;}
                  >> > };
                  >> > template<I> int _name<int I>::i=0;
                  >> >
                  >> > int main()
                  >> > {
                  >> > // Equivalent loop code
                  >> > _name<5>::f();
                  >> > system("PAUSE") ;
                  >> > return 0;
                  >> > }
                  >> >
                  >> > Help, please. Thanks!!!!![/color]
                  >>
                  >> You're back in the same place you just left. You still have 5
                  >> independant instances of i. Presumably you're trying to only have
                  >> one instance of i shared among the 5 completely unrelated classes.
                  >> There's a couple of ways of doing it.
                  >>
                  >> 1) a simple global variable (ie: int i = 0;).
                  >> 2) a common base class with a static protected variable, and the
                  >> template inherits from that base class.
                  >>
                  >> I guess a more basic question... what are you actually trying to
                  >> accomplish?[/color]
                  >
                  > Print 0-5!!!!![/color]

                  That's easy:

                  #include <iostream>
                  int main() { cout << "0\n1\n2\n3\n4\ n5\n"; }.

                  But I suspect you have more requirements than simply printing 0-5 ...
                  [color=blue]
                  > And did you read my last post?!? I made _name
                  > protectedly inherit from the struct var, which contains the static var
                  > i. And its still not working!!!!! And globals are evil!!!![/color]

                  Your last post had not made it to my newsserver yet. Do not assume the
                  method by which I read USENET. Which I why I quoted the post that I'm
                  replying to. (And, yes, one should avoid global variables... I'm only
                  pointing it out as an option).

                  Quoting the other post of yours:
                  [color=blue]
                  > //supplies static var i to _name template
                  > struct var
                  > {
                  > protected:
                  > static int i;
                  > };
                  >
                  > template<int I>
                  > class _name:protected var
                  > {
                  > public:
                  > static void f()
                  > {
                  > static int j=i;
                  > cout << j << endl;
                  > j++;
                  > _name<go?(I-1):0>::f();
                  > }
                  > private:
                  > enum {go=(I-1)!=0};
                  > };
                  > int var::i=0;
                  > // Specialization provides base case for
                  > // recursion
                  > template<>
                  > class _name<0>
                  > {
                  > public:
                  > static void f(){return;}
                  > };
                  >
                  >
                  > int main()
                  > {
                  > // Equivalent loop code
                  > _name<5>::f();
                  > system("PAUSE") ;
                  > return 0;
                  > }[/color]

                  You did not just make i inhereted from a common base class, you've also
                  added a j variable for some reason. Also, why do you need a variable at
                  all? Why not:

                  #include <iostream>
                  using namespace std;
                  template <int I> class _name {
                  public:
                  static void f() { _name<I - 1>::f(); cout << I << "\n"; };
                  };

                  template <> class _name<0> {
                  public:
                  static void f() { cout << "0\n"; };
                  };

                  int main() { _name<5>::f(); }

                  Heck.. why bother with a templated solution at all? Why not:

                  #include <iostream>
                  using namespace std;
                  void f(int i) {
                  if (i == 0) { cout << "0\n"; return; }
                  f(i - 1); cout << i << "\n";
                  }

                  int main() { f(5); }

                  Comment

                  • Protoman

                    #10
                    Re: Integer printer won't compile


                    Andre Kostur wrote:[color=blue]
                    > "Protoman" <Protoman2050@g mail.com> wrote in
                    > news:1140592978 .708305.99030@z 14g2000cwz.goog legroups.com:
                    >[color=green]
                    > >
                    > > Andre Kostur wrote:[color=darkred]
                    > >> "Protoman" <Protoman2050@g mail.com> wrote in
                    > >> news:1140587166 .821389.21660 @o13g2000cwo.go oglegroups.com:
                    > >>
                    > >> > OK, I fixed that, but how do I initialize my static var? I have no
                    > >> > clue on how to do it for a template.
                    > >> >
                    > >> > Code:
                    > >> >
                    > >> > template<int I>
                    > >> > class _name
                    > >> > {
                    > >> > public:
                    > >> > static void f()
                    > >> > {
                    > >> > cout << i << endl;
                    > >> > _name<go?(I-1):0>::f();
                    > >> > i++;
                    > >> > }
                    > >> > private:
                    > >> > enum {go=(I-1)!=0};
                    > >> > static int i;
                    > >> > };
                    > >> >
                    > >> > // Specialization provides base case for
                    > >> > // recursion
                    > >> > template<>
                    > >> > class _name<0>
                    > >> > {
                    > >> > public:
                    > >> > static void f(){return;}
                    > >> > };
                    > >> > template<I> int _name<int I>::i=0;
                    > >> >
                    > >> > int main()
                    > >> > {
                    > >> > // Equivalent loop code
                    > >> > _name<5>::f();
                    > >> > system("PAUSE") ;
                    > >> > return 0;
                    > >> > }
                    > >> >
                    > >> > Help, please. Thanks!!!!!
                    > >>
                    > >> You're back in the same place you just left. You still have 5
                    > >> independant instances of i. Presumably you're trying to only have
                    > >> one instance of i shared among the 5 completely unrelated classes.
                    > >> There's a couple of ways of doing it.
                    > >>
                    > >> 1) a simple global variable (ie: int i = 0;).
                    > >> 2) a common base class with a static protected variable, and the
                    > >> template inherits from that base class.
                    > >>
                    > >> I guess a more basic question... what are you actually trying to
                    > >> accomplish?[/color]
                    > >
                    > > Print 0-5!!!!![/color]
                    >
                    > That's easy:
                    >
                    > #include <iostream>
                    > int main() { cout << "0\n1\n2\n3\n4\ n5\n"; }.
                    >
                    > But I suspect you have more requirements than simply printing 0-5 ...
                    >[color=green]
                    > > And did you read my last post?!? I made _name
                    > > protectedly inherit from the struct var, which contains the static var
                    > > i. And its still not working!!!!! And globals are evil!!!![/color]
                    >
                    > Your last post had not made it to my newsserver yet. Do not assume the
                    > method by which I read USENET. Which I why I quoted the post that I'm
                    > replying to. (And, yes, one should avoid global variables... I'm only
                    > pointing it out as an option).
                    >
                    > Quoting the other post of yours:
                    >[color=green]
                    > > //supplies static var i to _name template
                    > > struct var
                    > > {
                    > > protected:
                    > > static int i;
                    > > };
                    > >
                    > > template<int I>
                    > > class _name:protected var
                    > > {
                    > > public:
                    > > static void f()
                    > > {
                    > > static int j=i;
                    > > cout << j << endl;
                    > > j++;
                    > > _name<go?(I-1):0>::f();
                    > > }
                    > > private:
                    > > enum {go=(I-1)!=0};
                    > > };
                    > > int var::i=0;
                    > > // Specialization provides base case for
                    > > // recursion
                    > > template<>
                    > > class _name<0>
                    > > {
                    > > public:
                    > > static void f(){return;}
                    > > };
                    > >
                    > >
                    > > int main()
                    > > {
                    > > // Equivalent loop code
                    > > _name<5>::f();
                    > > system("PAUSE") ;
                    > > return 0;
                    > > }[/color]
                    >
                    > You did not just make i inhereted from a common base class, you've also
                    > added a j variable for some reason. Also, why do you need a variable at
                    > all? Why not:
                    >
                    > #include <iostream>
                    > using namespace std;
                    > template <int I> class _name {
                    > public:
                    > static void f() { _name<I - 1>::f(); cout << I << "\n"; };
                    > };
                    >
                    > template <> class _name<0> {
                    > public:
                    > static void f() { cout << "0\n"; };
                    > };
                    >
                    > int main() { _name<5>::f(); }
                    >
                    > Heck.. why bother with a templated solution at all? Why not:
                    >
                    > #include <iostream>
                    > using namespace std;
                    > void f(int i) {
                    > if (i == 0) { cout << "0\n"; return; }
                    > f(i - 1); cout << i << "\n";
                    > }
                    >
                    > int main() { f(5); }[/color]

                    Because I want to make use of TMP!!!! And, no, I don't have anyother
                    requirements other than printing 0-5. Thanks!!!!

                    Comment

                    • Ben Pope

                      #11
                      Re: Integer printer won't compile

                      Protoman wrote:[color=blue]
                      > Andre Kostur wrote:[color=green]
                      >> "Protoman" <Protoman2050@g mail.com> wrote in
                      >> news:1140592978 .708305.99030@z 14g2000cwz.goog legroups.com:
                      >>[color=darkred]
                      >>> Andre Kostur wrote:
                      >>>> "Protoman" <Protoman2050@g mail.com> wrote in
                      >>>>
                      >>>> I guess a more basic question... what are you actually trying to
                      >>>> accomplish?
                      >>> Print 0-5!!!!![/color]
                      >> That's easy:
                      >>
                      >> #include <iostream>
                      >> int main() { cout << "0\n1\n2\n3\n4\ n5\n"; }.
                      >>
                      >> But I suspect you have more requirements than simply printing 0-5 ...[/color]
                      >
                      > no, I don't have anyother
                      > requirements other than printing 0-5. Thanks!!!![/color]

                      Well then you're making it much harder than need be. The solution is
                      above, although I'm unsure if you want that or:

                      #include <iostream>
                      int main() { std::cout << "0-5"; }

                      Ben Pope
                      --
                      I'm not just a number. To many, I'm known as a string...

                      Comment

                      Working...