Do Not Understand why these three Files Will Not Compile

Discussion in 'C++' started by KevinSimonson, Nov 4, 2010.

  1. 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::problem () 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]++;
    }
    }
    KevinSimonson, Nov 4, 2010
    #1
    1. Advertising

  2. 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"'.
    KevinSimonson, Nov 4, 2010
    #2
    1. Advertising

  3. 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::problem () 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/library/ms644906(VS.85).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
    Stuart Golodetz, Nov 4, 2010
    #3
  4. 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::pObject".

    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
    Ulrich Eckhardt, Nov 5, 2010
    #4
  5. KevinSimonson

    Geoff Guest

    On Thu, 4 Nov 2010 16:23:16 -0700 (PDT), 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"


    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::problem () 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.
    Geoff, Nov 5, 2010
    #5
  6. KevinSimonson

    Paul N Guest

    Other people have pointed out various things, but Ithink I have
    spotted a couple of extra problems:

    On Nov 4, 11:23 pm, KevinSimonson <> 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.
    Paul N, Nov 5, 2010
    #6
  7. 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<> 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
    Stuart Golodetz, Nov 5, 2010
    #7
  8. KevinSimonson

    Jorgen Grahn Guest

    ["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 .
    Jorgen Grahn, Nov 6, 2010
    #8
  9. In article <>,
    (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.

    --
    /~\ 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!
    Charlie Gibbs, Nov 8, 2010
    #9
  10. KevinSimonson

    Jorgen Grahn Guest

    On Mon, 2010-11-08, Charlie Gibbs wrote:
    > In article <>,
    > (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 .
    Jorgen Grahn, Nov 10, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  2. Nagaraj
    Replies:
    1
    Views:
    852
    Lionel B
    Mar 1, 2007
  3. xz
    Replies:
    4
    Views:
    290
    James Kanze
    Mar 16, 2008
  4. Replies:
    43
    Views:
    1,354
    Acmeous
    Nov 10, 2008
  5. RC
    Replies:
    0
    Views:
    556
Loading...

Share This Page