Results 1 to 9 of 9

Thread: Why is it saying this...

  1. #1
    NOMADMAN
    Guest

    Why is it saying this...

    I have a class called mouse. I'm tring to use the << friend. In my public section of the class declaration I have:
    Code:
    friend ostream & operator << (ostream & o, const mouse m);
    In the cpp for the definitions I have:
    Code:
    ostream & operator << (ostream & o, const mouse m)
    {
         // PRINT MOUSE
         o << "M";
    
         return o;
    }
    Then I get the error: binary '<<' : no operator defined which takes a right-hand operand of type 'class mouse' (or there is no acceptable conversion)

    I'm not sure if you put friend in from of the word ostream but when I tried I got 3 errors, including the first:
    '<<' : a friend function can only be declared in a class
    '<<' : 'void' function returning a value


    Anyone know whats going wrong? If you want I'll post the code, don't think its really nessicary seeing as I'm probably just missing something.

    NOMAD

  2. #2
    New Member
    Join Date
    Apr 2002
    Posts
    10
    hmm wondering too

  3. #3
    NOMADMAN
    Guest

    I'll post the code for the mouse

    I'll post the code for the mouse.h and mouse.cpp, also I have uploaded a zip with mouse.h, mouse.cpp, and main.cpp. assume maze works (cause it does).

    PHP Code:
    #include <iostream>
    using std:: ostream;
    using std::endl;

    #include "maze.h"
    //#include "screen.h"

    class mouse
    {
    protected:
        
    unsigned short x;
        
    unsigned short y;
        
    unsigned int steps;
        
    // N = 1; E = 2; S = 3; W = 4;
        
    unsigned int direction;
        
    char footprint;
        
    bool righthand;

        
    void turnLeft();
        
    void turnRight();
        
    void stepForward();

    public:
        
    mouse(maze<charmchar charfoot);
        
    void takeOneStep(maze<charm);
        
    bool AmIInTheExit(maze<charmunsigned short xunsigned short y);
        
    bool AmIInTheEntrance(maze<charmunsigned short xunsigned short y);
        
    //friend ostream & operator << (ostream & o, const mouse m);
        
    friend ostream operator << (ostream o, const mouse m);

    }; 
    Above is mouse.h

    PHP Code:
    #include "mouse.h"

    mouse::mouse(maze<charmchar charfoot)
    {    
        
    m.getEntrance(xy);
        
        
    steps 0;
        
    // N = 1; E = 2; S = 3; W = 4;
        
    direction 2;
        
    footprint charfoot;
        
    righthand true;
    }

    void mouse::turnLeft()
    {
        
    direction--;
        if (
    direction 1)
            
    direction 4;
    }

    void mouse::turnRight()
    {
        
    direction++;
        if (
    direction 4)
            
    direction 1;
    }

    void mouse::stepForward()
    {

    }

    void mouse::takeOneStep(maze<charm)
    {
        
    //What's the difference between this and stepForward()?
        
    switch (direction)
        {
        case 
    1:
            
            if ( !( 
    m.IsThisCellBlocked(x, ( y-)) ) )
                
    y--;
            break;
        case 
    2:
            if ( !( 
    m.IsThisCellBlocked(( x+), y-1) ) )
                
    x++;
            break;
        case 
    3:
            if ( !( 
    m.IsThisCellBlocked(x, ( y+)) ) )
                
    y++;
            break;
        case 
    4:
            if ( !( 
    m.IsThisCellBlocked(( x-), y-1) ) )
                
    x--;
            break;
        default:
            break;
        }
    }

    bool mouse::AmIInTheExit(maze<charmunsigned short xunsigned short y)
    {
        return 
    m.IsThisCellTheExit (xy);
    }

    bool mouse::AmIInTheEntrance(maze<charmunsigned short xunsigned short y)
    {
        return 
    m.IsThisCellTheEntrance (xy);
    }

    ostream operator << (ostream o, const mouse m)
    {

        
    //Clear screen and set
        //CURSOR AT TOP LEFT!!!
        //ClearScreen();

        // PRINT MOUSE
        
    cout << "M";

        return 
    o;
    }

    //End mouse.cpp 
    Above is mouse.cpp

    Anyway, hope someone can help cause I'm stumped. I just have a feeling its something stupid so keep your eyes open!

    NOMAD
    Attached Files Attached Files

  4. #4
    Zaei
    Guest
    I sliced out everything that had to do with the maze class, and simply compiled your mouse.cpp and .h with a skeleton main function, and there were no errors (gcc under Linux).

    Actually, I just figured out what your problem is =). Its this line:
    Code:
    cout << "M";
    cout isnt defined, because you dont import it from the std namespace. Either #include <iostream.h> instead of #include <iostream> or add:
    Code:
    using std::cout;
    to the top of the mouse.h file.

    It compiled for me because I always use iostream.h, which imports the std namespace.

    Z.

  5. #5
    NOMADMAN
    Guest
    I'm sorry, i don't understand you mean:
    #include <iostream>
    using namespace std;
    ???

    Also, I should have made the cout o right
    o << "M";

    I'm sorry I'm being so dense. You think you could post the mouse.cpp you got to work?

    Thanks once again Z!

    NOMAD

  6. #6
    Zaei
    Guest
    Just remove anything that has to do with maze, and change your include to #include <iostream.h>

    Z.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by Zaei
    Just remove anything that has to do with maze, and change your include to #include <iostream.h>

    Z.
    Not a good idea.

    Not all library vendors have iostream.h importing the new iostreams into the global namespace. In many, it's actually implemented separately.

    Therefore, never use iostream.h
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Zaei
    Guest
    Ok =).

    Z.

  9. #9
    NOMADMAN
    Guest
    I fixed it! Well rather than fixing it I compromised. I turned mouse into a template class and al seems well. Go figure! I tried what you said Z and it worked (compiled) but when I added the maze stuff back it reverted back t how it was. I guess something from maze, maybe its friend ostream, did something.

    Anyway, I'm happy it works now!!! Thanks Z and everyone else who have helped me!

    NOMAD!

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