silly question

E

earl

class foo
{
foo();
private:
char table[64];
}

foo::foo()
{
table[0] = 'a';
}

Why doesnt the above work ?
 
R

Ron Natalie

earl said:
class foo
{
foo();
private:
char table[64];
}

foo::foo()
{
table[0] = 'a';
}

Why doesnt the above work ?

Well other than the fact you left a semicolon off the end of the class
definition.
 
C

Charles Herman

earl said:
class foo
{
foo();
private:
char table[64];
}

foo::foo()
{
table[0] = 'a';
}

Why doesnt the above work ?

You cannot instantiate a variable in a class definition. That is, you
cannot allocate memory to an array or initialize a variable. One way to do
what you wan to do is use "char* table" in the definition, and then malloc
space for the table.

-charles
 
A

Alf P. Steinbach

earl said:
class foo
{
foo();
private:
char table[64];
}

foo::foo()
{
table[0] = 'a';
}

Why doesnt the above work ?

You cannot instantiate a variable in a class definition.

That is incorrect.

That is, you cannot allocate memory to an array or initialize a variable.

That is incorrect.

One way to do
what you wan to do is use "char* table" in the definition, and then malloc
space for the table.

That is positively misguided.
 
H

Howard

Charles Herman said:
earl said:
class foo
{
foo();
private:
char table[64];
}

foo::foo()
{
table[0] = 'a';
}

Why doesnt the above work ?

You cannot instantiate a variable in a class definition. That is, you
cannot allocate memory to an array or initialize a variable. One way to do
what you wan to do is use "char* table" in the definition, and then malloc
space for the table.

-charles

He's not doing any initialization or allocation in the class definition.
He's declared the array to be of size 64, then set the first character of it
to 'a'. The problem (as others have stated) is a missing semicolon after
the class definition.

(If he expects to read the array as a null-terminated string, then I think
he also needs to set table[1] = 0. But I suspect that there will eventually
be more code than was shown and the point will be moot.)

-Howard
 
E

earl

Alf P. Steinbach said:
earl said:
class foo
{
foo();
private:
char table[64];
}

foo::foo()
{
table[0] = 'a';
}

Why doesnt the above work ?

You cannot instantiate a variable in a class definition.

That is incorrect.

That is, you cannot allocate memory to an array or initialize a variable.

That is incorrect.

One way to do
what you wan to do is use "char* table" in the definition, and then malloc
space for the table.

That is positively misguided.

You care to tell what is correct then ?
 
E

earl

Pete Becker said:
earl said:
class foo
{
foo();
private:
char table[64];
}

foo::foo()
{
table[0] = 'a';
}

Why doesnt the above work ?

Missing semi-colon.

The semi-colan was a typo by me when typing this post, it's done correctly
in my program.
 
A

Alf P. Steinbach

Alf P. Steinbach said:
earl wrote:

class foo
{
foo();
private:
char table[64];
}

foo::foo()
{
table[0] = 'a';
}

Why doesnt the above work ?

You cannot instantiate a variable in a class definition.

That is incorrect.

That is, you cannot allocate memory to an array or initialize a variable.

That is incorrect.

One way to do
what you wan to do is use "char* table" in the definition, and then malloc
space for the table.

That is positively misguided.

You care to tell what is correct then ?

As others have pointed out, the one and only error in your code was
a missing semicolon. But then you say you have that semicolon in
"the program". Well, in that case all is well, nothing is wrong.

Perhaps there is some other problem related to code you haven't
shown, but none of us are telepaths (although I can guess that
you're trying to use 'table' as a zero-terminated string, but others
have already covered that possibility, too).

Post a minimal example that _compiles_ (if failure to compile isn't
the problem) and exemplifies whatever problem you have; also explain
what you're trying to achieve and what effect you get instead.
 
P

Pete Becker

earl said:
Pete Becker said:
earl said:
class foo
{
foo();
private:
char table[64];
}

foo::foo()
{
table[0] = 'a';
}

Why doesnt the above work ?

Missing semi-colon.
The semi-colan was a typo by me when typing this post, it's done correctly
in my program.

Well, you need to give at least a hint about why you think there's
something wrong.
 
D

Dave

earl said:
class foo
{
foo();
private:
char table[64];
}

foo::foo()
{
table[0] = 'a';
}

Why doesnt the above work ?


In addition to the other comments, you have a private constructor. How will
you ever create an object of this class?
 
E

earl

class foo
{
foo();
private:
char table[64];
};

foo::foo()
{
for(int index=0; index < 64; index++)
table[index] = index;
}

in the main program I do ;

foo test;

nothing more, just run the constructor

it seems to run through the above loop coz I get 'Press any key to continue'
then I get popup box saying test.exe has encountered a problem and needs to
close. We are sorry for the inconvenience.

If I remove the for loop in the constructor everything runs smoothly.
 
E

earl

earl said:
class foo
{
foo();
private:
char table[64];
};

foo::foo()
{
for(int index=0; index < 64; index++)
table[index] = index;
}

in the main program I do ;

foo test;

nothing more, just run the constructor

it seems to run through the above loop coz I get 'Press any key to continue'
then I get popup box saying test.exe has encountered a problem and needs to
close. We are sorry for the inconvenience.

If I remove the for loop in the constructor everything runs smoothly.

btw, the constructor should be public
 
M

Mike Wahler

earl said:
class foo
{
foo();
private:
char table[64];
};

foo::foo()
{
for(int index=0; index < 64; index++)
table[index] = index;
}

in the main program I do ;

foo test;

Don't just tell us, *show* us. That is a *complete*,
compilable program.
nothing more, just run the constructor

it seems to run through the above loop coz I get 'Press any key to
continue'

That's from your IDE, not part of your program.
then I get popup box saying test.exe has encountered a problem and needs to
close. We are sorry for the inconvenience.

This appears to be a complaint from your operating
system about something. Perhaps its caused by your
program, perhaps by something else. We cannot rule
out your program until we see its *exact* form as
you've compiled it.
If I remove the for loop in the constructor everything runs smoothly.

Don't just describe it, *show* it, verbatim.

-Mike
 
A

Alf P. Steinbach

class foo
{
foo();
private:
char table[64];
};

foo::foo()
{
for(int index=0; index < 64; index++)

General coding practice advice 1: try to get into the habit of
writing '++index' instead of 'index++'.

General coding practice advice 2: don't litter your code with
magic numbers like '64'; give them names.


table[index] = index;
}

in the main program I do ;

foo test;

That should not compile since you don't have an accessible default
constructor (members of a 'class' are private by default, and you
use the default in the declaration of the default constructor).

nothing more, just run the constructor

it seems to run through the above loop coz I get 'Press any key to continue'
then I get popup box saying test.exe has encountered a problem and needs to
close. We are sorry for the inconvenience.

It seems you're victim of a less than correct compiler.

Make the default constructor public and try again.
 
E

earl

Mike Wahler said:
earl said:
class foo
{
foo();
private:
char table[64];
};

foo::foo()
{
for(int index=0; index < 64; index++)
table[index] = index;
}

in the main program I do ;

foo test;

Don't just tell us, *show* us. That is a *complete*,
compilable program.
nothing more, just run the constructor

it seems to run through the above loop coz I get 'Press any key to
continue'

That's from your IDE, not part of your program.
then I get popup box saying test.exe has encountered a problem and needs to
close. We are sorry for the inconvenience.

This appears to be a complaint from your operating
system about something. Perhaps its caused by your
program, perhaps by something else. We cannot rule
out your program until we see its *exact* form as
you've compiled it.
If I remove the for loop in the constructor everything runs smoothly.

Don't just describe it, *show* it, verbatim.

-Mike

Did a rebuild of my program and now all of a sudden everything is alright.
Thanks for the help though.
 
E

earl

Alf P. Steinbach said:
class foo
{
foo();
private:
char table[64];
};

foo::foo()
{
for(int index=0; index < 64; index++)

General coding practice advice 1: try to get into the habit of
writing '++index' instead of 'index++'.

General coding practice advice 2: don't litter your code with
magic numbers like '64'; give them names.


table[index] = index;
}

in the main program I do ;

foo test;

That should not compile since you don't have an accessible default
constructor (members of a 'class' are private by default, and you
use the default in the declaration of the default constructor).

nothing more, just run the constructor

it seems to run through the above loop coz I get 'Press any key to continue'
then I get popup box saying test.exe has encountered a problem and needs to
close. We are sorry for the inconvenience.

It seems you're victim of a less than correct compiler.

Make the default constructor public and try again.

After a rebuild everything seems ok.

Thanks for the help.
 
J

jeffc

earl said:
The semi-colan was a typo by me when typing this post, it's done correctly
in my program.

Earl, for your own benefit I say: that's a really, really bad habit. Make
sure everything you post is EXACTLY what you compiled. Copy and paste if
you need to. One problem I see with your code is implicit return value on
the function. Is the function void or does it return int?
 
J

jeffc

jeffc said:
One problem I see with your code is implicit return value on
the function. Is the function void or does it return int?

Disregard - I misread the code.
 
T

Thomas Matthews

earl said:
class foo
{
foo();
private:
char table[64];
};

foo::foo()
{
for(int index=0; index < 64; index++)
table[index] = index;

Here you assign an "int" value (index)
into a "char" type (table[]).

In many systems, a char type is smaller than an
int type. In these systems, an "invisible" truncation
or conversion will take place. Ugly, very ugly.

Try this:
for (char index = 0; index < 64; ++index)
table[index] = index;
}

in the main program I do ;

foo test;

nothing more, just run the constructor

it seems to run through the above loop coz I get 'Press any key to continue'
then I get popup box saying test.exe has encountered a problem and needs to
close. We are sorry for the inconvenience.

If I remove the for loop in the constructor everything runs smoothly.

One item that others haven't mentioned is that you
declare the array as type "char" while you assign it
"int" values. Although many compilers may not issue a warning,
I believe it should, and you should turn up the warning levels.

In your main() program, insert the following:
std::cout << "Size of char: " << sizeof(char) << "\n";
std::cout << "Size of int: " << sizeof(int) << "\n";

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

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,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top