help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why doesn't code from TYC++in21days work?


From: Guy Harrison
Subject: Re: Why doesn't code from TYC++in21days work?
Date: Thu, 05 Aug 2004 13:00:23 GMT
User-agent: KNode/0.7.7

Gerald Lafreniere wrote:

[snip]

> I try to compile it just like it is, so coming from a text book on the
> topic, it should be right, right?

It is right. Ultimately this is down to M$. They can't write headers, most
especially C++ ones. I'll explain this one as a google reveals no proper
answer as to the cause (but you're not alone - as a search on DoDrawRect
reveals).
 
> I get 32 errors in the compiler log.
> 
> ERROR LOG(source is further down the page)
> 
> Compiler: Default compiler
> Executing  g++.exe...
> g++.exe "C:\c\4eWir1.cpp" -o "C:\c\4eWir1.exe"
> -I"C:\Dev-Cpp\include\c++"  -I"C:\Dev-Cpp\include\c++\mingw32"
> -I"C:\Dev-Cpp\include\c++\backward"
> -I"C:\Dev-Cpp\lib\gcc-lib\mingw32\3.2\include"  -I"C:\Dev-Cpp\include"
> -L"C:\Dev-Cpp\lib"
> C:/c/4eWir1.cpp:46: variable or field `DoDrawRect' declared void
> 
> C:/c/4eWir1.cpp:46: invalid conversion from `BOOL (*)(HDC__*, int, int,
> int,
>    int)' to `int'
^^^
Hello M$. <iostream> is a standard header but at end of day it has to talk
to the OS and that means <iostream> pulling in WinAPI headers behind the
scenes. There may be DevCpp options to reduce this impact but that's
something you'll have to ask DevCpp. Suffice it to say, for whatever
reason, by the time g++ encounters...

void DoDrawRect(Rectangle)

...the WinAPI junk has polluted the "global namespace" with a function
declaration...

BOOL WINAPI Rectangle(HDC,int,int,int,int);     //<wingdi.h>

...so instead of DoDrawRect() taking your Rectangle class as an argument (as
it should), g++ has been told it's taking that WinAPI Rectangle function as
the argument instead. Functions can't be passed verbatim, they "decay" to
pointers (like arrays). Additionally, an (HDC) is really an (HDC__*) so g++
is seeing...

void DoDrawRect( BOOL(*)(HDC__*,int,int,int,int) );

...the WinAPI Rectangle function has obscured the Rectangle class at this
point and as "undeclared things" default to "int" this is why g++ is
moaning about not being able to convert that WinAPI function into an 'int'.

You can either go through the code and change your Rectangle class to a
different name or do a quick workaround as follows...

> #include <iostream>
> using namespace std;

namespace app {

> enum CHOICE { DrawRect = 1, GetArea, GetPerim,
>  ChangeDimensions, Quit};
[snip] 
> int DoMenu();
> void DoDrawRect(Rectangle);
> void DoGetArea(Rectangle);
> void DoGetPerim(Rectangle);
> 
> int main ()
^^^
int Main()

> {
>    // initialize a rectangle to 30,5
>    Rectangle theRect(30,5);
[snip] 
>  void DoGetArea(Rectangle theRect)
>  {
>    cout << "Area: " <<  theRect.GetArea() << endl;
>  }
> 
>  void DoGetPerim(Rectangle theRect)
>  {
>    cout << "Perimeter: " <<  theRect.GetPerim() << endl;
>  }

}       //namespace app

int main()
{
 return app::Main();
}
 
> 
> Thanks for any help on this.

If even a fraction of that makes sense you're doing well. Don't worry about
anything you don't understand. It shouldn't have happened. Most conflicts
are reasonably obvious. It's simply M$ habit of perpetually giving the same
thing different names that makes for most of the obscurity.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]