8 Replies - 1290 Views - Last Post: 29 June 2011 - 11:40 AM Rate Topic: -----

#1 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

'Rectangle' is not at type error

Posted 29 June 2011 - 08:07 AM

I've been banging my head up against the wall for the past 2 hours trying to get this to work. I've searched many places and have come up with no solutions.

I have a class named GraphicsRenderer, and a class named Rectangle. There is a static function in GraphicsRenderer that takes a Rectangle as a parameter. I've made sure that Rectangle.h is included in GraphicsRenderer, but it still gives me the error "'Rectangle' is not a type". I've tried a forward declaration of Rectangle inside of GraphicsRenderer, but that gave me the same error. I've also tried putting Rectangle all inside of one header file, but that didn't help either.

I think it's very weird because my Vector2 class works just fine inside of GraphicsRenderer, and Vector2 is set up the same way as Rectangle.

The error points to the function prototype of DrawSprite(SDL_Surface *sprite, SDL_Surface *destination, Vector2 position, Rectangle source)

Thanks for any help in advance.

Here's my code:

GraphicsRenderer.h
#ifndef GRAPHICSRENDERER_H
#define GRAPHICSRENDERER_H

#include "SDL/SDL.h"
#include "Rectangle.h"
#include "Vector2.h"

class GraphicsRenderer
{

public:

    GraphicsRenderer() { }
    ~GraphicsRenderer() { }

    static void ClearScreen(SDL_Surface *screen, Uint32 color);

    static void DrawSprite(SDL_Surface *sprite, SDL_Surface *destination, Vector2 position);
    static void DrawSprite(SDL_Surface *sprite, SDL_Surface *destination, Vector2 position, Rectangle source);
};


void GraphicsRenderer::ClearScreen(SDL_Surface *screen, Uint32 color)
{
    SDL_FillRect(screen, &screen->clip_rect, color);
}

void GraphicsRenderer::DrawSprite(SDL_Surface *sprite, SDL_Surface *destination, Vector2 position)
{
    SDL_Rect offset;

    offset.x = (int)position.X;
    offset.y = (int)position.Y;

    SDL_BlitSurface(sprite, NULL, destination, &offset);
}

void GraphicsRenderer::DrawSprite(SDL_Surface *sprite, SDL_Surface *destination, Vector2 position, Rectangle source)
{
    SDL_Rect offset;
    SDL_Rect sdlSource;

    offset.x = (int)position.X;
    offset.y = (int)position.Y;

    sdlSource.x = source.X;
    sdlSource.y = source.Y;
    sdlSource.w = source.Width;
    sdlSource.h = source.Height;

    SDL_BlitSurface(sprite, &sdlSource, destination, &offset);
}


#endif




Rectangle.h
/*
  Author: Fib
  Date: May 11, 2011
*/

#ifndef RECTANGLE_H
#define RECTANGLE_H

#include <string>
#include <sstream>
#include "Random.h"

/*
  Class: Point
  Description: Class definitions that represents a point in 2D space.
*/
class Point
{
public:
    int X, Y;

    Point();
    Point(int, int);
    ~Point() { };

    void Zero();

    std::string ToString();
};


/*
  Class: Rectangle
  Description: Class definitions that represents a rectangle.
*/
class Rectangle
{
public:
    int X, Y, Width, Height;

    Rectangle();
    Rectangle(int, int, int, int);
    ~Rectangle() { }

    bool Contains(int, int);
    bool Contains(Point&);
    bool Contains(Point[], int);
    int Top();
    int Bottom();
    int Left();
    int Right();
    Point Center();
    Point RandomPointInRect();
    void Location(int, int);
    bool IsEmpty();
    void Empty();
    void Offset(int, int);

    int Area();

    bool Intersects(Rectangle&);
    Rectangle Overlap(Rectangle&);

    static Rectangle Union(Rectangle&, Rectangle&);

    std::string ToString();
};

#endif



Rectangle.cpp
/*
  ****************************************************************
  * Classes contained within this file
  ****************************************************************
  * Point
  * Rectangle
  ****************************************************************
  * Note: The complexity of each function is constant time, O(1)
  *       unless otherwise specified by the function header.
  ****************************************************************
*/

/*
  Author: Fib
  Date: May 11, 2011
  Class: Point
  Description: Class definitions that represents a point in 2D space.

  ****************************************************************
  * Public Members
  ****************************************************************
  *   Name                     Description
  ****************************************************************
  * X                       The X component of the point
  * Y                       The Y component of the point
  ****************************************************************


  ****************************************************************
  * Public Functionality
  ****************************************************************
  *   Function                 Description
  ****************************************************************
  * Zero                   Sets the X and Y component to zero
  * ToString               Returns a string representation of the components of the point
  ****************************************************************
*/

#include "Rectangle.h"

// ****************************************************************
// * Constructor.  Initializes both components to zero.
// ****************************************************************
Point::Point()
{
    X = 0;
    Y = 0;
}

// ****************************************************************
// * Constructor.  Initializes X and Y to a value.
// ****************************************************************
Point::Point(int x, int y)
{
    this->X = x;
    this->Y = y;
}

// ****************************************************************
// * Sets both components to zero.
// ****************************************************************
void Point::Zero()
{
    X = 0;
    Y = 0;
}

// ****************************************************************
// * Returns a string representation of the current instance.
// *
// * Returns: The string that represents the point.
// ****************************************************************
std::string Point::ToString()
{
    std::stringstream ss(std::stringstream::in | std::stringstream::out);

    ss << "(" << X << ", " << Y << ")";

    return ss.str();
}




/*
  Author: Fib
  Date: January 25, 2011
  Class: Rectangle
  Description: Class definitions that represents a rectangle.

  ****************************************************************
  * Public Members
  ****************************************************************
  *   Name                     Description
  ****************************************************************
  * X                       The X position of the rectangle
  * Y                       The Y position of the rectangle
  * Width                   The width of the rectangle
  * Height                  The height of the rectangle
  ****************************************************************


  ****************************************************************
  * Public Functionality
  ****************************************************************
  *   Function                 Description
  ****************************************************************
  * Contains                Overloaded.  Determines if the point(s) is contained within the rectangle
  * Top                     Returns where the top of the rectangle is, which is the Y component
  * Bottom                  Returns where the bottom of the rectangle is, which is the Y + Height
  * Left                    Returns where the left of the rectangle is, which is the X component
  * Right                   Returns where the right of the rectangle is, which is the X + Width
  * Center                  Returns a point that is the center of the rectangle
  * RandomPointInRect       Returns a random point inside of the rectangle
  * Location                Moves the rectangle to a specified point
  * IsEmpty                 Determines if the rectangle's width and height components are zero
  * Empty                   Sets the rectangle's width and height to zero
  * Offset                  Moves the rectangle by a specified amount
  * Area                    Returns the area of the rectangle
  * Intersects              Determines if the rectangle is intersecting with another rectangle
  * Overlap                 Returns a rectangle with its components set to the overlapping portion of two intersecting rectangles
  * Union                   Returns a rectangle that exactly contains two other rectangles
  * ToString                Returns a string representation of the components of the rectangle
  ****************************************************************
*/

// ****************************************************************
// * Constructor.  Initializes all components to zero.
// ****************************************************************
Rectangle::Rectangle()
{
    X = 0;
    Y = 0;
    Width = 0;
    Height = 0;
}

// ****************************************************************
// * Constructor.  Initializes all components to a value.
// ****************************************************************
Rectangle::Rectangle(int x, int y, int width, int height)
{
    X = x;
    Y = y;
    Width = width;
    Height = height;
}

// ****************************************************************
// * Determines if the rectangle contains an (x, y) value.
// *
// * Returns: True if the (x, y) value is inside the rectangle,
// *          false otherwise.
// ****************************************************************
bool Rectangle::Contains(int X, int Y)
{
    return (X > this->X && X < (this->X + Width) && Y > this->Y && Y < (this->Y + Height));
}

// ****************************************************************
// * Determines if the rectangle contains a Point.
// *
// * Returns: True if the Point is inside the rectangle,
// *          false otherwise.
// ****************************************************************
bool Rectangle::Contains(Point& p)
{
    return (p.X > this->X && p.X < (this->X + Width) && p.Y > this->Y && p.Y < (this->Y + Height));
}

// ****************************************************************
// * Determines if the rectangle contains all points in the array.
// *
// * Returns: True if each Point in the array is inside of the
// *          rectangle, false otherwise.
// *
// * Complexity: Worst Case:   O(N)
// *             Average Case: ????
// *             Best Case:    O(1)
// ****************************************************************
bool Rectangle::Contains(Point p[], int size)
{
    for (int i = 0; i < size; i++)
        if (!Contains(p[i]))
            return false;

    return true;
}

// ****************************************************************
// * Calculates the top of the rectangle.
// *
// * Returns: A value that represents the top of the rectangle.
// ****************************************************************
int Rectangle::Top()
{
    return Y;
}

// ****************************************************************
// * Calculates the bottom of the rectangle.
// *
// * Returns: A value that represents the bottom of the rectangle.
// ****************************************************************
int Rectangle::Bottom()
{
    return (Y + Height);
}

// ****************************************************************
// * Calculates the left side of the rectangle.
// *
// * Returns: A value that represents the left of the rectangle.
// ****************************************************************
int Rectangle::Left()
{
    return X;
}

// ****************************************************************
// * Calculates the right side of the rectangle.
// *
// * Returns: A value that represents the right of the rectangle.
// ****************************************************************
int Rectangle::Right()
{
    return (X + Width);
}

// ****************************************************************
// * Calculates the center of the rectangle.
// *
// * Returns: A Point that represents the center of the rectangle.
// ****************************************************************
Point Rectangle::Center()
{
    Point temp;

    temp.X = (this->X + Width) / 2;
    temp.Y = (this->Y + Height) / 2;

    return temp;
}

// ****************************************************************
// * Calculates a random point inside of the rectangle.
// *
// * Returns: A Point with random x and y values that will be
// *          inside of the rectangle.
// ****************************************************************
Point Rectangle::RandomPointInRect()
{
    Point temp;
    Random rand;

    // I use X + 1 and Width - 1 for the params because I only want values
    // that are INSIDE of the rectangle, not on the boundary.  Same for the Y.
    temp.X = rand.GetRandomInt(this->X + 1, Width - 1);
    temp.Y = rand.GetRandomInt(this->Y + 1, Height - 1);

    return temp;
}

// ****************************************************************
// * Sets the location of the rectangle.
// *
// * Returns: Nothing.
// ****************************************************************
void Rectangle::Location(int x, int y)
{
    X = x;
    Y = y;
}

// ****************************************************************
// * Determines if the width and height of the rectangle is zero.
// *
// * Returns: True if the width and height are both zero, false
// *          otherwise.
// ****************************************************************
bool Rectangle::IsEmpty()
{
    return (Width == 0 && Height == 0);
}

// ****************************************************************
// * Sets each component of the rectangle to zero.
// *
// * Returns: Nothing.
// ****************************************************************
void Rectangle::Empty()
{
    X = 0;
    Y = 0;
    Width = 0;
    Height = 0;
}

// ****************************************************************
// * Moves the rectangle from its current position by a value.
// *
// * Returns: Nothing.
// ****************************************************************
void Rectangle::Offset(int x, int y)
{
    X += x;
    Y += y;
}

// ****************************************************************
// * Calculates the area of the rectangle.
// *
// * Returns: The area in pixels.
// ****************************************************************
int Rectangle::Area()
{
    return Width * Height;
}

// ****************************************************************
// * Determines if the current rectangle is intersecting, or being
// * contained, or contains another rectangle.
// *
// * Returns: True if there is an intersection or containment
// *          of any kind, false otherwise.
// ****************************************************************
bool Rectangle::Intersects(Rectangle& other)
{
    return !(Left() > other.Right() || Right() < other.Left() || Top() > other.Bottom() || Bottom() < other.Top());
}

// ****************************************************************
// * Creates a Rectangle with its components set to the overlapping
// * portion of an intersection with another rectangle.  If there
// * is no intersection, all components of the given rectangle will
// * be set to zero.
// *
// * Returns: A Rectangle of the overlapping portion of the intersection
// ****************************************************************
Rectangle Rectangle::Overlap(Rectangle& other)
{
    Rectangle temp;

    if(Intersects(other))
    {
        if (Left() > other.Left())
            temp.X = Left();
        else
            temp.X = other.Left();

        if (Top() > other.Top())
            temp.Y = Top();
        else
            temp.Y = other.Top();

        if (Right() < other.Right())
            temp.Width = Right() - temp.X;
        else
            temp.Width = other.Right() - temp.X;

        if (Bottom() < other.Bottom())
            temp.Height = Bottom() - temp.Y;
        else
            temp.Height = other.Bottom() - temp.Y;

        return temp;
    }
    else
        return temp;;
}

// ****************************************************************
// * Creates the smallest Rectangle that completely contains two
// * other rectangles.
// *
// * Returns: A Rectangle that completely contains two other rectangles.
// ****************************************************************
Rectangle Rectangle::Union(Rectangle& value1, Rectangle& value2)
{
    Rectangle temp;

    if (value1.X < value2.X)
        temp.X = value1.X;
    else
        temp.X = value2.X;

    if (value1.Y < value2.Y)
        temp.Y = value1.Y;
    else
        temp.Y = value2.Y;

    if (value1.Right() > value2.Right())
        temp.Width = value1.Right() - temp.X;
    else
        temp.Width = value2.Right() - temp.X;

    if (value1.Bottom() > value2.Bottom())
        temp.Height = value1.Bottom() - temp.Y;
    else
        temp.Height = value2.Bottom() - temp.Y;

    return temp;
}


// ****************************************************************
// * Returns a string representation of the current instance.
// *
// * Returns: The string that represents the rectangle.
// ****************************************************************
std::string Rectangle::ToString()
{
    std::stringstream ss(std::stringstream::in | std::stringstream::out);

    ss << "(" << X << ", " << Y << ", " << Width << ", " << Height << ")";

    return ss.str();
}


This post has been edited by Fib: 29 June 2011 - 08:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: 'Rectangle' is not at type error

#2 Elliotd123  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 37
  • Joined: 24-January 11

Re: 'Rectangle' is not at type error

Posted 29 June 2011 - 08:16 AM

Somewhere you are trying to create a Rectangle object before you've declared the Rectangle class. Are there more functions in your project? Like perhaps you did something in a main() or WinMain() function that wants a rectangle...

In fact my guess is that it has to do with the order that you are including your headers. You may have a header that creates a Rectangle object before a header that defines the Rectangle class.

This post has been edited by Elliotd123: 29 June 2011 - 08:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: 'Rectangle' is not at type error

Posted 29 June 2011 - 08:22 AM

Thanks for the quick reply.

No, nothing in my main. I'm only trying to use it GraphicsRenderer and that's it.

I know what the error is telling me, I just don't understand how to fix it. I'm including Rectangle.h and I even tried a forward declaration. So I just don't understand why the compiler can't find my Rectangle class.

This post has been edited by Fib: 29 June 2011 - 08:23 AM

Was This Post Helpful? 0
  • +
  • -

#4 Elliotd123  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 37
  • Joined: 24-January 11

Re: 'Rectangle' is not at type error

Posted 29 June 2011 - 08:28 AM

Which Rectangle gives the compiler error?
Was This Post Helpful? 0
  • +
  • -

#5 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: 'Rectangle' is not at type error

Posted 29 June 2011 - 08:48 AM

In GraphicsRenderer, the error points to this line:

static void DrawSprite(SDL_Surface *sprite, SDL_Surface *destination, Vector2 position, Rectangle source);


Was This Post Helpful? 0
  • +
  • -

#6 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: 'Rectangle' is not at type error

Posted 29 June 2011 - 09:48 AM

I just tried this and got the same error as you mentioned.
#include <string>
#include <sstream>
#include "windows.h"


class Rectangle
{
public:
    int X, Y, Width, Height;
};

class check {
    static void test(Rectangle rec) {
    }
};
int main() {

    return 0;
}


Basically, Windows API already defined Rectangle as a function. So the only solution is to rename your class.
[Me thinks C programming needs namespaces :D]

This post has been edited by Munawwar: 29 June 2011 - 09:49 AM

Was This Post Helpful? 1
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2132
  • View blog
  • Posts: 3,263
  • Joined: 21-June 11

Re: 'Rectangle' is not at type error

Posted 29 June 2011 - 10:14 AM

View PostMunawwar, on 29 June 2011 - 06:48 PM, said:

[Me thinks C programming needs namespaces :D]


True, but luckily C++ does have namespaces. So if the OP puts all his classes into a namespace, he can call them whatever he wants without having to worry about name conflicts with any C libraries.
Was This Post Helpful? 3
  • +
  • -

#8 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: 'Rectangle' is not at type error

Posted 29 June 2011 - 10:53 AM

Never knew that. I thought namespaces could only solve naming conflicts between each other. So I tried the following and it works :):
#include "windows.h"

namespace myspace {
    class Rectangle
    {
    public:
        int X, Y, Width, Height;
    };

    class check {
        static void test(Rectangle rec) {
        }
    };
}
int main() {
    return 0;
}


This post has been edited by Munawwar: 29 June 2011 - 10:54 AM

Was This Post Helpful? 1
  • +
  • -

#9 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: 'Rectangle' is not at type error

Posted 29 June 2011 - 11:40 AM

Omg THAT was the problem! Why wouldn't it give me the error "Class being redefined" instead of "is not a type" then?

Well it works now. I'm definitely going to put all my classes in a namespace now.

Thank you very much for the help Munawwar.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1