-
December 18th, 2006, 05:20 PM
#1
Problem with '<<'
Hi, I've this code and I don't know how fix the Error: Member function must be called or its address taken in function main():
Code:
#include <iostream>
#include <fstream>
#include <ostream>
using namespace std;
struct box {
high;
width;
diagonal;
};
int main() {
int i;
int j;
box make[8][8];
ofstream thing ("sample.xxx", ios::out | ios::binary);
thing.write << make[i][j] = 128; // Here is error
}
plz.help.me
Last edited by Brad Jones; December 19th, 2006 at 11:11 AM.
-
December 18th, 2006, 05:22 PM
#2
Re: Problem with '<<'
Please go back and edit your post, and use code tags. This will make it actually readable and prevent your code from getting little smiley faces. I will take a look at it then.
In the meantime
1) "make[i][j]" is referencing an instance of your object which you are tring to assign a value (128) too.....
2) You have not shown your functions for streaming objects of your type (typically friend functions
3) Your definition of the struct is not valid code
Last edited by TheCPUWizard; December 18th, 2006 at 05:25 PM.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 18th, 2006, 05:34 PM
#3
Re: Problem with '<<'
yes I'll make code tags but I don't know how, I'm junior here.
I don't have friends functions
Code:
struct box {
unsigned short h;
unsigned short w;
unsigned short d;
};
Last edited by Brad Jones; December 19th, 2006 at 11:12 AM.
-
December 18th, 2006, 05:52 PM
#4
Re: Problem with '<<'
Originally Posted by style
Code:
thing.write << make[i][j] = 128; // Here is error
This line of code makes no sence. First, you cannot assign "128" to your struct. Second, you don't overload the "<<" operator to allow the struct to be written into the stream. Third, you are calling the 'write' function incorrectly.
Viggy
-
December 18th, 2006, 06:19 PM
#5
Re: Problem with '<<'
I've changed this line and now it's :
Code:
box.write ((char*) &color_box,sizeof(color_box)) << make[i][j] = 8;
and the error is : 'operator<<' not implemented in type 'ostream' for arguments of type 'box' in function main();
.
-
December 18th, 2006, 06:24 PM
#6
Re: Problem with '<<'
What are you trying to do with the '<<' operator? Typically, this operator is used to write stuff into a stream:
Code:
#include <iostream>
int main()
{
std::cout << "Hello World!" << endl;
}
However, you are calling the "write" funciton directly, so there is no need for the '<<' operator.
Viggy
-
December 18th, 2006, 06:42 PM
#7
Re: Problem with '<<'
because I need to use the '<<' operator
-
December 18th, 2006, 07:14 PM
#8
Re: Problem with '<<'
Originally Posted by style
because I need to use the '<<' operator
What book, online tutorial, website, etc. has ever used operator << in the way you're doing it? The answer is probably "none". If there is one, please point out where it's being used the way you're using it.
Is it something you just made up? So what are you trying to accomplish? Please don't just say "I need to use operator <<" -- tell us what you're trying to accomplish.
Regards,
Paul McKenzie
-
December 18th, 2006, 11:22 PM
#9
Re: Problem with '<<'
....
thing.write << make[i][j] = 128; // Here is error
I lost you on this line here.
It doesn't make any sense.
You can't just assign your struct the value of 128. Also, you already use the write() function, so what is the need for <<?
If you want to use << that's fine, but you need to overload it for your struct. And if you do use it, then don't use write(). Pick one or the other.
What are you trying to do?
Please rate my post if you felt it was helpful
-
December 19th, 2006, 02:58 AM
#10
Re: Problem with '<<'
Originally Posted by style
because I need to use the '<<' operator
Simple. Just- locate the key labelled < on your keyboard
- press it twice
-
December 19th, 2006, 04:05 AM
#11
Re: Problem with '<<'
Originally Posted by treuss
Simple. Just - locate the key labelled < on your keyboard
- press it twice
Use with shift key..
Seriously, "style" can you please explain in your own words what you mean by this statement:
Code:
box.write ((char*) &color_box,sizeof(color_box)) << make[i][j] = 8;
I mean to ask - what you tried achieving.. what do you expect that to do and what are the various things written there and why did you write it so.. just trying to understand the underlying idea.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
December 19th, 2006, 05:19 AM
#12
Re: Problem with '<<'
Originally Posted by exterminator
Use with shift key..
It depends on the keyboard configuration.
With my french keyboard, I mustn't press shift (maj).
style:
thing is an ofstream.
It looks like you're trying to output an object of type box on this ofstream.
make[i][j] being of type box.
However I really can't understand what you're trying to do when assiging 128 to a box... A box is a structure containing several fields...
It's a bit like assigning 42 to a square. That doesn't make sense.
Assuming you'll assign something sensible to make[i][j] elsewhere in the program, here is a mean to output a box (without operator overloading):
Code:
void Output(ostream& out, const box& data) {
out << "[" << data.w << " " << data.h << " " << data.d << "]";
}
Then, you can write out your box like that:
Code:
Output(thing, make[i][j]);
If you want to use a more friendly syntax, you can overload operator <<, saying that the left hand operand is of type ostream& and the right hand operand is of type box.
Code:
ostream& operator <<(ostream& out, const box& data) {
return out << "[" << data.w << " " << data.h << " " << data.d << "]";
}
The returned value is of type ostream&, and is the parameter passed to the function so that you can use an expression calling this operator as a left operand of another expression using this operator or an overload of this operator.
Like that:
Code:
(thing << make[i][j]) << "\n";
Parentheses are not necessary since operator<< has a left-to-right associativity.
The statement above, is equivalent to:
Code:
operator << (operator << (thing, make[i][j]), "\n");
Which will be, in practice, equivalent to:
Code:
operator << (thing, make[i][j]);
operator << (thing, "\n");
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
December 19th, 2006, 09:06 AM
#13
Re: Problem with '<<'
Code:
thing.write << make[i][j] = 128; // Here is error
Firstly operator << has a higher precedence to operator = so it parses as
Code:
(thing.write << make[i][j]) = 128;
if you want it the other way round you must put in parentheses
thing is not declared and we don't know what write is either. Assuming it is a stream-type, it does not make sense to assign it to 128.
To get this to compile you would need to:
1. Have thing and write defined (to be some stream)
2. overload operator<< with the first parameter of the type of thing.write (or something it derives from) and the second parameter of type box.
3. Get operator precedence right by putting in parentheses
4. Overload operator= for box to take an integer as a parameter, if this makes sense.
-
December 21st, 2006, 10:34 AM
#14
Re: Problem with '<<'
Originally Posted by NMTop40
Code:
thing.write << make[i][j] = 128; // Here is error
Firstly operator << has a higher precedence to operator = so it parses as
Code:
(thing.write << make[i][j]) = 128;
if you want it the other way round you must put in parentheses
thing is not declared and we don't know what write is either. Assuming it is a stream-type, it does not make sense to assign it to 128.
To get this to compile you would need to:
1. Have thing and write defined (to be some stream)
2. overload operator<< with the first parameter of the type of thing.write (or something it derives from) and the second parameter of type box.
3. Get operator precedence right by putting in parentheses
4. Overload operator= for box to take an integer as a parameter, if this makes sense.
Could You paste an example code with "overload operator<<"?
-
December 21st, 2006, 11:02 AM
#15
Re: Problem with '<<'
Originally Posted by style
Could You paste an example code with "overload operator<<"?
Code:
#include <iostream>
using namespace std;
class rect {
public:
rect( int w, int h ): w_(w),h_(h) {}
protected:
int w_;
int h_;
friend ostream & operator << ( ostream & ostr, const rect & r );
};
ostream & operator << ( ostream & ostr, const rect & r ) {
ostr << r.w_ << "," << r.h_;
return ostr;
}
int main() {
rect r( 3,5 );
cout << r << endl;
return 0;
}
output:
Kurt
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|