views:

227

answers:

8

I want to copy and paste a C function I found in another program into my C++ program.
One of the function arguments uses the "this" pointer.

void cfunction( FILE *outfilefd, const VARTYPEDEFINED this);

The C++ compiler errors here on the function prototype:

error C2143: syntax error : missing ')' before 'this' 

How do I make this C++ usable?

Thanks.

EDIT ( per Betamoo comment )

void cfunction( FILE *outfilefd, const VARTYPEDEFINED this);
    {
    UINT8 temp = 0;

    temp = (UINT8)( this & 0x000000FF );

    if ( ( temp > LIMIT ) )
        ......
    else
    {
          ......
    }
}
+6  A: 

Rename this to something else. C does not have a special this variable, it's just a variable like any other.

Sjoerd
I think 'this' is a pointer rather than a variable, right?
AlexW
A pointer is also a variable.
Alexander Gessler
`this` isn't really a variable in C++; it's a key word that refers to a pointer value in non-static member functions.
Jerry Coffin
OK - but it's not a straightforward variable in any language that uses it as a keyword though.. it references other variables.
AlexW
+2  A: 

Is the problem simply that it's trying to name a parameter "this", which is a reserved word in C++? If so, I think the only solution is to rename the parameter. You could also put the function into its own .c file and compile it with a C compiler, and then link it into your C++ program (of course the prototype in the C++ program would have to omit the names of the parameters and just put in the types, which is allowed).

rmeador
+1  A: 

this is a reserved word in c++. It refers to the instance of the object in a class method.

You can rename the parameter to anything you'd like, as long as it too isn't a reserved word ;)

FYI, if you're going to be doing a lot of copy and pasting from the "other program", consider using extern "C" { }, and linking against the c libraries from the other program. This is described in detail in mixing c and c++.

Stephen
+2  A: 

this is reserved in C++, it isn't in C, so change this:

void cfunction( FILE *outfilefd, const VAR this);

to:

void cfunction( FILE *outfilefd, const VAR this1);
JonH
+8  A: 

You have two choices. You can leave the code as C, and just create a C++ header to let you call that C code from C++:

#ifdef __cplusplus
extern "C" { 
#endif

    void cfunction(FILE *, const VAR);

#ifdef __cplusplus
}
#endif

Or you can rewrite that function enough to get it to compile as C++ (probably just rename its this parameter to something else like thisvar).

Jerry Coffin
+3  A: 

First of all, I'd recommend leaving the original C function unchanged. Put it into a C source file, compile it and this as a variable name will be fine. In the header (where the function is declared), you can simply rename the parameter or leave it out:

void cfunction( FILE *, const VAR);

(You may need to use extern "C" to get the linkage right, see Jerry Coffin's answer).

Alexander Gessler
+1  A: 

I believe you are just copy/pasting instead of actually trying to call C function from within C++ program. Even otherwise, can you not just rename "this" parameter to something else like "old_this"?

void cfunction( FILE *outfilefd, const VAR old_this);
Sharad
+1  A: 

A notorious example of this problem (keywords in C++ that weren't keywords in C) happens in X11. Since X11 predates C++ popularity, class wasn't a keyword when X11 was written. But, having to work with C++ compilers, many X11 headers have declarations like the following:

typedef struct {
    VisualID visualID B32;
#if defined(__cplusplus) || defined(c_plusplus)
    CARD8 c_class;
#else
    CARD8 class;
#endif
    CARD8 bitsPerRGB;
    CARD16 colormapEntries B16;
    CARD32 redMask B32, greenMask B32, blueMask B32;
    CARD32 pad B32;
} xVisualType;
ninjalj
Interesting, thanks.
Tommy