473,383 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

silly question

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

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

Why doesnt the above work ?
Jul 19 '05 #1
24 1529
earl wrote:

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

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

Why doesnt the above work ?


Missing semi-colon.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #2

"earl" <b@email.com> wrote in message news:3f******@news.broadpark.no...
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.
Jul 19 '05 #3
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, 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

Jul 19 '05 #4
On Tue, 21 Oct 2003 15:06:42 -0700, Charles Herman <sp**@no.spam> wrote:
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.

Jul 19 '05 #5

"Charles Herman" <sp**@no.spam> wrote in message
news:3f********@127.0.0.1...
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, 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

Jul 19 '05 #6

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Tue, 21 Oct 2003 15:06:42 -0700, Charles Herman <sp**@no.spam> wrote:
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 mallocspace for the table.


That is positively misguided.


You care to tell what is correct then ?
Jul 19 '05 #7

"Pete Becker" <pe********@acm.org> wrote in message
news:3F***************@acm.org...
earl wrote:

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

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

Why doesnt the above work ?


Missing semi-colon.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


The semi-colan was a typo by me when typing this post, it's done correctly
in my program.
Jul 19 '05 #8

"earl" <b@email.com> wrote in message news:3f******@news.broadpark.no...
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?
Jul 19 '05 #9
On Wed, 22 Oct 2003 00:16:14 +0200, "earl" <b@email.com> wrote:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Tue, 21 Oct 2003 15:06:42 -0700, Charles Herman <sp**@no.spam> wrote:
>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 thenmalloc >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.

Jul 19 '05 #10
earl wrote:

"Pete Becker" <pe********@acm.org> wrote in message
news:3F***************@acm.org...
earl wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #11
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.
Jul 19 '05 #12

"earl" <b@email.com> wrote in message news:3f******@news.broadpark.no...
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
Jul 19 '05 #13

"earl" <b@email.com> wrote in message news:3f******@news.broadpark.no...
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
Jul 19 '05 #14
On Wed, 22 Oct 2003 00:27:32 +0200, "earl" <b@email.com> wrote:
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.

Jul 19 '05 #15

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:lw***************@newsread4.news.pas.earthlin k.net...

"earl" <b@email.com> wrote in message news:3f******@news.broadpark.no...
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.
Jul 19 '05 #16

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Wed, 22 Oct 2003 00:27:32 +0200, "earl" <b@email.com> wrote:
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 toclose. 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.
Jul 19 '05 #17

"earl" <b@email.com> wrote in message news:3f******@news.broadpark.no...


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?
Jul 19 '05 #18

"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
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.
Jul 19 '05 #19
earl wrote:
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

Jul 19 '05 #20
Thomas Matthews escribió:
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.


Perhaps you must switch to Pascal? ;)

Regards.
Jul 19 '05 #21


Thomas Matthews wrote:

earl wrote:
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.


truncation: yes
conversion: no

after all a char is nothing more then a small int.
The only special thing about a char is the way it is handled
during input/output. In all other aspects one can treat
a char as beeing a variation of int with a lower bit count
(and you do not know if its signed or unsigned).
Just like a long is a variation of int with a higher bit count.

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


Although the number of bits in a char is not defined there
is a lower limit. A char must have at least 8 bits.
Clearly more then enough to store the number 64 in it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #22
In article <vp************@news.supernews.com>, be***********@yahoo.com
says...

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


Private ctors are really fairly common -- singletons (for one example)
nearly never have any ctors that aren't private (though they normally
also befriend a static function that uses a private ctor).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #23
In article <3F***************@gascad.at>, kb******@gascad.at says...

[ ... ]
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.
truncation: yes
conversion: no


Truncation: maybe
Conversion: Definitely YES

This is all covered in $4.7 "Integral conversions", which is part of
section 4, "Standard conversions".
after all a char is nothing more then a small int.
The only special thing about a char is the way it is handled
during input/output. In all other aspects one can treat
a char as beeing a variation of int with a lower bit count
(and you do not know if its signed or unsigned).
Just like a long is a variation of int with a higher bit count.


According to the standard, there are conversions and promotions.
Anytime something that's one integer type gets assigned to a variable of
another integer type, it has to go through a promotion or a conversion.
In many cases, the value of the result will be the same, but that
doesn't prevent it from being a conversion. Just for example:

int x = 0L;

This involves a conversion, even though it's guaranteed that value will
remain unaffected by the conversion.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #24


Jerry Coffin wrote:

In article <3F***************@gascad.at>, kb******@gascad.at says...

[ ... ]
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.


truncation: yes
conversion: no


Truncation: maybe
Conversion: Definitely YES

This is all covered in $4.7 "Integral conversions", which is part of
section 4, "Standard conversions".


I should have checked the standard how the word 'conversion' is
used. Thanks for correcting.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pjotr Wedersteers | last post by:
This may be a silly question, but I was wondering. If 10 clients fill out a form on my page simultaneaously and hit submit, how does my (Apache2.0.50/PHP4.3.8) server exactly ensure each gets...
4
by: Jenny | last post by:
Hi you al I have two very silly question The first one is In vb 6.0 you can add a procedure, sub or function, by clicking add procedure from the tools item on the menuba I cannot seem to find...
4
by: musosdev | last post by:
I think I'm having a dim day and should just go back to bed, but alas, I cant do that.... I'm writing a peice of code to create XHTML compliant documents using System.IO, there's probably an...
6
by: Adam Honek | last post by:
Hi, I've looked over and over and in MSDN 2005 and just can't find what is a really simple answer. How do I select the first value in a combo box to be shown? I tried the .selectedindex -1...
7
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a...
2
by: Willem Voncken | last post by:
Hi guys, might be a silly question, but i'm wondering whether it is even possible to generate access violations when using C#? I'm new at c# programming, but i have some experience with c++....
1
by: Pontifex | last post by:
Hi all, Has anyone devised a simple method for passing a string to an external script? For instance, suppose I want to produce a title bar that is very fancy and I don't want my main HTML...
2
by: toton | last post by:
Hi, This is a silly question related to syntax only. I have a template class with template member function, How to write it separately in a file (not translation unit, just want to separate...
4
by: Marcin Kasprzak | last post by:
Hello Guys, Silly question - what is the most elegant way of compiling a code similar to this one? <code> typedef struct a { b_t *b; } a_t; typedef struct b {
5
by: Ben Bacarisse | last post by:
Newbie <newbie@gmail.comwrites: <snip> The version you posted in comp.programming has the correct loop. Why change it? -- Ben.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.