Thread View: be.comp.programming
3 messages
3 total messages
Started by Jim
Tue, 08 Jun 2004 11:52
Trying to overload the << operator using C++
Author: Jim
Date: Tue, 08 Jun 2004 11:52
Date: Tue, 08 Jun 2004 11:52
34 lines
1274 bytes
1274 bytes
I am trying to overload the << operator to a bag that is a class.
container is a bag.
class bag;
bags_to_use = 12;
bag container[bags_to_use]; // A declared bag of arrays.
So you have a structure that is an array initialized of 12 containers.
I would think that the correct implementation of overloading the
operator would be something of the following:
ostream& operator <<(ostream& outs, const bag& container[bags_to_use])
{
stuff I will write later.
}
but I think that I may be wrong? Is my syntax wrong?
thanks,
Jim
Re: Trying to overload the << operator using C++
Author: Aquila
Date: Wed, 09 Jun 2004 05:32
Date: Wed, 09 Jun 2004 05:32
23 lines
1795 bytes
1795 bytes
Jim wrote:
> bag container[bags_to_use];��//�A�declared�bag�of�arrays.
>
>
>
you don't want to write an output operator for just this array right?
> ostream& operator <<(ostream& outs, const bag& container[bags_to_use])
> {
>
> stuff I will write later.
>
> }
>
> but I think that I may be wrong?��Is�my�syntax�wrong?
>
I don't think it's possible to have a variable array size in a definition...
if it does work: did you put this operator in your class? what errors do you
get?
--
"May the source be with you"
Re: Trying to overload the << operator using C++
Author: Matata
Date: Wed, 09 Jun 2004 19:13
Date: Wed, 09 Jun 2004 19:13
38 lines
2510 bytes
2510 bytes
On Wed, 09 Jun 2004 07:32:21 +0200, Aquila wrote:
> Jim wrote:
>
>> bag container[bags_to_use];��//�A�declared�bag�of�arrays.
>>
>>
>>
>
> you don't want to write an output operator for just this array right?
>
>> ostream& operator <<(ostream& outs, const bag& container[bags_to_use])
>> {
>>
>> stuff I will write later.
>>
>> }
>>
>> but I think that I may be wrong?��Is�my�syntax�wrong?
>>
> I don't think it's possible to have a variable array size in a definition...
> if it does work: did you put this operator in your class? what errors do you
> get?
It is perfectly possible. An array is a pointer to the first element in
c++. Just leave the [], and do some pointer arithmetic...
ostream& operator <<(ostream& outs, bag* container);
It is though easier to use a vector, and traverse with iterators.
vector <bag> container;
ostream& operator <<(ostream& outs, const vector<bag>& container);
If you place the << operator in your class declaration you will need a
friend with ostream. I don't think it is necessary if you don't place it
in the declaration of your bag class.
Greetz Matata
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads