Array Value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cplusplusquestion@gmail.com

    Array Value

    I have code in main function:

    int main(){
    int a[20];
    int b[20][20];

    for(int i=0; i<20; i++)
    a[i]=0;
    .......
    AClass a(a, b, c);
    ....
    }

    For AClass:

    class AClass{
    private:
    int* a1;
    int* b1[20];

    .....
    public:
    AClass(int x[], int y[][20], int z);
    }

    AClass::AClass( int x[], int y[][20], int z): a1(x), c1(z){
    for(int i=0; i<MAX; ++i)
    edge[i]=e[i];

  • cplusplusquestion@gmail.com

    #2
    Re: Array Value

    Sorry I didn't finish the edited yet:

    I have code in main function:

    int main(){
    int a[20];
    int b[20][20];

    for(int i=0; i<20; i++)
    a[i]=0;
    .......
    AClass a(a, b, c);
    ....

    }

    For AClass:

    class AClass{
    private:
    int* a1;
    int* b1[20];
    int c1;
    .....
    public:
    AClass(int x[], int y[][20], int z);
    .....
    }

    AClass::AClass( int x[], int y[][20], int z): a1(x), c1(z){

    // here a1's value is right //

    for(int i=0; i<20; ++i)
    b1[i]=y[i];

    // a1's value changed to some strange value like
    a[0]=-7890987 //

    .....
    }


    When I create an object using constructor AClass, the "a1" value is
    assigned wrong, can anyone tell me what's wrong with it?

    Comment

    • joseph  cook

      #3
      Re: Array Value

      On Oct 7, 11:12 pm, cplusplusquest. ..@gmail.com wrote:
      Sorry I didn't finish the edited yet:
      >
      I have code in main function:
      >
      int main(){
        int a[20];
        int b[20][20];
      >
        for(int i=0; i<20; i++)
            a[i]=0;
        .......
        AClass a(a, b, c);
      This compiled?? What compiler? You are declaring two variable 'a' in
      the same scope. One is an array, and the other is of type "AClass"
      Joe C

      Comment

      • cplusplusquestion@gmail.com

        #4
        Re: Array Value

        On Oct 8, 1:36 pm, joseph cook <joec...@gmail. comwrote:
        On Oct 7, 11:12 pm, cplusplusquest. ..@gmail.com wrote:
        >
        Sorry I didn't finish the edited yet:
        >
        I have code in main function:
        >
        int main(){
        int a[20];
        int b[20][20];
        >
        for(int i=0; i<20; i++)
        a[i]=0;
        .......
        AClass a(a, b, c);
        >
        This compiled?? What compiler? You are declaring two variable 'a' in
        the same scope. One is an array, and the other is of type "AClass"
        Joe C
        Sorry again. You are right. The declaration should be AClass
        aclass(a, b, c).

        Comment

        • cplusplusquestion@gmail.com

          #5
          Re: Array Value

          I can only think about the problem may come from array size. However,
          the size here only 20, not very big. If I re-write the code as:

          for(int i=0; i<19; ++i)
          b1[i]=y[i];

          It seems no problem. Is there any explanation?


          Comment

          • cplusplusquestion@gmail.com

            #6
            Re: Array Value

            I can only think about the problem may come from array size. However,
            the size here only 20, not very big. If I re-write the code as:

            for(int i=0; i<19; ++i)
            b1[i]=y[i];

            It seems no problem. Is there any explanation?


            Comment

            • Salt_Peter

              #7
              Re: Array Value

              On Oct 7, 11:44 pm, cplusplusquest. ..@gmail.com wrote:
              On Oct 8, 1:36 pm, joseph cook <joec...@gmail. comwrote:
              >
              >
              >
              On Oct 7, 11:12 pm, cplusplusquest. ..@gmail.com wrote:
              >
              Sorry I didn't finish the edited yet:
              >
              I have code in main function:
              >
              int main(){
              int a[20];
              int b[20][20];
              >
              for(int i=0; i<20; i++)
              a[i]=0;
              .......
              AClass a(a, b, c);
              >
              This compiled?? What compiler? You are declaring two variable 'a' in
              the same scope. One is an array, and the other is of type "AClass"
              Joe C
              >
              Sorry again. You are right. The declaration should be AClass
              aclass(a, b, c).
              ok, to get you started...
              this is a class:

              class A { };

              this is not:

              class A { }

              This doesn't compile:

              class A
              {
              public:
              A(int z) : n(z) { }
              };

              int main()
              {
              A a;
              }

              but this should:

              class A
              {
              int n;
              public:
              A(int z) : n(z) { }
              };

              int main()
              {
              A a(99);
              }

              Magic numbers is usually bad news, templates are SO simple:

              template < typename T, const std::size_t Size >
              class A
              {
              T array[Size];
              public:
              A()
              {
              for( std::size_t i = 0; i < Size; ++i )
              array[i] = i;
              }
              };

              int main()
              {
              A< int, 20 a;
              }

              See any pointers? No, not directly, why?
              Pointers nearly always mean MORE work, less maintainable code, and few
              guarentees.
              You can pass an array by reference, but even that is a lost exercise.
              A solution using std::vector< int would most likely be vastly
              superior.

              Comment

              • cplusplusquestion@gmail.com

                #8
                Re: Array Value

                On Oct 8, 2:45 pm, Salt_Peter <pj_h...@yahoo. comwrote:
                On Oct 7, 11:44 pm, cplusplusquest. ..@gmail.com wrote:
                >
                >
                >
                On Oct 8, 1:36 pm, joseph cook <joec...@gmail. comwrote:
                >
                On Oct 7, 11:12 pm, cplusplusquest. ..@gmail.com wrote:
                >
                Sorry I didn't finish the edited yet:
                >
                I have code in main function:
                >
                int main(){
                int a[20];
                int b[20][20];
                >
                for(int i=0; i<20; i++)
                a[i]=0;
                .......
                AClass a(a, b, c);
                >
                This compiled?? What compiler? You are declaring two variable 'a' in
                the same scope. One is an array, and the other is of type "AClass"
                Joe C
                >
                Sorry again. You are right. The declaration should be AClass
                aclass(a, b, c).
                >
                ok, to get you started...
                this is a class:
                >
                class A { };
                >
                this is not:
                >
                class A { }
                >
                This doesn't compile:
                >
                class A
                {
                public:
                A(int z) : n(z) { }
                >
                };
                >
                int main()
                {
                A a;
                >
                }
                >
                but this should:
                >
                class A
                {
                int n;
                public:
                A(int z) : n(z) { }
                >
                };
                >
                int main()
                {
                A a(99);
                >
                }
                >
                Magic numbers is usually bad news, templates are SO simple:
                >
                template < typename T, const std::size_t Size >
                class A
                {
                T array[Size];
                public:
                A()
                {
                for( std::size_t i = 0; i < Size; ++i )
                array[i] = i;
                }
                >
                };
                >
                int main()
                {
                A< int, 20 a;
                >
                }
                >
                See any pointers? No, not directly, why?
                Pointers nearly always mean MORE work, less maintainable code, and few
                guarentees.
                You can pass an array by reference, but even that is a lost exercise.
                A solution using std::vector< int would most likely be vastly
                superior.
                Thank you for your explanation, I still can't understand why my code
                does not work well.

                Comment

                • cplusplusquestion@gmail.com

                  #9
                  Re: Array Value

                  It seems that I find the problem. I delete old object files and re-
                  compile it, then the problem is gone. Is this the right answer? If it
                  is, why?

                  Comment

                  Working...