[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Array Value

cplusplusquestion

10/8/2008 3:04:00 AM

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];

8 Answers

cplusplusquestion

10/8/2008 3:13:00 AM

0

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?

joseph cook

10/8/2008 3:36:00 AM

0

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

cplusplusquestion

10/8/2008 3:44:00 AM

0

On Oct 8, 1:36 pm, joseph cook <joec...@gmail.com> wrote:
> 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).

cplusplusquestion

10/8/2008 4:41:00 AM

0

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?


cplusplusquestion

10/8/2008 4:41:00 AM

0

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?


Salt_Peter

10/8/2008 4:45:00 AM

0

On Oct 7, 11:44 pm, cplusplusquest...@gmail.com wrote:
> On Oct 8, 1:36 pm, joseph cook <joec...@gmail.com> wrote:
>
>
>
> > 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.

cplusplusquestion

10/8/2008 5:12:00 AM

0

On Oct 8, 2:45 pm, Salt_Peter <pj_h...@yahoo.com> wrote:
> On Oct 7, 11:44 pm, cplusplusquest...@gmail.com wrote:
>
>
>
> > On Oct 8, 1:36 pm, joseph cook <joec...@gmail.com> wrote:
>
> > > 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.

cplusplusquestion

10/9/2008 12:05:00 AM

0

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?