C++ problem

Björn Lindberg d95-bli at nada.kth.se
Sat Aug 7 06:45:03 PDT 2004


michael <michael8110 at terra.com.br> writes:

> Daniel,thanks a lot,in fact using 'string' did it.Björn,very nice to
> read  you again,i still have to look at your message more closely,but
> what is  really difficult for me to understand at this point is why
> #include<iostream>
> using namespace std;
> int main()
> {char a[20]="citroen";
> cout<<a;
> return 0;
> }
> 
> produces
>
>    michael ~ # ./a.out
> citroen
> 
> and
> 
> #include<iostream>
> using namespace std;
> class car
> {public:
> char itsName[20];
> };

This is because of the same reason I discussed in my previous posts,
namely the difference between /initialization/, and
/assignment/. Syntactically they happen to look very similar in C++
(and some other languages), but semantically they are very different.

The first case,

  char a[20]="citroen";

is an example of an /initialization/. A variable comes into existance,
and C++ provides a way to give it an initial value. This is completely
different from doing

  char a[20];  // Default initialization.
  a="citroen"; // Assignment (will not work).

The only way to initialize a member field of an object is via its
constructors. This is because the variable is created when the object
is constructed, so any attempts to set the field after construction is
complete has to be through /assignment/.

Even when initializing a member field in the constructor, it is not
enough to assign to it in the constructor body, because by then the
object has been fully constructed, including all fields. (To
understand this, you need to read up on the process of object
construction in C++, either in a book or web resource.) This is why
constructors have an initialization list.

Thus, the /only/ way to /initialize/ a member field of an object is by
using the /initialization list/ of that objects constructors.

However, I think that you additionally suffer from another
misconception. In C++, (and C), you cannot assign arrays. Every effort
to assign an array will either fail, or just copy a pointer. The
actual array is never copied. The solution is in C to use functions
such as strcpy(), and in C++ to use the standard class std::string
instead of character arrays for representing strings.

> int main()
> {car c;
> c.itsName[20]="citroen";
> cout<<c.itsName;
> return 0;
> }
> 
> returns this error message
> 
>    michael ~ # g++ ./test2.cpp
> test2.cpp: In function `int main()':
> test2.cpp:12: error: invalid conversion from `const char*' to `char'
> (ISN'T it the same to write char a[20]="citroen"; and
> c.itsName[20]="citroen"; since itsName[20] is a char in the class
> declaration?I tried using string,as Daniel and you suggested and
> everything worked fine,but i still would like to see through
> this.Thanks  again.

The compiler is telling exactly what is wrong! If c.itsName is of type
'char[]', or 'char *', then c.itsName[20] obviously is of type 'char',
right? You are selecting the 21st element of the array, and the
elements themselves are of type 'char'.


Björn



More information about the lfs-chat mailing list