| re: inialisation problem.
"muser" <charlie12345@hotmail.com> wrote in message
news:f9a2a258.0309181019.302892b5@posting.google.c om...[color=blue]
> The following function[/color]
[color=blue]
> gives me the following error: warning C4700:
> local variable 'Newirrecord' used without having been initialized.[/color]
This would result from code such as:
int i;
cout << i;
[color=blue]
>
> Newirrecord is the instance of a structure. I get the same in another
> function as well, where an ordinary variable gives me the same
> warning.[/color]
Yes, this issue is independent of the object's type.
[color=blue]
>
> isn't strncpy passing a string to partnum which in turn I access with
> Newirrecord.partnum?[/color]
[color=blue]
>Shouldn't Newirrecord.partnum contain something
> at run time?[/color]
Not if you haven't explicitly caused it to.
The only types of objects which when instantiated
always have an initial value are those which have
default constructors or which are supplied an initializer
at point of definition. Since you mention 'strncpy()'
I assume the data member in question is type 'char*',
which is a built-in type. Built-in types do not
have constructors, default or otherwise.
[color=blue]
>
> Thank you in advance for your help.
>
> bool CheckDigit(ofstream& prnfile, char* record)
> {
>
> int weightingfactor;
> int remainder;
> int weightitem1, weightitem2, weightitem3, weightitem4, weightitem5;
> int product;
> int Mod11 = 11;
> int checkdigit;
> char partnum[6];[/color]
I recommend replacing char arrays with 'std::string' objects.
Life will become much simpler.
[color=blue]
>
> irrecord Newirrecord;[/color]
What is the definition of type 'irrecord'?
Does it have a default constructor? Does
this constructor initialize all the data
members?
If not, then the above instantiates a type
'irrecord' object whose data members are *not*
initialized, except for those with types that
have default ctors.
[color=blue]
>
>
> strncpy(partnum, &record[7], 6);[/color]
I think this will write off the end of your 'partnum' array.
[color=blue]
> partnum[6] = '\0';[/color]
And this is certainly out of bounds. Valid indices
for an array of six elements range from zero to five.
All these array mistakes can be eliminated by using
'std::string' object instead.
[color=blue]
> Newirrecord.partnum[6] = atol( partnum );[/color]
Another out of bounds array access.
Also, your direct access of 'Newirrecord' tells me you
have public data. Not illegal, but typically poor
design.
-Mike |