Const qualifier problem

This is a discussion on Const qualifier problem within the C++ Programming forums, part of the General Programming Boards category; This is a constructor for a binary tree that creates a copy of the tree given as argument.... Code: Tree(const ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    8

    Const qualifier problem

    This is a constructor for a binary tree that creates a copy of the tree given as argument....

    Code:
    Tree(const Tree<T>& aTree){
    		this->ghost = new Node<T>();
    		
    		if(aTree.treeIsEmpty()){
    			this->root = this->ghost;
    			
    		}else{
    			this->root = new Node<T>(aTree.getRoot()->key, 0, this->ghost, this->ghost);
    			this->treeCopyFill(aTree, aTree.getRoot(), this->root);
    		}
    	}
    and its subroutine treeCopyFill...

    Code:
    void treeCopyFill(const Tree<T>& aTree, Node<T> * theOriginalFatherNode, Node<T> * theCopyFatherNode){
    
    		if(!aTree.isLeef(theOriginalFatherNode->left)){
    			theCopyFatherNode->left = new Node<T>(theOriginalFatherNode->left->key, theCopyFatherNode, this->ghost, this->ghost);
    
    			treeCopyFill(aTree, theOriginalFatherNode->left, theCopyFatherNode->left);
    		}
    
    		if(!aTree.isLeef(theOriginalFatherNode->right)){
    			theCopyFatherNode->right = new Node<T>(theOriginalFatherNode->right->key, theCopyFatherNode, this->ghost, this->ghost);
    			
                            treeCopyFill(aTree, theOriginalFatherNode->right, theCopyFatherNode->right);
    		}
    	}


    When I compile with GCC, I get the following error about 5 times....

    passing const Tree<T> as 'this' argument of return_type Tree<T>::method [with T=int] discards qualifier


    Can anyone explain what's happening and what can I do to solve the problem?
    Last edited by Lexidata; 10-02-2010 at 02:23 PM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Indent your code better and use the [CODE] tag (the # button)

    You should also compile with G++ not GCC though that is probably not the cause of your error

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Ok done, now, can you help me?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You have a const "Tree" variable somewhere that you call a function on. However, there is no const version of the function. Const versions are declared by adding "const" at the end, like so:
    Code:
    void Class:funcName() const;
    It informs the compiler the function is allowed to be called even if the object it is called on is const. It also informs the compiler that the "this" object will NOT be modified in this function.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    Thanks mate! I'll try it right away!

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    8
    It works thanks!
    I've had a lot of trouble with tose things in the past and always had to find another way to do what I wanted. Not only your answer was very helpful this time but since I can understand it clearly I'll be able to use it again in the future!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compile-time vs. runtime const multiplication
    By CodeMonkey in forum C++ Programming
    Replies: 5
    Last Post: 08-21-2010, 05:32 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Another problem with templates
    By robatino in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2006, 04:32 PM
  5. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21