• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 747
  • Last Modified:

Rectangle class error: Type Redefinition

I have a Rectangle class that I've created but when I try to use it I get compile errors:
   error C2923: 'Ref' : 'Rectangle' is not a valid template type argument for parameter 'T'.
   error C3203: 'Ref' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
   error C2371: 'RectRef' : redefinition; different basic types; see declaration of 'RectRef'

When I do a check it seems to me that there is a function BOOL Rectangel(HDC hdc, int left, int top, int right, int bottom) that is causing the problem because the compiler is treating my class as a Redefinition of the type Rectangle. How do I get around this obstacle? I'm using MS Visual C++ 2008 IDE.

In the code snippet template class Ref is basically a smart pointer (similar to std::auto_ptr).

class Rectangle;
typdef Ref<Rectangle> RectRef;
 
class Rectangle
{
   //some code
};

Open in new window

0
mkandre
Asked:
mkandre
  • 4
  • 4
  • 3
  • +1
2 Solutions
 
williamcampbellCommented:
typdef  typo  should be typedef
0
 
williamcampbellCommented:
Can you show us the Ref class
0
 
williamcampbellCommented:
Sorry ... not understanding the problem ..

You could use a namspace around your Rectangle class

namespace MyStuff
{

 class Rectangle
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
evilrixSenior Software Engineer (Avast)Commented:

class Rectangle;
typedef Ref<class Rectangle> RectRef; // <-- tell compiler the template type is a class of type Rectangle
 
class Rectangle
{
   //some code
};

Open in new window

0
 
mkandreAuthor Commented:
@evilrix:

adding the keyword class solved the problem with the template, thanks. However, I'm getting the following error:
   error C2061: syntax error : identifier 'Rectangle'

whenever I attempt to instantiate a rectangle object.

I'm thinking it's the same type conflict with the Rectangle function.

Is the Rectangle function a standard function or a MS specific?

//Recall that RectRef is a typedef for Ref<Rectangle>
//Ref<T> takes a pointer of type T
 
RectRef Component::getBounds()
{
   // x, y width & height are data members of Component
   return new Rectangle(x, y, width, height);
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
You've forward declare the class... before you can instantiate an instance of it though the compiler needs to see the definition of the class, which I guess is defined in a header somewhere. You can only define pointers and references with a forward declaration, to instantiate the object or to be able to call methods on a referece/pointer to the object the compiler needs that definition.

Of course, you might still get an issue with the class/function name clash. You should really fix this either by renaming the function, the class or using namespaces as suggested above by Willian.
0
 
mkandreAuthor Commented:
Sorry I probably should have been clearer.

The class is properly declared and defined in it's own header and source files respectively, and the .h is #included in the the Component.cpp source file that is now being compiled.

you may notice that even in the original snippet I showed that the class was defined below the RectRef typedef. It was only forward declared so that the RectRef type could be used withing the class declaration itself.
#include "Rectangle.h"
 
// some Component member functions here
 
RectRef Component::getBounds()
{
   // x, y width & height are data members of Component
   return new Rectangle(x, y, width, height);
}
 
// some more member functions here.

Open in new window

0
 
mkandreAuthor Commented:
>>Of course, you might still get an issue with the class/function name clash. You should really fix this either >>by renaming the function, the class or using namespaces as suggested above by Willian.

I did not create the Rectangle function. It seems to have been included with the compiler's library. That's why I was asking the Rectangle function is a C++ standard or a Microsoft Visual C++ specific inclusion.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> That's why I was asking the Rectangle function is a C++ standard or a Microsoft Visual C++ specific inclusion.
Well, it's certainly NOT a standard C++ function as defined by the ANSI C++03 standard. For me to assist you more I'd really need to see the full source code.
0
 
DanRollinsCommented:
Rectangle is a Win322 API function:
   http://msdn.microsoft.com/en-us/library/dd162898(VS.85).aspx
it draws a rectangle in the specified DC with the location and size with the current GDI pen.
Most programs will #include Windows.H which includes WinGDI.h  -- so your class object is going to collide with a lot of source code.  Same problem if you call it RECT or other commonly-defined structure.
Just rename your class to MyRect or RectPlus or something.  Avoid thousands of headaches to come.
0
 
mkandreAuthor Commented:
Ok thanks a lot...I appreciate the help.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Anytime.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now