Array Value

  • Thread starter cplusplusquestion
  • Start date
C

cplusplusquestion

I have code in main function:

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

for(int i=0; i<20; i++)
a=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=e;
 
C

cplusplusquestion

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=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=y;

// 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?
 
J

joseph cook

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=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
 
C

cplusplusquestion

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=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).
 
C

cplusplusquestion

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=y;

It seems no problem. Is there any explanation?
 
C

cplusplusquestion

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=y;

It seems no problem. Is there any explanation?
 
S

Salt_Peter

On Oct 7, 11:12 pm, (e-mail address removed) 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=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;
}
};

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.
 
C

cplusplusquestion

On Oct 7, 11:12 pm, (e-mail address removed) 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=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;
}

};

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.
 
C

cplusplusquestion

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top