CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Dec 2006
    Posts
    27

    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.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  3. #3
    Join Date
    Dec 2006
    Posts
    27

    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.

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Problem with '<<'

    Quote 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

  5. #5
    Join Date
    Dec 2006
    Posts
    27

    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();


    .

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    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

  7. #7
    Join Date
    Dec 2006
    Posts
    27

    Re: Problem with '<<'

    because I need to use the '<<' operator

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with '<<'

    Quote 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

  9. #9
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    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

  10. #10
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Problem with '<<'

    Quote Originally Posted by style
    because I need to use the '<<' operator
    Simple. Just
    • locate the key labelled < on your keyboard
    • press it twice

  11. #11
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Problem with '<<'

    Quote 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.

  12. #12
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Problem with '<<'

    Quote 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()!

  13. #13
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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.

  14. #14
    Join Date
    Dec 2006
    Posts
    27

    Re: Problem with '<<'

    Quote 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<<"?

  15. #15
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Problem with '<<'

    Quote 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:
    Code:
    3,5
    Kurt

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured