Re: GNU C++ code/concept help needed.

Mayank Jain <[email protected]> Mon, 19 Sep 2005 12:49:20 +0530
Newsgroups gmane.user-groups.linux.delhi.devel
Message-ID <[email protected]>
Hi Rohit,

On 9/19/05, Rohit Kumar <rohitsz2-/[email protected]> wrote:
> First code is
> here:http://www.geocities.com/rohitsz2/Student.txt
> =

> Problem:
> unable to access a char pointer. can not understand
> why.
> just a guess: is initialization needed, if yes how to
> set size for an run-time defined string length
> pointer?

The problem seems to be that you have not allocated memory to your "nm" poi=
nter.
You need to allocate mem to nm & also to char *name in your class as
you initialize it to NULL in constructor & then start copying data to
it in setName. For *name, you will also need to pass a "length" var to
the setName function, which will be the length of "nm"

so setName might look like this
void setName(char *name, int length)
{
name =3D new char [length];
strcpy(whatevr);
}

As for the size guessing of "nm", probably the way you are writing ur
input mechanism, you might need to make a future guess for it. & it
also makes sense as each database or recordset has a predifined
variable length. You cannot just let the user enter a name of n
length! Before taking the name input from user, notify the length
constraints to him.

So before nm[i]=3Dc
do
nm =3D new char[a_guessed_size];



> Second code is here:
> http://www.geocities.com/rohitsz2/ProfileCustomized.txt
> =

> Problem:
> Unable to intialize a default argument constructor
> that has char pointer. so am unable to create objects
> in main(indicated in the main function in the code)
> got *strange* error for second code at compilation...
> =

> --------------------ERROR(second code)----------------
> =

> [rohitsz@localhost Constructor]$ g++
> ProfileCustomized.cpp
> ProfileCustomized.cpp: In function `int main()':
> ProfileCustomized.cpp:339: error: jump to case label
> ProfileCustomized.cpp:335: error:   crosses
> initialization of `ProfileCustomized pcn'
> ProfileCustomized.cpp:349: error: jump to case label
> ProfileCustomized.cpp:344: error:   crosses
> initialization of `ProfileCustomized pcnp'


Rohit, it seems like you cannot create variables inside case statements.
What you can do is create a pointer to ProfileCustomized *pcn &
initialize it with new in your case
pcn =3D new ProfileCustomized(nm) ;
pc =3D pcn;

This will work.

Also, try keeping your variable names a bit descriptive & do comment
your code. This will go a long way in maintaining its readibility.
Also, do not forget to delete whatevr you instantiated with new.

-- =

regards,
makuchaku
---
http://makuchaku.info
When you speak out with the courage of your convictions, people listen!
-- Valmik Thapar, Wildlife Conservationist.