dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Weird Boost/Compiler Error

  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Exclamation Weird Boost/Compiler Error

    Hello,

    I'm trying to use Boost thread library. The problem when I compile I get an error saying that the thread I created is not a class/struct/union that I cannot use . operator to call the specified function. However before this point everything compiles fine. The thread object is created and no complains. The header files are there and can open. The weird part is that Visual C++ 9 is unable to auto list the members of the name space boost or the class thread. And when I go to definition it shows me a dialog box with different sets of boost namespace like 10s: boost::boost::boost....
    which makes no sense to me.

    I'm using Visual Studio 2008. Boost 1.38. (BoostPro pre-compiled binaries)

    Now I'm wondering if the compiler is broken, or the boost installation...

    Help appreciated.

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

    Re: Weird Boost/Compiler Error

    The weird part is that Visual C++ 9 is unable to auto list the members of the name space boost or the class thread. And when I go to definition it shows me a dialog box with different sets of boost namespace like 10s: boost::boost::boost....
    which makes no sense to me.
    Visual C++ Intellisense is just a tool -- it is not a compiler.

    If you have a compiler error, then it is a compiler error regardless of what Intellisense does or does not do. So if you actually post the code, then others can help.
    Now I'm wondering if the compiler is broken, or the boost installation...
    No one here will know until we see the code.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 2009
    Posts
    4

    Re: Weird Boost/Compiler Error

    #include <boost/thread/thread.hpp>

    void DorWork1()
    {

    }

    void DoWork2()
    {

    }


    class MyThread1
    {
    public:
    void operator()()
    {
    DoWork1();
    }
    };

    class MyThread2
    {
    public:
    void operator()()
    {
    DoWork2();
    }
    };


    int main()
    {
    boost::thread thrd1(MyThread1());
    boost::thread thrd2(MyThread2());

    thrd1.join(); // Error:thrd1 is not recognized as a class/struct/union
    thrd2.join(); // Error:thrd1 is not recognized as a class/struct/union

    return 0;
    }


  4. #4
    Join Date
    Aug 2009
    Posts
    4

    Re: Weird Boost/Compiler Error

    I forgot to mention that the Boost installation I have is the one provided by BoostPro. Could be the headers are corrupted?

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

    Re: Weird Boost/Compiler Error

    Quote Originally Posted by CodeZoro View Post
    I forgot to mention that the Boost installation I have is the one provided by BoostPro. Could be the headers are corrupted?
    No, it's not the headers -- it's your code.

    Look at this simplified example without Boost. It doesn't compile:
    Code:
    class MyThread1
    {
    };
    
    class thread
    {
      public:
        thread(MyThread1&);
        void join();
    };
    
          
    int main()
    {
       thread thrd1(MyThread1()); 
       thrd1.join(); 
       return 0;
    }
    All of those classes are dummy classes so as to illustrate that it isn't Boost.

    The problem is hard to explain, but the issue is that the line in red does not create an object. It really is a function definition. What's confusing is that the MyThread1() that you gave as an argument is really a parameter type -- that also doesn't create an object.

    So the fix is this:
    Code:
    class MyThread1
    {
    };
    
    
    class thread
    {
      public:
        thread(MyThread1&);
        void join();
    };
    
          
    int main()
    {
       MyThread1 t;
       thread thrd1(t);
       thrd1.join(); 
       return 0;
    }
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 24th, 2009 at 03:13 PM. Reason: Clarification: Function parameter is pointer-to-function

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

    Re: Weird Boost/Compiler Error

    All in all, the bottom line is that it is perfectly valid to declare functions that have types with parentheses. It basically boils down to a pointer-to-function type. See here:
    Code:
    class foo
    {
    };
    
    void myFunc(foo());  // declare a function that takes pointer-to-function returning a foo object.
    
    foo someFunc() { return foo(); }
    
    int main()
    {
       myFunc(someFunc);
    }
    Weird, but these are the very rare parts of C++ that no one really deals with until they have code that looks like your original example.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Aug 2009
    Posts
    4

    Re: Weird Boost/Compiler Error

    Thanks for your informative reply. I should read more about these mysterious situations...

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)