473,320 Members | 1,766 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,320 software developers and data experts.

array in class

Hi,
I've an array in a class declered in the following way:

char pippo[256];

In the constructor of the class I would like to fill this array, but I
received an error.
Why?
tnx

Jul 18 '06 #1
21 1511
Salvatore Di Fazio wrote:
I've an array in a class declered in the following way:

char pippo[256];

In the constructor of the class I would like to fill this array, but I
received an error.
Why?
Most likely, because you write some incorrect code.

--
Salu2
Jul 18 '06 #2

Salvatore Di Fazio wrote:
Hi,
I've an array in a class declered in the following way:

char pippo[256];

In the constructor of the class I would like to fill this array, but I
received an error.
Why?
tnx
The error is on line 42.

Jul 18 '06 #3
well, here is one way you do it....
class Foo
{
private:
char pippo[256];
public:
Foo();
};
void main()
{
Foo A;
}
Foo::Foo()
{
//constructor is called when you declared the object in main and is
usued to initalize private member variable in your class
for(int i = 0; pippo[i]; i++)
{ pippo[i] = '0'; }
}

--You might want to write more code in the main portion of your program
to "fill" the array. This program only fills it with 0's. Hope that
helps!
Salvatore Di Fazio wrote:
Hi,
I've an array in a class declered in the following way:

char pippo[256];

In the constructor of the class I would like to fill this array, but I
received an error.
Why?
tnx
Jul 18 '06 #4
Salvatore Di Fazio wrote:
Hi,
I've an array in a class declered in the following way:

char pippo[256];

In the constructor of the class I would like to fill this array, but I
received an error.
Show. Us. The. Code.


Brian
Jul 18 '06 #5
Show us the code!

Oh yea! for(i=0; pippo[i];i++) in my earlier program should be
for(i=0;i < 256;i++)

Oh yea,
uche wrote:
well, here is one way you do it....
class Foo
{
private:
char pippo[256];
public:
Foo();
};
void main()
{
Foo A;
}
Foo::Foo()
{
//constructor is called when you declared the object in main and is
usued to initalize private member variable in your class
for(int i = 0; pippo[i]; i++)
{ pippo[i] = '0'; }
}

--You might want to write more code in the main portion of your program
to "fill" the array. This program only fills it with 0's. Hope that
helps!
Salvatore Di Fazio wrote:
Hi,
I've an array in a class declered in the following way:

char pippo[256];

In the constructor of the class I would like to fill this array, but I
received an error.
Why?
tnx
Jul 18 '06 #6
uche,
Please don't top-post. Your responses should be placed below or
interleaved with what you are replying to [fixed].

uche <ur***********@hotmail.comwrote:
Salvatore Di Fazio wrote:
>Hi,
I've an array in a class declered in the following way:

char pippo[256];

In the constructor of the class I would like to fill this array, but I
received an error.

well, here is one way you do it....
class Foo
{
private:
char pippo[256];
public:
Foo();
};
void main()
main() always returns int.
{
Foo A;
}
Foo::Foo()
{
//constructor is called when you declared the object in main and is
usued to initalize private member variable in your class
for(int i = 0; pippo[i]; i++)
The behavior of this program is undefined, since in the test condition
of your loop you access uninitialized memory (pippo[i]). Better would
be:

for (int i = 0; i < 256; ++i)

with the additional recommendation that the "magic number" 256 should be
replaced by a constant.
{ pippo[i] = '0'; }
Depending on what the OP is using it for, this may be acceptable, though
I would prefer

pippo[i] = '\0';
}

--You might want to write more code in the main portion of your program
to "fill" the array. This program only fills it with 0's. Hope that
helps!
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 18 '06 #7
Nothing to easy to do :>

class Lop
{
Lop();

char a[2];
};
Lop::Lop()
{
a [] = { 1, 2 };
}

Default User ha scritto:
Show. Us. The. Code.
Brian
Jul 18 '06 #8
Salvatore Di Fazio wrote:
Default User ha scritto:
Show. Us. The. Code.
Nothing to easy to do :>

class Lop
{
Lop();

char a[2];
};
Lop::Lop()
{
a [] = { 1, 2 };
}
Please don't top-post, I have rearranged it.

You can't assign arrays. That's just the way it is. You can assign to
individual array members, so:

Lop::Lop()
{
a[0] = 1;
a[1] = 2;
}

Or:

Lop::Lop()
{
char b[sizeof a/sizeof a[0]] = {1,2};

memcpy(a, b, sizeof a);
}


Brian
Jul 18 '06 #9
Default User wrote:
Salvatore Di Fazio wrote:
>Default User ha scritto:
>>Show. Us. The. Code.
>Nothing to easy to do :>

class Lop
{
Lop();

char a[2];
};
Lop::Lop()
{
a [] = { 1, 2 };
}

Please don't top-post, I have rearranged it.

You can't assign arrays. That's just the way it is. You can assign to
individual array members, so:

Lop::Lop()
{
a[0] = 1;
a[1] = 2;
}

Or:

Lop::Lop()
{
char b[sizeof a/sizeof a[0]] = {1,2};

memcpy(a, b, sizeof a);
In an industrial program I'd prefer to see

static char b[sizeof a] = {1,2}; // since 'a' is of type char[]
// sizeof a[0] always yields 1
std::copy(b, b + sizeof b, a);
}


Brian
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 19 '06 #10
Victor Bazarov posted:

> char b[sizeof a/sizeof a[0]] = {1,2};

memcpy(a, b, sizeof a);

In an industrial program I'd prefer to see

static char b[sizeof a] = {1,2}; // since 'a' is of type char[]
// sizeof a[0] always yields 1
std::copy(b, b + sizeof b, a);

One could easy argue the opposite side by stating that the former doesn't
break if the type of "a" is changed. Sort of like why people write:

memset( p, 0, sizeof *p );

instead of:

memset( p, 0, sizeof(SomeType) );
I myself would prefer the former form (in a slightly altered form albeit:

char b[sizeof a / sizeof *a];
--

Frederick Gotham
Jul 19 '06 #11
Okkey when I've just 2 elements.

But I've an array of 256 elements where it's filled by an algorithm

Default User wrote:
>
You can't assign arrays. That's just the way it is. You can assign to
individual array members, so:

Lop::Lop()
{
a[0] = 1;
a[1] = 2;
}

Or:

Lop::Lop()
{
char b[sizeof a/sizeof a[0]] = {1,2};

memcpy(a, b, sizeof a);
}


Brian
Jul 19 '06 #12
in this kind of condition, better
use fstream functions, open the text file
contains the algorithm, and import the algorithm into
the array, that's it.



Salvatore Di Fazio wrote:
Okkey when I've just 2 elements.

But I've an array of 256 elements where it's filled by an algorithm

Default User wrote:

You can't assign arrays. That's just the way it is. You can assign to
individual array members, so:

Lop::Lop()
{
a[0] = 1;
a[1] = 2;
}

Or:

Lop::Lop()
{
char b[sizeof a/sizeof a[0]] = {1,2};

memcpy(a, b, sizeof a);
}


Brian
Jul 19 '06 #13
check out the fstream file in your include document
or read c++ book about how to use c++ to operate on files.

Salvatore Di Fazio wrote:
Okkey when I've just 2 elements.

But I've an array of 256 elements where it's filled by an algorithm

Default User wrote:

You can't assign arrays. That's just the way it is. You can assign to
individual array members, so:

Lop::Lop()
{
a[0] = 1;
a[1] = 2;
}

Or:

Lop::Lop()
{
char b[sizeof a/sizeof a[0]] = {1,2};

memcpy(a, b, sizeof a);
}


Brian
Jul 19 '06 #14
* Salvatore Di Fazio:
[top-posting]
What part of "Please don't top-post" did you not understand?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 19 '06 #15
* mike:
[top-posting on top of top-posting]
Please read the FAQ on how to post. Please don't top-post in this
group. Thank you.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 19 '06 #16
Victor Bazarov wrote:
Default User wrote:
char b[sizeof a/sizeof a[0]] = {1,2};

memcpy(a, b, sizeof a);

In an industrial program I'd prefer to see
static char b[sizeof a] = {1,2}; // since 'a' is of type char[]
// sizeof a[0] always yields 1
std::copy(b, b + sizeof b, a);
In an industrial program, I'd use something else altogether. For the
given problem, you're of course correct. It's slightly more robust to
leave it the way I have it in case someone changed the type, but not a
big deal.

Brian
Jul 19 '06 #17
Salvatore Di Fazio wrote:
Default User wrote:

You can't assign arrays. That's just the way it is. You can assign
to individual array members, so:

Lop::Lop()
{
a[0] = 1;
a[1] = 2;
}

Or:

Lop::Lop()
{
char b[sizeof a/sizeof a[0]] = {1,2};

memcpy(a, b, sizeof a);
}
Okkey when I've just 2 elements.

But I've an array of 256 elements where it's filled by an algorithm

Please don't top-post, I have rearranged the text properly.

From now on, present the ACTUAL problem, not something like the
problem. How can we suggest a solution when you hide the details?

Please note that the second version I have there isn't reliant on the
size.


Brian
Jul 19 '06 #18

Alf P. Steinbach wrote:
* Salvatore Di Fazio:
[top-posting]

What part of "Please don't top-post" did you not understand?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
What means "Top-posting"

Jul 19 '06 #19
Salvatore Di Fazio schrieb:
Alf P. Steinbach wrote:
>* Salvatore Di Fazio:
> [top-posting]

What part of "Please don't top-post" did you not understand?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

What means "Top-posting"
Use this:
http://www.google.com/

Or read here:
http://en.wikipedia.org/wiki/Top-posting
http://www.netmeister.org/news/learn2quote.html
http://www.caliburn.nl/topposting.html

--
Thomas
Jul 19 '06 #20
Salvatore Di Fazio <sa***************@gmail.comwrote:
>
Alf P. Steinbach wrote:
>* Salvatore Di Fazio:
> [top-posting]

What part of "Please don't top-post" did you not understand?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

What means "Top-posting"
http://en.wikipedia.org/wiki/Top-posting

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 19 '06 #21

Marcus Kwok ha scritto:
>
http://en.wikipedia.org/wiki/Top-posting
okkey :>

Jul 19 '06 #22

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

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
6
by: Buddy Ackerman | last post by:
I created a simple class: Public Class MyTestClass Public Test() As String End Class I tried to assign some values to the array Test() and display them like this:
14
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
3
by: michi | last post by:
Hello, I need to initialize a 2 dimensional square arrays of structures. The size of array I get from the user. I can do one-dimensional array, but I don't know how to specify the size of array...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
7
by: Rade | last post by:
Please have a look at the following program: #include <iostream> template <const int array, size_t index> class ArrayIndex { public: static const int value = array; };
4
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
6
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.