Thread: compiler error on class constructor

  1. #1
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594

    compiler error on class constructor

    I'm trying to build a class called Tile. The class will hold a position for drawing the tile to the screen, and a string array to hold the image (the image has to be in ascii characters). I'm using Microsft.Net Visual C++ 2003.

    Here's the code from my .h file:
    Code:
    #ifndef _TILE_H_
    #define _TILE_H_
    
    
    const int T_WIDTH = 5; // width of the tile
    const int T_HEIGHT = 4;// height of the tile
    
    //---------------------------------------------------------
    struct Position
    {
    	int X;
    	int Y;
    
    	Position & operator = (Position rhs)
    	{
    		X = rhs.X;
    		Y = rhs.Y;
    		return *this;
    	}
    
    	Position(int x, int y)
    	{
    		X = x;
    		Y = y;
    	}
    };
    
    //---------------------------------------------------------
    class Tile
    {
    public:
    	Tile(int xPos, int yPos, string* img);
    	~Tile();
    
    	int X() const;
    	int Y() const;
        
    	void Draw() const;
    
    	void MoveUp();
    	void MoveDown();
    	void MoveLeft();
    	void MoveRight();
    
    private:	
    	Position myPos;
    	string* myImg;
    };
    
    #endif
    The error occurs on the constructor..."error C2062: type 'int' unexpected."
    I don't have a function named Tile anywhere else in my program. I'm not sure about any libraries the compiler includes, but if there is it should overload it anyway. I suspect there may be something wrong with my position struct.

    Any help is greatly appreciated.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>The error occurs on the constructor
    ... which you haven't posted for us. So, here's some working code for your to look at:
    Code:
    #include <iostream>
    
    struct Position
    {
      int X;
      int Y;
    
      //Position& operator = (const Position &rhs)
      //{
      //  /* This function is not needed,
      //   * the default assignment operator
      //   * will do the same job
      //   */
      //	X = rhs.X;
      //	Y = rhs.Y;
      //	return *this;
      //}
      Position (int x, int y) : X(x), Y(y) {}
    };
    
    class C
    {
    private:
      Position  myPos;
    
    public:
      C(int A, int B) : myPos(A, B) { };
      int X(void) const
      {
        return(myPos.X);
      }
    
      int Y(void) const
      {
        return(myPos.Y);
      }
    };
    
    int main(void)
    {
      C c1(1, 2), c2(3, 4);
      
      std::cout << "c1: " << c1.X() << " " << c1.Y() << std::endl;
      std::cout << "c2: " << c2.X() << " " << c2.Y() << std::endl;
      
      c1 = c2;
    
      // Prove assignment worked:
      std::cout << "c1: " << c1.X() << " " << c1.Y() << std::endl;
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    I did post the constructor

    Tile(int xPos, int yPos, string* img);


    That is the line the error occurs on. The code in the .cpp file for the class is fine. I tried your code, and it still does not work when I add a string* parameter. Maybe there is something wrong with my configuration.

    Thank you for pointing out the = operator.

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Could you post more code, including definitions?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    the problem is solved. I don't know what was wrong, but I restarted from a backup file of my program, and this time I went the right direction apperently. I don't know what I did exactly, but it was probably a missplaced comma or something like that. Thanks for the initiative to help though guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  5. Virtual Base Class & Constructor :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 03:14 PM