Strange problem about constructor

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

    Strange problem about constructor

    Hi all:

    In my code I define a class with inline constructor. But it does not
    work. I describe the class as below:

    myclass{
    public:
    myclass(int a, int b) { r1 = a; r2 = b;}

    protected:
    int r1;
    int r2;
    double z;
    }

    But the constructor does not work. When I declare an object of the
    class, the constructor can not correctly initialize the variables.

    Then I modify the code as below:

    myclass{
    public:
    myclass(int a, int b);

    protected:
    int r1;
    int r2;
    double z;
    }

    myclass::myclas s(int a, int b) {
    r1 = a;
    r2 = b;
    }

    It works. When I declare an object of the class, the constructor can
    correctly initialize variables.

    What is the problem?

    Thanks a lot.

    John
  • Andrew Koenig

    #2
    Re: Strange problem about constructor

    "John" <johnw822003@ya hoo.com> wrote in message
    news:c30e885a.0 406081609.7a728 088@posting.goo gle.com...
    [color=blue]
    > In my code I define a class with inline constructor. But it does not
    > work. I describe the class as below:
    >
    > myclass{
    > public:
    > myclass(int a, int b) { r1 = a; r2 = b;}
    >
    > protected:
    > int r1;
    > int r2;
    > double z;
    > }
    >
    > But the constructor does not work. When I declare an object of the
    > class, the constructor can not correctly initialize the variables.[/color]

    How do you know the program isn't working? The code you show above is
    obviously not the actual code, because it won't compile. So what did you
    actually do, and how do you know that it's not behaving correctly?


    Comment

    • Victor Bazarov

      #3
      Re: Strange problem about constructor

      "Andrew Koenig" <ark@acm.org> wrote...[color=blue]
      > "John" <johnw822003@ya hoo.com> wrote in message
      > news:c30e885a.0 406081609.7a728 088@posting.goo gle.com...
      >[color=green]
      > > In my code I define a class with inline constructor. But it does not
      > > work. I describe the class as below:
      > >
      > > myclass{
      > > public:
      > > myclass(int a, int b) { r1 = a; r2 = b;}
      > >
      > > protected:
      > > int r1;
      > > int r2;
      > > double z;
      > > }
      > >
      > > But the constructor does not work. When I declare an object of the
      > > class, the constructor can not correctly initialize the variables.[/color]
      >
      > How do you know the program isn't working? The code you show above is
      > obviously not the actual code, because it won't compile. So what did you
      > actually do, and how do you know that it's not behaving correctly?[/color]

      Absolutely agree with Andrew here. Just a side note, I've seen many
      times that a newbie would write

      myclass(int a, int b) { int r1 = a; int r2 = b; }

      without even realising that it's different from the intended way. It
      can even happen that while typing in their problem into a newsgroup
      posting they inadvertently correct the error. Of course, it would be
      nice if compilers warned about situations like that...

      To the OP: do NOT use assignment, use the initialiser list.

      Victor


      Comment

      • John Harrison

        #4
        Re: Strange problem about constructor


        "John" <johnw822003@ya hoo.com> wrote in message
        news:c30e885a.0 406081609.7a728 088@posting.goo gle.com...[color=blue]
        > Hi all:
        >
        > In my code I define a class with inline constructor. But it does not
        > work. I describe the class as below:
        >
        > myclass{
        > public:
        > myclass(int a, int b) { r1 = a; r2 = b;}
        >
        > protected:
        > int r1;
        > int r2;
        > double z;
        > }
        >
        > But the constructor does not work. When I declare an object of the
        > class, the constructor can not correctly initialize the variables.
        >
        > Then I modify the code as below:
        >
        > myclass{
        > public:
        > myclass(int a, int b);
        >
        > protected:
        > int r1;
        > int r2;
        > double z;
        > }
        >
        > myclass::myclas s(int a, int b) {
        > r1 = a;
        > r2 = b;
        > }
        >
        > It works. When I declare an object of the class, the constructor can
        > correctly initialize variables.
        >
        > What is the problem?
        >[/color]

        Without seeing an entire program it is impossible to say. Both code examples
        you posted are equally incorrect (missing semi colons, missing keywords etc)
        but as far as writing an inline constructor goes you did that correctly. So
        whatever your problem was it was somewhere in the code you didn't post. This
        is a very common situation with newbies posting to this group, they don't
        understand where they are going wrong and often post the part of the program
        that is correct, not the part that is wrong. If you want help then post the
        real code, and post a complete program.

        john


        Comment

        • John

          #5
          Re: Strange problem about constructor

          Thanks for reply.
          The code is not the original code. I wish I could post the original
          code here. But I can not, because the code is large and complex. The
          class in my post shows the structure of the class that generates
          problem. In my original code, I output the result after an object is
          declared, like:

          myclass v(2,3);
          std::cout<<v.a< <" "<<v.b<<std::en dl;
          The result is not 2 and 3, but some large numbers.

          But when I do not use inline constructor, the problem does not happen.
          I know it is difficult to figure out the problem without the original
          code post here.

          Thanks anyway.

          John

          "Andrew Koenig" <ark@acm.org> wrote in message news:<sEtxc.498 29$_k3.1248049@ bgtnsc05-news.ops.worldn et.att.net>...[color=blue]
          > "John" <johnw822003@ya hoo.com> wrote in message
          > news:c30e885a.0 406081609.7a728 088@posting.goo gle.com...
          >[color=green]
          > > In my code I define a class with inline constructor. But it does not
          > > work. I describe the class as below:
          > >
          > > myclass{
          > > public:
          > > myclass(int a, int b) { r1 = a; r2 = b;}
          > >
          > > protected:
          > > int r1;
          > > int r2;
          > > double z;
          > > }
          > >
          > > But the constructor does not work. When I declare an object of the
          > > class, the constructor can not correctly initialize the variables.[/color]
          >
          > How do you know the program isn't working? The code you show above is
          > obviously not the actual code, because it won't compile. So what did you
          > actually do, and how do you know that it's not behaving correctly?[/color]

          Comment

          • John Carson

            #6
            Re: Strange problem about constructor

            "John" <johnw822003@ya hoo.com> wrote in message
            news:c30e885a.0 406082229.3041f 11@posting.goog le.com[color=blue]
            > Thanks for reply.
            > The code is not the original code. I wish I could post the original
            > code here. But I can not, because the code is large and complex. The
            > class in my post shows the structure of the class that generates
            > problem.[/color]

            If the code does not generate the problem, then it does NOT show the
            structure of the class that generates the problem. Produce a code sample
            that actually shows the problem and I am sure we can quickly solve it.
            [color=blue]
            > In my original code, I output the result after an object is
            > declared, like:
            >
            > myclass v(2,3);
            > std::cout<<v.a< <" "<<v.b<<std::en dl;
            > The result is not 2 and 3, but some large numbers.[/color]

            a and b are not members of myclass as you specified it (the members are r1
            and r2). Further, those members are protected, so you cannot access them
            from, say, main().

            The following works.

            #include <iostream>
            class myclass{
            public:
            myclass(int a, int b) { r1 = a; r2 = b;}
            void print()
            {
            std::cout<< r1 <<" "<< r2 <<std::endl;
            }
            protected:
            int r1;
            int r2;
            double z;
            };

            int main()
            {
            myclass v(2,3);
            v.print();
            return 0;
            }


            --
            John Carson
            1. To reply to email address, remove donald
            2. Don't reply to email address (post here instead)

            Comment

            • grahamo

              #7
              Re: Strange problem about constructor

              /*
              This is a long shot but are you by any chance using a flakey compiler?
              i.e. not Suns Forte C++, GNU c++, VC++ or any of the other big names.
              If the compiler isn't very well known or is in beta release there's a
              SLIM SLIM SLIM possibility that they've got the inlining bit wrong or
              something. Just for kicks post your compilers version of CC
              -V(ersion). Methinks you're using a well dodgy compiler or have an
              error somewhere else in your code/app.

              In any case, give this code a lash. If it prints out anything other
              than

              entered main
              myclass.r1: 2 myclass.r2: 3

              then we've narrowed down your problem a bit. If it does print out the
              above text as above, then you have to look elsewhere as the inlined
              constructor is not an issue.


              BTW somebody mentioned that you should use the initialiser list in
              your ctor. I think that applies only for types other than built in
              types no? i.e. there's no overhead in using assignment with integers
              etc. No?




              */

              #include <iostream>


              class myclass
              {
              public:

              // if you get the expected output with this code, recompile without
              the
              // inline keyword and see what you get.
              inline myclass(int a, int b)
              {
              r1 = a;
              r2 = b;
              }

              // protected: you won't be able to get your hands on these members
              externally.
              // so I'll leave them in the public interface

              int r1;
              int r2;
              double z;

              };

              int main(int argc, char** argv)
              {
              std::cout << "entered main" << std::endl;
              myclass mc(2,3);
              std::cout << "myclass.r1 : " << mc.r1 << " myclass.r2: " << mc.r2 <<
              std::endl;
              return 0;
              }

              Comment

              • Karl Heinz Buchegger

                #8
                Re: Strange problem about constructor

                grahamo wrote:[color=blue]
                >
                >
                > BTW somebody mentioned that you should use the initialiser list in
                > your ctor. I think that applies only for types other than built in
                > types no? i.e. there's no overhead in using assignment with integers
                > etc. No?[/color]

                There is no overhead. But even with builtin types it is easy to
                show an example where initializer lists work, while assignment doesn't.
                Hint: Think of const members. Think of references.

                So getting into the habit of using initializer lists doesn't do any
                harm, but is vital in some cases.

                --
                Karl Heinz Buchegger
                kbuchegg@gascad .at

                Comment

                • New_user

                  #9
                  Re: Strange problem about constructor

                  > Thanks for reply.[color=blue]
                  > The code is not the original code. I wish I could post the original
                  > code here. But I can not, because the code is large and complex.[/color]

                  Ok, try to minimize and simplify your code so, that it is "simple" and
                  still buggy. It seems to me, that you should give us more detailed
                  description of the problem?

                  For example, compile this:

                  #include <iostream>

                  class A
                  {
                  public:
                  A(int a, int b)
                  {
                  a_ = a;
                  b_ = b;
                  }
                  int a_;
                  int b_;
                  };

                  int main()
                  {
                  A a(1,2);
                  std::cout<<a.a_ <<" "<<a.b_<<std::e ndl;
                  }

                  In this example we have 1 2 in output. And so your example does not
                  show your problem, right?

                  Comment

                  • JKop

                    #10
                    Re: Strange problem about constructor

                    Victor Bazarov posted:
                    [color=blue]
                    > To the OP: do NOT use assignment, use the initialiser list.[/color]


                    Just to expand on that a little...

                    Consider the following:


                    class BankAccountData
                    {
                    public:
                    unsigned long int const ID;

                    BankAccountData (unsigned long int& const in_ID) : ID(in_ID)
                    {
                    ;
                    }
                    };


                    Without the initializer list, you couldn't work with ID, as it is const.


                    -JKop

                    Comment

                    • John

                      #11
                      Re: Strange problem about constructor

                      Hi all:

                      Thanks a lot. I post a simplified version of my code.

                      ---------------
                      header-file.h
                      ---------------
                      #include <list>
                      #include <algorithm>

                      myclass{
                      friend class yclass;
                      public:
                      myclass(u_int32 _t a, u_int32_t b) { r1 = a; r2 = b;}
                      protected: //u_int32_t is defined type, a kind of integer.
                      u_int32_t r1;
                      u_int32_t r2;
                      double z;
                      }

                      yclass{

                      public:

                      .....
                      void m_insert(u_int3 2_t id, u_int32_t bd);
                      bool m_lookup(u_int3 2_t id, u_int32_t bd);
                      void m_purge(void);
                      void funct(u_int32_t id, u_int32_t bd);

                      std::list< myclass > myclass_list;

                      .....
                      }


                      -----------------------------------
                      file.cc
                      -----------------------------------
                      #include <header-file.h>

                      void yclass::m_inser t(u_int32_t d, u_int32_t bd) {
                      myclass b(d, bd);
                      std::cout<<"r1: "<<b.r1<<" r2:"<<b.r2<<std ::endl; //LINE 1
                      b.z = TIME;//a constant.
                      myclass_list.pu sh_back(b);

                      std::cout<<"ins ert--d:"<<d<<" bd:"<< bd<<std::endl;
                      std::list< myclass >::const_iterat or pos;
                      for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos)
                      std::cout<<"pos->r1:"<<(*pos).r 1<<" r2:"<<(*pos).r2 <<"
                      z:"<<pos->z<<std::endl ;

                      }

                      bool yclass::m_looku p(u_int32_t d, u_int32_t bd) {
                      std::list< myclass >::const_iterat or pos;
                      std::cout<<"loo kup--d:"<<d<<" bd:"<< bd<<std::endl;
                      for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
                      std::cout<<"(*p os).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::end l;
                      if (((*pos).r1 == d) && ((*pos).r2 == bd)){
                      std::cout<<"fin d it"<<std::end l; //LINE 2
                      return true;
                      }
                      }
                      return false;
                      }

                      void yclass::m_purge () {
                      std::list< myclass >::iterator pos;
                      for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
                      if((*pos).z <= 10) {
                      myclass_list.er ase(pos);
                      --pos;
                      }
                      }
                      }

                      void funct(u_int32_t id, u_int32_t bd){

                      .........
                      if(!m_lookup(id , bd)){
                      m_insert(id, bd); //LINE 3
                      }

                      .........

                      }

                      If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2
                      and 3, but two large numbers. So LINE 2 is never executed, since the
                      list -- myclass_list does not store the correct value. But if I do not
                      use inline constructor, the output is correct.

                      I post all the code related to myclass. I hope the bug has been
                      exposed.

                      Thanks again for the help.

                      John


                      tpochep@mail.ru (New_user) wrote in message news:<d29bee5d. 0406090454.605e e6b8@posting.go ogle.com>...[color=blue][color=green]
                      > > Thanks for reply.
                      > > The code is not the original code. I wish I could post the original
                      > > code here. But I can not, because the code is large and complex.[/color]
                      >
                      > Ok, try to minimize and simplify your code so, that it is "simple" and
                      > still buggy. It seems to me, that you should give us more detailed
                      > description of the problem?
                      >
                      > For example, compile this:
                      >
                      > #include <iostream>
                      >
                      > class A
                      > {
                      > public:
                      > A(int a, int b)
                      > {
                      > a_ = a;
                      > b_ = b;
                      > }
                      > int a_;
                      > int b_;
                      > };
                      >
                      > int main()
                      > {
                      > A a(1,2);
                      > std::cout<<a.a_ <<" "<<a.b_<<std::e ndl;
                      > }
                      >
                      > In this example we have 1 2 in output. And so your example does not
                      > show your problem, right?[/color]

                      Comment

                      • Jeff Flinn

                        #12
                        Re: Strange problem about constructor

                        Forgive the top post, but again the code you provided below is not
                        compilable, even with cursory examination. ie: myclass{ ... } is not C++,
                        You need to prefix with the keyword 'class' and terminate the class
                        declaraton with ';'

                        Get this code to compile and show us the problem.

                        Jeff F

                        "John" <johnw822003@ya hoo.com> wrote in message
                        news:c30e885a.0 406101043.7f3c8 d42@posting.goo gle.com...[color=blue]
                        > Hi all:
                        >
                        > Thanks a lot. I post a simplified version of my code.
                        >
                        > ---------------
                        > header-file.h
                        > ---------------
                        > #include <list>
                        > #include <algorithm>
                        >
                        > myclass{
                        > friend class yclass;
                        > public:
                        > myclass(u_int32 _t a, u_int32_t b) { r1 = a; r2 = b;}
                        > protected: //u_int32_t is defined type, a kind of integer.
                        > u_int32_t r1;
                        > u_int32_t r2;
                        > double z;
                        > }
                        >
                        > yclass{
                        >
                        > public:
                        >
                        > .....
                        > void m_insert(u_int3 2_t id, u_int32_t bd);
                        > bool m_lookup(u_int3 2_t id, u_int32_t bd);
                        > void m_purge(void);
                        > void funct(u_int32_t id, u_int32_t bd);
                        >
                        > std::list< myclass > myclass_list;
                        >
                        > .....
                        > }
                        >
                        >
                        > -----------------------------------
                        > file.cc
                        > -----------------------------------
                        > #include <header-file.h>
                        >
                        > void yclass::m_inser t(u_int32_t d, u_int32_t bd) {
                        > myclass b(d, bd);
                        > std::cout<<"r1: "<<b.r1<<" r2:"<<b.r2<<std ::endl; //LINE 1
                        > b.z = TIME;//a constant.
                        > myclass_list.pu sh_back(b);
                        >
                        > std::cout<<"ins ert--d:"<<d<<" bd:"<< bd<<std::endl;
                        > std::list< myclass >::const_iterat or pos;
                        > for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos)
                        > std::cout<<"pos->r1:"<<(*pos).r 1<<" r2:"<<(*pos).r2 <<"
                        > z:"<<pos->z<<std::endl ;
                        >
                        > }
                        >
                        > bool yclass::m_looku p(u_int32_t d, u_int32_t bd) {
                        > std::list< myclass >::const_iterat or pos;
                        > std::cout<<"loo kup--d:"<<d<<" bd:"<< bd<<std::endl;
                        > for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
                        > std::cout<<"(*p os).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::end l;
                        > if (((*pos).r1 == d) && ((*pos).r2 == bd)){
                        > std::cout<<"fin d it"<<std::end l; //LINE 2
                        > return true;
                        > }
                        > }
                        > return false;
                        > }
                        >
                        > void yclass::m_purge () {
                        > std::list< myclass >::iterator pos;
                        > for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
                        > if((*pos).z <= 10) {
                        > myclass_list.er ase(pos);
                        > --pos;
                        > }
                        > }
                        > }
                        >
                        > void funct(u_int32_t id, u_int32_t bd){
                        >
                        > .........
                        > if(!m_lookup(id , bd)){
                        > m_insert(id, bd); //LINE 3
                        > }
                        >
                        > .........
                        >
                        > }
                        >
                        > If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2
                        > and 3, but two large numbers. So LINE 2 is never executed, since the
                        > list -- myclass_list does not store the correct value. But if I do not
                        > use inline constructor, the output is correct.
                        >
                        > I post all the code related to myclass. I hope the bug has been
                        > exposed.
                        >
                        > Thanks again for the help.
                        >
                        > John
                        >
                        >
                        > tpochep@mail.ru (New_user) wrote in message[/color]
                        news:<d29bee5d. 0406090454.605e e6b8@posting.go ogle.com>...[color=blue][color=green][color=darkred]
                        > > > Thanks for reply.
                        > > > The code is not the original code. I wish I could post the original
                        > > > code here. But I can not, because the code is large and complex.[/color]
                        > >
                        > > Ok, try to minimize and simplify your code so, that it is "simple" and
                        > > still buggy. It seems to me, that you should give us more detailed
                        > > description of the problem?
                        > >
                        > > For example, compile this:
                        > >
                        > > #include <iostream>
                        > >
                        > > class A
                        > > {
                        > > public:
                        > > A(int a, int b)
                        > > {
                        > > a_ = a;
                        > > b_ = b;
                        > > }
                        > > int a_;
                        > > int b_;
                        > > };
                        > >
                        > > int main()
                        > > {
                        > > A a(1,2);
                        > > std::cout<<a.a_ <<" "<<a.b_<<std::e ndl;
                        > > }
                        > >
                        > > In this example we have 1 2 in output. And so your example does not
                        > > show your problem, right?[/color][/color]


                        Comment

                        • Victor Bazarov

                          #13
                          Re: Strange problem about constructor

                          "John" <johnw822003@ya hoo.com> wrote...[color=blue]
                          > Thanks a lot. I post a simplified version of my code.[/color]

                          Too "simplified " to be useful. Please read the FAQ 5.8.
                          [color=blue]
                          >
                          > ---------------
                          > header-file.h
                          > ---------------
                          > #include <list>
                          > #include <algorithm>[/color]

                          This doesn't seem to be relevant at all.
                          [color=blue]
                          >
                          > myclass{
                          > friend class yclass;
                          > public:
                          > myclass(u_int32 _t a, u_int32_t b) { r1 = a; r2 = b;}
                          > protected: //u_int32_t is defined type, a kind of integer.
                          > u_int32_t r1;
                          > u_int32_t r2;
                          > double z;
                          > }
                          >
                          > yclass{
                          >
                          > public:
                          >
                          > .....
                          > void m_insert(u_int3 2_t id, u_int32_t bd);
                          > bool m_lookup(u_int3 2_t id, u_int32_t bd);
                          > void m_purge(void);
                          > void funct(u_int32_t id, u_int32_t bd);
                          >
                          > std::list< myclass > myclass_list;
                          >
                          > .....
                          > }
                          >
                          >
                          > -----------------------------------
                          > file.cc
                          > -----------------------------------
                          > #include <header-file.h>
                          >
                          > void yclass::m_inser t(u_int32_t d, u_int32_t bd) {[/color]

                          What are the values of these arguments when you step into this
                          function?
                          [color=blue]
                          > myclass b(d, bd);[/color]

                          What do you see in the debugger when you step over this definition?
                          [color=blue]
                          > std::cout<<"r1: "<<b.r1<<" r2:"<<b.r2<<std ::endl; //LINE 1
                          > b.z = TIME;//a constant.
                          > myclass_list.pu sh_back(b);
                          >
                          > std::cout<<"ins ert--d:"<<d<<" bd:"<< bd<<std::endl;
                          > std::list< myclass >::const_iterat or pos;
                          > for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos)
                          > std::cout<<"pos->r1:"<<(*pos).r 1<<" r2:"<<(*pos).r2 <<"
                          > z:"<<pos->z<<std::endl ;
                          >
                          > }
                          >
                          > bool yclass::m_looku p(u_int32_t d, u_int32_t bd) {[/color]

                          What are the values of the arguments here? Are they correct?
                          [color=blue]
                          > std::list< myclass >::const_iterat or pos;
                          > std::cout<<"loo kup--d:"<<d<<" bd:"<< bd<<std::endl;[/color]

                          What output do you see here?
                          [color=blue]
                          > for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
                          > std::cout<<"(*p os).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::end l;
                          > if (((*pos).r1 == d) && ((*pos).r2 == bd)){
                          > std::cout<<"fin d it"<<std::end l; //LINE 2
                          > return true;
                          > }
                          > }
                          > return false;
                          > }
                          >
                          > void yclass::m_purge () {
                          > std::list< myclass >::iterator pos;
                          > for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
                          > if((*pos).z <= 10) {
                          > myclass_list.er ase(pos);
                          > --pos;
                          > }
                          > }
                          > }[/color]

                          Seems that 'm_purge' is also errelevant.
                          [color=blue]
                          >
                          > void funct(u_int32_t id, u_int32_t bd)[/color]
                          {

                          What are the values of 'id' and 'bd' here?
                          [color=blue]
                          >
                          > .........
                          > if(!m_lookup(id , bd)){
                          > m_insert(id, bd); //LINE 3
                          > }
                          >
                          > .........
                          >
                          > }[/color]

                          Who is calling this function? How is it called?
                          [color=blue]
                          >
                          > If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2
                          > and 3, but two large numbers. So LINE 2 is never executed, since the
                          > list -- myclass_list does not store the correct value. But if I do not
                          > use inline constructor, the output is correct.
                          >
                          > I post all the code related to myclass.[/color]

                          No, you didn't post _all_ code. You posted _some_ code. Non-compilable,
                          first. Incomplete, second.

                          [color=blue]
                          > I hope the bug has been
                          > exposed.[/color]

                          How can it have been? So far only your inattentiveness to requirements
                          has.

                          V


                          Comment

                          • John Harrison

                            #14
                            Re: Strange problem about constructor


                            "John" <johnw822003@ya hoo.com> wrote in message
                            news:c30e885a.0 406101043.7f3c8 d42@posting.goo gle.com...[color=blue]
                            > Hi all:
                            >
                            > Thanks a lot. I post a simplified version of my code.
                            >
                            > ---------------
                            > header-file.h
                            > ---------------
                            > #include <list>
                            > #include <algorithm>
                            >
                            > myclass{
                            > friend class yclass;
                            > public:
                            > myclass(u_int32 _t a, u_int32_t b) { r1 = a; r2 = b;}
                            > protected: //u_int32_t is defined type, a kind of integer.
                            > u_int32_t r1;
                            > u_int32_t r2;
                            > double z;
                            > }
                            >
                            > yclass{
                            >
                            > public:
                            >
                            > .....
                            > void m_insert(u_int3 2_t id, u_int32_t bd);
                            > bool m_lookup(u_int3 2_t id, u_int32_t bd);
                            > void m_purge(void);
                            > void funct(u_int32_t id, u_int32_t bd);
                            >
                            > std::list< myclass > myclass_list;
                            >
                            > .....
                            > }
                            >
                            >
                            > -----------------------------------
                            > file.cc
                            > -----------------------------------
                            > #include <header-file.h>
                            >
                            > void yclass::m_inser t(u_int32_t d, u_int32_t bd) {
                            > myclass b(d, bd);
                            > std::cout<<"r1: "<<b.r1<<" r2:"<<b.r2<<std ::endl; //LINE 1
                            > b.z = TIME;//a constant.
                            > myclass_list.pu sh_back(b);
                            >
                            > std::cout<<"ins ert--d:"<<d<<" bd:"<< bd<<std::endl;
                            > std::list< myclass >::const_iterat or pos;
                            > for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos)
                            > std::cout<<"pos->r1:"<<(*pos).r 1<<" r2:"<<(*pos).r2 <<"
                            > z:"<<pos->z<<std::endl ;
                            >
                            > }
                            >
                            > bool yclass::m_looku p(u_int32_t d, u_int32_t bd) {
                            > std::list< myclass >::const_iterat or pos;
                            > std::cout<<"loo kup--d:"<<d<<" bd:"<< bd<<std::endl;
                            > for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
                            > std::cout<<"(*p os).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::end l;
                            > if (((*pos).r1 == d) && ((*pos).r2 == bd)){
                            > std::cout<<"fin d it"<<std::end l; //LINE 2
                            > return true;
                            > }
                            > }
                            > return false;
                            > }
                            >
                            > void yclass::m_purge () {
                            > std::list< myclass >::iterator pos;
                            > for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
                            > if((*pos).z <= 10) {
                            > myclass_list.er ase(pos);
                            > --pos;
                            > }
                            > }
                            > }
                            >
                            > void funct(u_int32_t id, u_int32_t bd){
                            >
                            > .........
                            > if(!m_lookup(id , bd)){
                            > m_insert(id, bd); //LINE 3
                            > }
                            >
                            > .........
                            >
                            > }
                            >
                            > If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2
                            > and 3, but two large numbers. So LINE 2 is never executed, since the
                            > list -- myclass_list does not store the correct value. But if I do not
                            > use inline constructor, the output is correct.
                            >
                            > I post all the code related to myclass. I hope the bug has been
                            > exposed.
                            >[/color]

                            Perhaps the problem is related to u_int32_t, how is that defined?

                            There is nothing wrong with your inline constructor.

                            john


                            Comment

                            Working...