Thread: Calling a Method from Another Class...

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    Unhappy Calling a Method from Another Class...

    Alright well I have been trying for a while and have had no luck to be able to do this. I removed the unimportant code and left just what is needed. I'm just going to post the two classes because that's pretty much all that is involved in this.

    So the problem is with the method getWholesale in the cPRODUCT class. I'm trying to get it to return the value into the cINVENTORY class and make it equal to the variable fWholesaleCost in the addProduct method (this is all where the ? are )

    Code:
    //Removed all the beginning code, end code and unimportant methods
    
    class cPRODUCT
    {
    	private:
    		long nSerialNum;
    		string ProductDesc;
    		float fWholesale;
    		float fRetailcost;
    		int nInStock;
    	public:
    		cPRODUCT(void) 
    		{ 
    			nSerialNum = 0;
    			ProductDesc = "";
    			fWholesale = 0;
    			fRetailcost = 0;
    			nInStock = 0;
    		}
    		float getWholesale(void)
    		{
    		        return fWholesale;
    		}
    Code:
    //Again, I removed the beginningcode, end code and other methods that weren't important
    
    class cINVENTORY
    {
    	private: 
    		cPRODUCT Products[100];
    		short nNumProdTypes;
    		float fTotalWhole;
    		float fTotalRetail;
    		int nOverallItems;
    	public:
    		cINVENTORY(void)
    		{
    			Products[100];
    			nNumProdTypes = 0;
    			fTotalWhole = 0;
    			fTotalRetail = 0;
    			nOverallItems = 0;
    		}
    		void addProduct(void)
    		{
                            fWholesaleCost = ??????????.getWholesale();
    
    		}
    If you guys need any more info, let me know...but I think this is pretty much it (the barebones).

    Thanks for any help you can give...trying to get this part to work but it just isn't working out for me,

    ...Dan

  2. #2
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    Code:
    fWholesaleCost = ??????????.getWholesale();
    In order to call a function in the class cPRODUCT you have to place an instance of that class on the left hand side.

    Code:
    fWholesaleCost = Products[?].getWholesale();
    Where the ? mark is the desired product object in the array you want. i.e. the index between 0 - 99.

    Hope this helps alittle.

    P.S. You will get an undefined variable error on
    Code:
    fWholesaleCost = Products[?].getWholesale();
    because the variable fWholesaleCost is never defined, but I'm sure you knew that and probably edited out wherever you defined it.
    Last edited by Kurisu; 02-02-2006 at 06:31 PM.

  3. #3
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Jeez, I can't believe I didn't see that, lol. Thanks a lot.

    ...Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inline templated class method as standalone function in namespace
    By monikersupreme in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2008, 11:38 AM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Calling a Function From A Class
    By Okiesmokie in forum C++ Programming
    Replies: 8
    Last Post: 06-28-2002, 10:09 PM