class foo
{
foo();
private:
char table[64];
}
foo::foo()
{
table[0] = 'a';
}
Why doesnt the above work ? 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)
"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.
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
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.
"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
"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 ?
"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.
"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?
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.
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)
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.
"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
"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
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.
"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.
"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.
"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?
"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.
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
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.
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
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.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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++....
|
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...
|
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...
|
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 {
|
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.
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |