Thread: Threads giving the following error: call of an object of a class type without appropr

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Threads giving the following error: call of an object of a class type without appropr

    I made a simple binary tree then decide to try out threads too. I got the following error:

    call of an object of a class type without appropriate operator or conversion

    Code:
    #include "Tree.h"
    #include <iostream>
    #include <thread>
    
    
    void main(void){
    
    
    	Tree tree;
    
    
    	for (int i = 0; i <= 25; i++)
    	{
    		tree.insert(i);
    	}
    
    
    	std::thread t1;
    	std::thread t2;
    
    
    	t1(tree.inorder());
    	t2(tree.inorder());
    
    
    	t1.join();
    	t2.join();
    	
    
    
    	system("PAUSE");
    	
    }
    Code:
    #include "Tree.h"
    #include <iostream>
    
    
    
    
    Tree::Tree()
    {
    	
    }
    
    
    
    
    Tree::~Tree()
    {
    }
    
    
    void Tree::insert(Node* leaf,int key){
    
    
    	if (key < leaf->num){
    
    
    		if (leaf->left != NULL){
    			insert(leaf->left, key);
    		}
    		else{
    			leaf->left = new Node;
    			leaf->left->num = key;
    			leaf->left->left = NULL;
    			leaf->left->right = NULL;
    		}
    	}
    	else if (key >= leaf->num){
    		
    		if (leaf->right != NULL){
    			insert(leaf->right, key);
    		}
    		else{
    			leaf->right = new Node;
    			leaf->right->num = key;
    			leaf->right->left = NULL;
    			leaf->right->right = NULL;
    		}
    	}
    
    
    }
    void Tree::insert(int num){
    
    
    	if (root != NULL){
    		insert(root,num);
    	}
    	else{
    		root = new Node();
    		root->num = num;
    		root->left = NULL;
    		root->right = NULL;
    	}
    
    
    }
    void Tree::search(int num){
    
    
    }
    void Tree::print(){
    
    
    }
    void Tree::inorder(Node* leaf){
    
    
    	if (leaf != NULL){
    		
    			inorder(leaf->left);
    			std::cout << leaf->num << "\n";
    			inorder(leaf->right);
    	}
    }
    void Tree::inorder(){
    
    
    	inorder(root);
    
    
    }
    void Tree::postorder(){
    
    
    
    
    }
    void Tree::preorder(){
    
    
    
    
    }
    I am having a hard time figuring out why the error exists. I tried adding the new operator but that did not work.

    advice?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >> t1(tree.inorder());
    >> t2(tree.inorder());
    First off, std::thread does not have an operator ().
    You need to use the constructor:

    std::thread t1(tree.inorder());
    std::thread t2(tree.inorder());

    Second, std::thread takes the address or function object that it shall start the new thread in. You haven't specified one.
    Finally, std::thread expects the callable object or function to take no parameters. Alternatively, you can pass arguments to the constructor, and then it will pass those along to the callable object.
    See std::thread.

    And a side note: main shall return int. That's final. main return void is an error in the standard which some compilers (erroneously) accept.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You should do:
    Code:
    std::thread t1([&](){tree.inorder();});
    If you construct two std::thread objects, you main thread sits idle here.
    So if you meant to have two 'threads' of execution, do something in main too.
    Last edited by manasij7479; 01-28-2015 at 03:39 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by manasij7479 View Post
    You should do:
    Code:
    std::thread t1([&](){tree.inorder();});
    If you construct two std::thread objects, you main thread sits idle here.
    So if you meant to have two 'threads' of execution, do something in main too.
    or:
    Code:
    std::thread t1(std::bind(&Tree::inorder, &tree));
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::bind is kind of deprecated by now. Lambdas are the way to go.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    std::bind is kind of deprecated by now. Lambdas are the way to go.
    As a general rule, I agree, but the C++ standards committee has not deprecated it, and in some cases, using bind makes for clearer, more readable code than a lambda, so it's still a useful tool. No reason to throw it away just yet.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Sorry I am not at my computer/compiler at the moment but I would like to thank you for the replies. @ Elysia you mentioned Lamda's. How would I accomplish this code in a Lamda? I am asking because I am very fond of them but I have to admit, Threads are something I am learning.

    @Elkvis, I would have never guess the solution you provided. I read several tutorials and none had remotely what you shown. I have not tried it and I am assuming it is right and it looked rather unique.

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    You would use lambdas in this context in the exact way post #3 does.

    This might also help : C++ Tutorial: C++11 Thread 2. Threading with Lambda Function - 2015

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    As a general rule, I agree, but the C++ standards committee has not deprecated it, and in some cases, using bind makes for clearer, more readable code than a lambda, so it's still a useful tool. No reason to throw it away just yet.
    I disagree on bind. It create for code that is harder to understand and maintain. Not to mention the horrible errors if you do something wrong. Lambdas are clearer and give clear errors. If you aren't using C++14, bind is still good for those generic functions (you don't need to specify the type), but in C++14, we have generic lambdas which do the same thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I disagree on bind. It create for code that is harder to understand and maintain.
    O_o

    I disagree with your disagreement.

    I think `std::bind', or equivalent, serves the documentation of intent in calling a single function more that the equivalent lambda.

    I think a lamda servers the documentation of intent in implementing extra logic than the equivalent extraneous function and `std::bind' call.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Elysia View Post
    And a side note: main shall return int. That's final. main return void is an error in the standard which some compilers (erroneously) accept.
    Not erroneously for the compilers. It's not standard, but compilers accept lots of non-standard code, intentionally.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by King Mir View Post
    Not erroneously for the compilers. It's not standard, but compilers accept lots of non-standard code, intentionally.
    Yes, but that means that the compiler is wrong, by definition. You should never write non-standard code, unless you really have to. This case is clearly not such a case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: expression must have pointer-to-object type
    By BarakaMaiseli in forum C Programming
    Replies: 7
    Last Post: 10-02-2013, 04:33 AM
  2. Declaring an object of class type. Giving errors, need help!
    By Kristyy_mariee in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2012, 11:51 AM
  3. Replies: 3
    Last Post: 12-31-2011, 02:23 AM
  4. Error: cannot call member function without object
    By TIMBERings in forum C++ Programming
    Replies: 7
    Last Post: 04-15-2010, 04:21 AM
  5. To Call an Object Outside of Class
    By marQade in forum C++ Programming
    Replies: 18
    Last Post: 11-25-2007, 05:36 PM