Thread: Class - Not Recognizing Constructor

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    361

    Question Class - Not Recognizing Constructor

    I have the following in my Board.h file:
    Code:
    class CBoard
    {
    public:
    	CBoard();
    	~CBoard();
    	int ReturnTile(int X, int Y);
    protected:
    	int Grid[20][20];
    };
    Now, when I go to Board.cpp, after including the above .h file, and I type "CBoard::", the pop-up menu that appears only contains: Grid, ReturnTile, and ~CBoard. But no Constructor.

    When I try to run the program, I get the error:
    error C2228: left of '.ReturnTile' must have class/struct/union type

    For the following code (Indicated at the *s):
    Code:
    	CBoard MyBoard();
    
    	int TileReturn;
    	for (int XX = 0; XX <= 20; XX++)
    	{
    		for (int YY = 0; YY <= 20; YY++);
    		{
    			TileReturn = MyBoard.ReturnTile(XX, YY); //*****************
    		}
    	}
    And incase you're wondering, all that the ReturnTile code is:
    Code:
    int CBoard::ReturnTile(int X, int Y)
    {
    	return Grid[X][Y];
    }
    But again, I think this all links back to the constructor and it not being recognized. But if you see something else that's causing the problem, I'm all ears
    Thanks for reading.

    Erik

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Did you actually write a constructor? Just because the popmenu doesn't show it doesn't mean you can't type it out yourself

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >CBoard MyBoard();
    That's not an object, it's a function called MyBoard returning a Cboard object. Remove the parentheses and it might work better for you.

    >for (int XX = 0; XX <= 20; XX++)
    This would be a buffer overflow. You want your loops to look like this:
    Code:
    for (int XX = 0; XX < 20; XX++)
    The same goes for your YY loop too.

    >for (int YY = 0; YY <= 20; YY++);
    Do you see the semicolon there at the end? It means your code is broken. This loop won't do much at all as it is, so you should remove the semicolon.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Thanks, I changed around the For Loops, and yes, I did write out the constructor, but your question made me look a bit closer and I had:
    Code:
    CBoard::CBoard();
    {
    //Insert Code
    }
    With a ";" After it. Which, as lots of you would probably point it, doesn't belong there It's all running smoothly now Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM