CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2011
    Posts
    10

    Member function not found?

    Apparently the function bool send(unsigned char*, int) is not declared...but I am looking at it in the .h file. Or at least I think so...can someone point out to me what I am missing here?

    Code:
    #ifndef ROBOT_H
    
    #define ROBOT_H
    #include "rs232.h"
    
    
    
    class Robot
    
    {
    
    public:
    
        Robot(int, int);
    
        ~Robot();
    
        int& getPort();
        void setPort(int&);
    
        int& getBaudRate();
        void setBaudRate(int&);
    
        SerialConnect& getConnection();
    
        bool send(unsigned char*, int);
    
    private:
        int port;
        int baudrate;
        SerialConnect connection;
    
    };
    
    
    
    #endif
    Code:
    #include "Robot.h"
    
    
    Robot::Robot(int portNo, int br) : port(portNo), baudrate(br) {
    
        if(connection.OpenComport(port, baudrate))
            printf("no worked\n");
        else
            printf("worked\n");
    
    }   //END ROBOT()
    
    
    
    Robot::~Robot() {
    
        connection.CloseComport(port);
    
    }   //END ~ROBOT()
    
    
    SerialConnect& Robot::getConnection() {return connection;}
    
    int& Robot::getPort() {return port;}
    void Robot::setPort(int& p) {port = p;}
    
    int& Robot::getBaudRate() {return baudrate;}
    void Robot::setBaudRate(int& br) {baudrate = br;}
    
    
    bool Robot::send(unsigned char* bytes, int size) {
        if(connection.SendBuf(port, bytes, size) == -1)
            return false;
        else
            return true;
    }

    Whenever I compile with -
    g++ -o go rs232.c Robot.cpp main.cpp

    I get the error -
    Robot.cpp:24: error: no ‘bool Robot::send(unsigned char*, int)’ member function declared in class ‘Robot’

    Is it just me...or is the declaration right there in the .h file? I don't see how its not declared. Can anyone please point out something that is wrong? This has been giving me entirely too much trouble. Thanks for any replies.

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

    Re: Member function not found?

    How do we know that the .h file you're showing us is the one that is being compiled?

    To remove all doubts, create a single Robot.cpp file, copy and paste the Robot class declaration in that file, and compile.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2011
    Posts
    10

    Re: Member function not found?

    It compiles when I do that...why is making a difference when I separate it into two files?

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Member function not found?

    There are two common reasons why it compiles when you include the contents of a headerfile versus using an include statement. The one is that the file it includes is a different file (a second robot.h) which exists for any reason in an include folder which was used prefered by the compiler. The other is that you used the header protection macro ROBOT_H somewhere else, so that it wasn't FALSE and so the contents of the header was not included.

  5. #5
    Join Date
    Apr 2008
    Posts
    118

    Re: Member function not found?

    If you use the switch -E with g++, you will get out the code immediately after pre-processing, so you can see exactly what code is going to the compiler, and you'll know for sure if the header is being included properly or not.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Member function not found?

    First, as has been said, check to make sure you aren't using the ROBOT_H symbol anywhere else.

    Second, most IDEs have an option to "Open this file" if you right-click on a #include statement. This can be helpful in determining whether the version of robot.h being included is actually the one you think it is.

  7. #7
    Join Date
    Apr 2008
    Posts
    118

    Re: Member function not found?

    Quote Originally Posted by Lindley View Post
    Second, most IDEs have an option to "Open this file" if you right-click on a #include statement. This can be helpful in determining whether the version of robot.h being included is actually the one you think it is.
    Whilst this is true, it is not perfect; I know of occasions on at least one IDE when the IDE and the compiler actually had different opinions, so everything looked great in the IDE, but the pre-processor picked up a different header file.

    I can't remember which IDE this was, or even how long ago, though. I hope it's pretty unlikely, but if the IDE relies on whichever one the user specifically drags in with some kind of menu-based directory/file browser, and the compiler has a big chunky include search path, they could definitely disagree.

    This is not to knock Lindley's words; what he said is true - it can be helpful - but be aware that just because the IDE knows which one to use is no guarantee that the rest of the system agrees.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Member function not found?

    You'd think that if the user had added a header to the project, and the compile settings were causing a different header with the same name to be included, that would be an excellent thing for the IDE to issue a warning about.

    Unfortunately I'm not aware of any IDEs which do this (most just forward the warnings of the compiler).

  9. #9
    Join Date
    Jan 2011
    Posts
    10

    Re: Member function not found?

    Okay the problem was that there was a precompiled header Robot in the same directory. After I deleted that, it compiles fine. Thank you for the help!

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Member function not found?

    Ah yes, precompiled headers strike again.

    My advice: disable them. They aren't really useful until you get to really, really big project sizes.

  11. #11
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Member function not found?

    The VC compiler (at least including VS2005) used to search header files in the last (include) folder accessed thus ignoring any order of the include folders or making a difference between project folders and global folders. So, if you have folders A and B both containing x.h and the (pre)compiler lastly included b.h from folder B it would take the x.h from that folder too.

    The problem arised with a VC8 solution (former a VC6 workspace) containing about hundred projects which had - for historical reasons - one single include folder that took a copy of all the header files used in the project directories and what makes it easier to include headers of other projects. If you changed a header file in your project it could happen that an old header of the common header folder was included instead. Of course that could lead to very strange error cases where we didn't know what happened for long until we finally could spot the reason. We dropped the common header folder then and made sure that all header files were unique.

    As a result of that experience I always avoid to have multiple versions of the same header within the same source tree.

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