Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Do Not Understand why these three Files Will Not Compile

Reply
Thread Tools

Do Not Understand why these three Files Will Not Compile

 
 
KevinSimonson
Guest
Posts: n/a
 
      11-04-2010
I've created the three files below and have gotten them into Visual
Studio 2010 (version 10.0.30319.1). I had a whole bunch of
compilation errors, but I've got them all fixed but two in "Bug.cpp".
I've embedded the error messages into the code with square brackets to
make it easier to see what the messages are referring to. What I'm
trying to do is get an example of a non-static function being called
every ten seconds. All this program does is write the contents of a
"Bug" object to a different file each time "problem()" is called.
This isn't the real software problem I'm working on; the one I'm
working on is much more complex. But the part I'm having trouble with
with the real software problem is this bit where a call to
"SetTimer()" needs to call a non-static method. Anybody have any idea
why this code won't compile for me?

Kevin S

---
BugMain.cpp------------------------------------------------------------------
// BugMain.cpp : Defines the entry point for the console application.
//

#include "Bug.h"
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
Bug bg = new Bug( 123, 456.789);
bg.runIt();
return 0;
}
---
Bug.h------------------------------------------------------------------------
#pragma once

class Bug
{
private:
int abc;
double def;

public:
Bug ( int ab, double de);

void runIt ();

void problem ();

static void* pObject;
};
---
Bug.cpp----------------------------------------------------------------------
#include "Bug.h"
#include <windows.h>
#include <stdio.h>
#include <fstream>
using namespace std;

char fileName[ 12];

Bug::Bug ( int ab
, double de)
{
abc = ab;
def = de;
strcpy( fileName, "Bug_000.Txt");
}

void CALLBACK repeatable ( HWND hwnt
, UINT uMsg
, UINT idEvent
, DWORD dwTime)
{
Bug* bg = (Bug *) pObject; [Error: identifier "pObject" is
undefined]
bg->problem();
}

void Bug::runIt ()
{
pObject = this;
SetTimer( NULL, 10000, repeatable); [Error: argument of type
"void (__stdc
} all *)(HWND hwnt, UINT uMsg,
UINT idEv
ent, DWORD dwTime)" is
incompatible wi
void Bug:roblem () th parameter of type "UINT"]
{
ofstream outFile( fileName);
outFile << "abc == " << abc << " and def == " << def << "." << endl;
outFile.close();
int dgt;
for (dgt = 6; 3 < dgt && fileName[ dgt] == '9'; dgt--)
{ fileName[ dgt] = '0';
}
if (3 < dgt)
{ fileName[ dgt]++;
}
}
 
Reply With Quote
 
 
 
 
KevinSimonson
Guest
Posts: n/a
 
      11-04-2010
That last error message got kind of mangled. It refers to the line
where I call 'SetTimer( NULL, 10000, repeatable);' and should say,
'Error: argument of type "void (__stdcall *)(HWND hwnt, UINT uMsg,
UINT idEvent, DWORD dwTime)" is incompatible with parameter of type
"UINT"'.
 
Reply With Quote
 
 
 
 
Stuart Golodetz
Guest
Posts: n/a
 
      11-04-2010
On 04/11/2010 23:23, KevinSimonson wrote:
> I've created the three files below and have gotten them into Visual
> Studio 2010 (version 10.0.30319.1). I had a whole bunch of
> compilation errors, but I've got them all fixed but two in "Bug.cpp".
> I've embedded the error messages into the code with square brackets to
> make it easier to see what the messages are referring to. What I'm
> trying to do is get an example of a non-static function being called
> every ten seconds. All this program does is write the contents of a
> "Bug" object to a different file each time "problem()" is called.
> This isn't the real software problem I'm working on; the one I'm
> working on is much more complex. But the part I'm having trouble with
> with the real software problem is this bit where a call to
> "SetTimer()" needs to call a non-static method. Anybody have any idea
> why this code won't compile for me?
>
> Kevin S
>
> ---
> BugMain.cpp------------------------------------------------------------------
> // BugMain.cpp : Defines the entry point for the console application.
> //
>
> #include "Bug.h"
> #include "stdafx.h"
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> Bug bg = new Bug( 123, 456.789);
> bg.runIt();
> return 0;
> }
> ---
> Bug.h------------------------------------------------------------------------
> #pragma once
>
> class Bug
> {
> private:
> int abc;
> double def;
>
> public:
> Bug ( int ab, double de);
>
> void runIt ();
>
> void problem ();
>
> static void* pObject;
> };
> ---
> Bug.cpp----------------------------------------------------------------------
> #include "Bug.h"
> #include<windows.h>
> #include<stdio.h>
> #include<fstream>
> using namespace std;
>
> char fileName[ 12];
>
> Bug::Bug ( int ab
> , double de)
> {
> abc = ab;
> def = de;
> strcpy( fileName, "Bug_000.Txt");
> }
>
> void CALLBACK repeatable ( HWND hwnt
> , UINT uMsg
> , UINT idEvent
> , DWORD dwTime)
> {
> Bug* bg = (Bug *) pObject; [Error: identifier "pObject" is
> undefined]
> bg->problem();
> }
>
> void Bug::runIt ()
> {
> pObject = this;
> SetTimer( NULL, 10000, repeatable); [Error: argument of type
> "void (__stdc
> } all *)(HWND hwnt, UINT uMsg,
> UINT idEv
> ent, DWORD dwTime)" is
> incompatible wi
> void Bug:roblem () th parameter of type "UINT"]
> {
> ofstream outFile( fileName);
> outFile<< "abc == "<< abc<< " and def == "<< def<< "."<< endl;
> outFile.close();
> int dgt;
> for (dgt = 6; 3< dgt&& fileName[ dgt] == '9'; dgt--)
> { fileName[ dgt] = '0';
> }
> if (3< dgt)
> { fileName[ dgt]++;
> }
> }


Well you're off-topic in comp.lang.c++, but judging by this:

http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

It looks like you're passing three arguments to SetTimer when it expects
four. The result of that is that you're trying to pass uElapse (10000 in
your case) as nIDEvent, and lpTimerFunc (repeatable in your case) as
uElapse. Since repeatable is a callback function, not a UINT, you get
the error shown.

Suggested fix would be to pass in a suitable nIDEvent. Perhaps 0, since
you don't want to replace an existing timer, as in:

SetTimer(NULL, 0, 10000, repeatable);

Hope that fixes it -- it's a bit "off the top of my head", but in case
it helps.

---> And please don't post Windows-specific messages to comp.lang.c++
where they're not on-topic <---

Cheers,
Stu
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      11-05-2010
KevinSimonson wrote:
> int _tmain(int argc, _TCHAR* argv[])


For the record: This is a non-standard MSC-ism.

> Bug bg = new Bug( 123, 456.789);
> bg.runIt();


Please go back to your C++ and Java textbooks, and compare the syntax used
to instantiate objects. This here is Java syntax, but you are writing C++.
Generally, in C++ you don't use "new", unless you have a reason to.

> #pragma once


Another non-portable construct, just for the record.

> class Bug
> {

[...]
> static void* pObject;
> };


The class (not instances thereof) contains a pointer-to-void named pObject.

> char fileName[ 12];


Look up strings (std::string, std::wstring, std::basic_string<T>).

> void CALLBACK repeatable ( HWND hwnt
> , UINT uMsg
> , UINT idEvent
> , DWORD dwTime)
> {
> Bug* bg = (Bug *) pObject; [Error: identifier "pObject" is
> undefined]
> bg->problem();
> }


1. Don't use C-style casts. I guess that either a static cast or a
reinterpret_cast would be better here.
2. The compiler sees "pObject" and tries to look up that name. A bit
simplified, it first looks at local variables, then at the function
parameters and then at globals. There is no "pObject" in either of them,
the only "pObject" is inside class "Bug". If you mean that,
use "Bug:Object".

Generally, but that is now specific to the use of the win32 API, you should
try to avoid passing parameters via "globals" like above. Instead, find out
which of the callback parameters you can use to pass context info to it.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      11-05-2010
On Thu, 4 Nov 2010 16:23:16 -0700 (PDT), KevinSimonson
<(E-Mail Removed)> wrote:

>I've created the three files below and have gotten them into Visual
>Studio 2010 (version 10.0.30319.1). I had a whole bunch of
>compilation errors, but I've got them all fixed but two in "Bug.cpp".
>I've embedded the error messages into the code with square brackets to
>make it easier to see what the messages are referring to. What I'm
>trying to do is get an example of a non-static function being called
>every ten seconds. All this program does is write the contents of a
>"Bug" object to a different file each time "problem()" is called.
>This isn't the real software problem I'm working on; the one I'm
>working on is much more complex. But the part I'm having trouble with
>with the real software problem is this bit where a call to
>"SetTimer()" needs to call a non-static method. Anybody have any idea
>why this code won't compile for me?
>
>Kevin S
>
>---
>BugMain.cpp------------------------------------------------------------------
>// BugMain.cpp : Defines the entry point for the console application.
>//
>
>#include "Bug.h"


Bad practice. Don't include your headers before your project's master
header.

>#include "stdafx.h"
>
>int _tmain(int argc, _TCHAR* argv[])
>{
> Bug bg = new Bug( 123, 456.789);
> bg.runIt();
> return 0;
>}
>---
>Bug.h------------------------------------------------------------------------
>#pragma once
>
>class Bug
>{
>private:
> int abc;
> double def;
>
>public:
> Bug ( int ab, double de);
>
> void runIt ();
>
> void problem ();
>
> static void* pObject;
>};
>---
>Bug.cpp----------------------------------------------------------------------
>#include "Bug.h"
>#include <windows.h>
>#include <stdio.h> <--- delete this
>#include <fstream>

#include <string>

>using namespace std;
>
>char fileName[ 12];


Are you writing C or C++? Forget char exists.

string fileName;

>
>Bug::Bug ( int ab
> , double de)
>{
> abc = ab;
> def = de;
> strcpy( fileName, "Bug_000.Txt"); <-- no!


Are you writing C?

fileName = "Bug_000.Txt";

>}
>


>void CALLBACK repeatable ( HWND hwnt
> , UINT uMsg
> , UINT idEvent
> , DWORD dwTime)
>{
> Bug* bg = (Bug *) pObject; [Error: identifier "pObject" is
>undefined]
> bg->problem();
>}
>


Now you are writing C to the Windows API but trying to instantiate an
object directly.

>void Bug::runIt ()
>{
> pObject = this;
> SetTimer( NULL, 10000, repeatable); [Error: argument of type


Invalid number of arguments to the function.

>"void (__stdc
>} all *)(HWND hwnt, UINT uMsg,
>UINT idEv
> ent, DWORD dwTime)" is
>incompatible wi
>void Bug:roblem () th parameter of type "UINT"]
>{
> ofstream outFile( fileName);
> outFile << "abc == " << abc << " and def == " << def << "." << endl;
> outFile.close();
> int dgt;
> for (dgt = 6; 3 < dgt && fileName[ dgt] == '9'; dgt--)
> { fileName[ dgt] = '0';
> }
> if (3 < dgt)
> { fileName[ dgt]++;
> }
>}


You are trying to use Windows messages, (WM_TIMER) in a console
application without a WindowProc and without a message loop. A console
application doesn't receive these kinds of messages and your timer
callback will never be called.

 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      11-05-2010
Other people have pointed out various things, but Ithink I have
spotted a couple of extra problems:

On Nov 4, 11:23*pm, KevinSimonson <(E-Mail Removed)> wrote:
> Anybody have any idea
> why this code won't compile for me?


> BugMain.cpp----------------------------------------------------------------*--
> // BugMain.cpp : Defines the entry point for the console application.
> //
>
> #include "Bug.h"
> #include "stdafx.h"


I'm not an expert, but I think that VC++ will actually ignore anything
before #include "stdafx.h" and so the stuff in Bug.h won't actually
get included.

> ---
> Bug.h----------------------------------------------------------------------*--
> #pragma once
>
> class Bug
> {
> private:
> * * *int abc;
> * double def;
>
> public:
> * Bug ( int ab, double de);
>
> * void runIt ();
>
> * void problem ();
>
> * static void* pObject;};


(note that pObject is therefore a member variable of this class)

>
> ---
> Bug.cpp--------------------------------------------------------------------*--


<snip>

> void CALLBACK repeatable ( *HWND hwnt
> * * * * * * * * * * * * *, *UINT uMsg
> * * * * * * * * * * * * *, *UINT idEvent
> * * * * * * * * * * * * *, DWORD dwTime)
> {
> * Bug* bg = (Bug *) pObject; * * * * *[Error: identifier "pObject" is
> undefined]


Here you are not in a member function, so there is no variable
pObject. (And, yes, this is the sort of problem that *is* on-topic in
comp.lang.c++.)

Hope this helps.
Paul.
 
Reply With Quote
 
Stuart Golodetz
Guest
Posts: n/a
 
      11-05-2010
On 05/11/2010 10:59, Paul N wrote:
> Other people have pointed out various things, but Ithink I have
> spotted a couple of extra problems:
>
> On Nov 4, 11:23 pm, KevinSimonson<(E-Mail Removed)> wrote:
>> Anybody have any idea
>> why this code won't compile for me?

>
>> BugMain.cpp----------------------------------------------------------------*--
>> // BugMain.cpp : Defines the entry point for the console application.
>> //
>>
>> #include "Bug.h"
>> #include "stdafx.h"

>
> I'm not an expert, but I think that VC++ will actually ignore anything
> before #include "stdafx.h" and so the stuff in Bug.h won't actually
> get included.
>
>> ---
>> Bug.h----------------------------------------------------------------------*--
>> #pragma once
>>
>> class Bug
>> {
>> private:
>> int abc;
>> double def;
>>
>> public:
>> Bug ( int ab, double de);
>>
>> void runIt ();
>>
>> void problem ();
>>
>> static void* pObject;};

>
> (note that pObject is therefore a member variable of this class)
>
>>
>> ---
>> Bug.cpp--------------------------------------------------------------------*--

>
> <snip>
>
>> void CALLBACK repeatable ( HWND hwnt
>> , UINT uMsg
>> , UINT idEvent
>> , DWORD dwTime)
>> {
>> Bug* bg = (Bug *) pObject; [Error: identifier "pObject" is
>> undefined]

>
> Here you are not in a member function, so there is no variable
> pObject. (And, yes, this is the sort of problem that *is* on-topic in
> comp.lang.c++.)
>
> Hope this helps.
> Paul.


Aye, that one's definitely on-topic I think I scrolled down too fast
and only saw the second problem.

Cheers,
Stu
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-06-2010
["Followup-To:" header set to comp.lang.c++.]

On Fri, 2010-11-05, Geoff wrote:
> On Thu, 4 Nov 2010 16:23:16 -0700 (PDT), KevinSimonson

....
>>BugMain.cpp------------------------------------------------------------------
>>// BugMain.cpp : Defines the entry point for the console application.
>>//
>>
>>#include "Bug.h"

>
> Bad practice. Don't include your headers before your project's master
> header.
>
>>#include "stdafx.h"
>>


Why would that be a bad practice? I (and others) believe it's a good
practice, because it ensures that "Bug.h" includes everything it
needs, so you don't get cascading build failures as you remove an
#include somewhere else.

(I don't know what a "project's master header" is, but I assume things
like <iostream> and <vector> fit the description.)

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Charlie Gibbs
Guest
Posts: n/a
 
      11-08-2010
In article <(E-Mail Removed)>,
(E-Mail Removed) (Jorgen Grahn) writes:

> ["Followup-To:" header set to comp.lang.c++.]
>
> On Fri, 2010-11-05, Geoff wrote:
>
>> On Thu, 4 Nov 2010 16:23:16 -0700 (PDT), KevinSimonson

>...
>>> BugMain.cpp---------------------------------------------------------
>>> // BugMain.cpp : Defines the entry point for the console application.
>>>/ /
>>>
>>> #include "Bug.h"

>>
>> Bad practice. Don't include your headers before your project's master
>> header.
>>
>>> #include "stdafx.h"

>
> Why would that be a bad practice? I (and others) believe it's a good
> practice, because it ensures that "Bug.h" includes everything it
> needs, so you don't get cascading build failures as you remove an
> #include somewhere else.


My procedure would be to move the #include "stdafx.h" (and
any other system includes) inside Bug.h - which will then
include _everything_ it needs.

--
/~\ http://www.velocityreviews.com/forums/(E-Mail Removed)lid (Charlie Gibbs)
\ / I'm really at ac.dekanfrus if you read it the right way.
X Top-posted messages will probably be ignored. See RFC1855.
/ \ HTML will DEFINITELY be ignored. Join the ASCII ribbon campaign!

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-10-2010
On Mon, 2010-11-08, Charlie Gibbs wrote:
> In article <(E-Mail Removed)>,
> (E-Mail Removed) (Jorgen Grahn) writes:
>
>> ["Followup-To:" header set to comp.lang.c++.]
>>
>> On Fri, 2010-11-05, Geoff wrote:
>>
>>> On Thu, 4 Nov 2010 16:23:16 -0700 (PDT), KevinSimonson

>>...
>>>> BugMain.cpp---------------------------------------------------------
>>>> // BugMain.cpp : Defines the entry point for the console application.
>>>>/ /
>>>>
>>>> #include "Bug.h"
>>>
>>> Bad practice. Don't include your headers before your project's master
>>> header.
>>>
>>>> #include "stdafx.h"

>>
>> Why would that be a bad practice?


[Someone elsewhere explained that was a Microsoft-specific advice,
having to do with their precompiled headers or something.]

>> I (and others) believe it's a good
>> practice, because it ensures that "Bug.h" includes everything it
>> needs, so you don't get cascading build failures as you remove an
>> #include somewhere else.

>
> My procedure would be to move the #include "stdafx.h" (and
> any other system includes) inside Bug.h - which will then
> include _everything_ it needs.


But that would make Bug.h include everything BugMain.cpp needs, which
is probably a whole lot more than Bug.h itself needs. A lot of people
(me, for example) aren't willing to pay the price for that.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Three Mobile --> Skype on three (Non-three [Symbian - Nokia] handsets) Harry Stottle UK VOIP 0 01-05-2010 08:59 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Opinions concerning these three ultracompacts? Anthony Buckland Digital Photography 0 12-05-2003 12:02 AM



Advertisments