Thread: Errors in passing a function my defined object

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Errors in passing a function my defined object

    Code:
    bool Board::placeShip(Ship whichShip)
    {
    	//1 == Horizontal;
    	//0 == Vertical;
    
    	short int pathOK = 0;
    	short int length = whichShip->getLength(m_whichPlayer);
    	short int xCoord = whichShip->getXCoords(m_whichPlayer);
    	short int yCoord = whichShip->getYCoords(m_whichPlayer);
    
    	if (whichShip->getOrientation(m_whichPlayer))
    	{	
    		for (short i = 0; i < length; i++)
    		{
    			if ((i + xCoord) > m_xWidth)
    			{
    				return 1; //Bad Placement
    			} else {
    				if (m_oceanArray[i + xCoord][yCoord] == 11)
    				{
    					if (pathOK == length)
    					{
    
    						m_oceanArray[i + xCoord]
    									[yCoord]
    									= whichShip->getNumber();
    
    						if (i == (length - 1))
    						{
    							return 0;
    						}
    					} else {
    
    						pathOK++;
    						if (pathOK == length)
    						{
    							i = -1;
    						} 
    
    					}
    
    				} else {
    					return 1; //Bad Placement
    				}
    			}
    		}
    
    
    	} else {
    
    		
    		for (short i = 0; i < length; i++)
    		{
    			if ((i + yCoord) > m_yHeight)
    			{
    				return 1; //Bad Placement
    			} else {
    				if (m_oceanArray[xCoord][i + yCoord] == 11)
    				{
    					if (pathOK == length)
    					{
    
    						m_oceanArray[xCoord]
    									[i + yCoord]
    									= whichShip->getNumber();
    
    						if (i == (length - 1))
    						{
    							return 0;
    						}
    					} else {
    
    						pathOK++;
    						if (pathOK == length)
    						{
    							i = -1;
    						} 
    
    					}
    
    				} else {
    					return 1; //Bad Placement
    				}
    			}
    		}
    	}
    	return 1;
    };
    I've been through, and through this code.. I don't know what to do, I'll be passing this function an object of type ship, and it will be a pointer.. but trying to access its member functions from within the class is giving me errors; how else could I do this?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What errors are you getting? If whichShip is a pointer shouldn't the parameter be a pointer -

    bool Board::PlaceShip(Ship* whichShip)

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>bool Board::PlaceShip(Ship whichShip)

    This looks like the func is recieveing an insance of a ship......not a pointer to one......

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    oops too late...

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Alright, fixed that, but I still get these errors...

    classdeclarations.cpp(202) : error C2819: type 'Ship' does not have an overloaded member 'operator ->'
    classdeclarations.cpp(202) : error C2227: left of '->setDamage' must point to class/struct/union
    classdeclarations.cpp(206) : error C2819: type 'Ship' does not have an overloaded member 'operator ->'
    classdeclarations.cpp(208) : error C2819: type 'Ship' does not have an overloaded member 'operator ->'
    classdeclarations.cpp(208) : error C2227: left of '->getNumber' must point to class/struct/union

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>type 'Ship' does not have an overloaded member 'operator ->'

    Again it seems you are trying to access class members of an instance with pointer notation.

    If the object is recieved by copy (which (Ship whichShip) would suggest), you access its data members like so

    whichShip.getLength(m_whichPlayer);

    You use -> when the recieved parameter is a pointer

  7. #7
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    As I said before, I changed
    (Ship whichShip) to (Ship *whichShip), therefore I should be using ->, shouldn't I?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  2. Passing object to function outside of loop
    By HLMetroid in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2008, 03:22 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM