Thread: Yet another simple class question...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    22

    Yet another simple class question...

    Well I have this program for a binary tree, and up to this point I've never used two classes in the same program. So basically I just need to know how to call a member function of one class from a different class. Here's the header files for both classes:

    Code:
    #include <string>
    #include <fstream>
    #include "ItemType.h"
    struct TreeNode;
    
    
    class TreeType
    {
    public:
      TreeType();                     // constructor
     ~TreeType();                    // destructor
      TreeType(const TreeType& originalTree); 
      void operator=(const TreeType& originalTree);
      // copy constructor
      void MakeEmpty();
      bool IsEmpty() const;
      bool IsFull() const;
      int LengthIs() const; 
      void RetrieveItem(ItemType& item, bool& found);
      void InsertItem(ItemType item);
      void DeleteItem(ItemType item);
      void Print(std::ostream& outFile) const;
      void TreeType::IncrementItem(ItemType& item, bool& found);
    private:
      TreeNode* root;
    };
    
    
    #include <iostream>
    using std::istream;
    using std::ostream;
    
    class ItemType
    {
    	friend ostream & operator <<(ostream & q, const ItemType & data);
    
    public:
    	ItemType();
    	ItemType(char c, int i);
    	bool operator<( const ItemType & ) const;
    	bool operator>( const ItemType &) const;
    	void inc(void);
    private:
    	char l;
    	int tally;
    };
    Basically itemtype contains the character that is in each node of the tree, and tally which is the number of times an attempt has been made to insert that character into the tree. Now I have this function which looks to see if the item exists in the tree, and if it does it increments tally... That's where I run into my problem.

    Code:
    void Increment(TreeNode* tree, 
         ItemType& item, bool& found);
    
    void TreeType::IncrementItem(ItemType& item, bool& found)
    // Calls recursive function Retrieve to search the tree for item.
    {
      Retrieve(root, item, found);
    }
    
    
    void Increment(TreeNode* tree, 
         ItemType& item, bool& found)
    // Recursively searches tree for item.
    // Post: If there is an element someItem whose key matches item's,
    //       found is true and item is set to a copy of someItem; 
    //       otherwise found is false and item is unchanged.
    {
      if (tree == NULL)
        found = false;                     // item is not found.
      else if (item < tree->info)      
        Increment(tree->left, item, found); // Search left subtree.
      else if (item > tree->info)
        Increment(tree->right, item, found);// Search right subtree.
      else
      {
        item = tree->info;                 // item is found.
    	ItemType::inc();
    	found = true;
       }
    }
    here is the inc member function of ItemType:

    Code:
    void ItemType::inc()
    {
    	tally++;
    }
    VSDN gives me an error saying "error C2352: 'ItemType::inc' : illegal call of non-static member function".

    How do I change the value of tally if I can't call a non-static member function?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I think ItemType::inc(); should be item.inc(); since you are calling inc() on the particular instance of ItemType named item.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The function isn't static, so you cannot call it as if it were. You need to create an object of the ItemType class to call one of its public member functions. Without looking harder at your code, perhaps this is what you want:
    Code:
    item.inc();
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    22
    Indeed. I had a brain fart. Sorry for the stupid question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  3. class and pointer question
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2006, 10:00 AM
  4. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  5. Simple Question, I think
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2002, 10:36 AM