Solved

Compiler errors

Posted on 2006-07-05
10
352 Views
Last Modified: 2006-11-18
Can anyone help me compile the following code?  I keep getting errors concerning the typedef definitions.  
They reference one another in such a way that it keeps throwing errors.  Can anyone correct this?  I have
been using MS Visual Studio .NET to compile the code.

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef long SetStringPtr(IExample *, char *);
typedef long GetStringPtr(IExample *, char *, long);

typedef struct {
   SetStringPtr * SetString;
   GetStringPtr * GetString;
} IExampleVtbl;

typedef struct {
   IExampleVtbl * lpVtbl;
   DWORD          count;
   char           buffer[80];
} IExample;

long SetString(IExample *this, char * str);
long GetString(IExample *this, char *buffer, long length);

void
main (int argc, char *argv[])
{
      // Since the contents of IExample_Vtbl will never change, we'll
      // just declare it static and initialize it that way. It can
      // be reused for lots of instances of IExample.
      static IExampleVtbl IExample_Vtbl = {SetString, GetString};

      IExample * example;

      // Create (allocate) a IExample struct.
      example = (IExample *)GlobalAlloc(GMEM_FIXED, sizeof(IExample));

      // Initialize the IExample (ie, store a pointer to
      // IExample_Vtbl in it).
      example->lpVtbl = &IExample_Vtbl;
      example->count = 1;
      example->buffer[0] = 0;

      char buffer[80];

      example->lpVtbl->SetString(example, "Some text");
      example->lpVtbl->GetString(example, buffer, sizeof(buffer));

      exit (0);

} // main

long SetString(IExample *this, char * str)
{
   DWORD  i;

   // Let's copy the passed str to IExample's buffer
   i = lstrlen(str);
   if (i > 79) i = 79;
   CopyMemory(this->buffer, str, i);
   this->buffer[i] = 0;

   return(0);
}

long GetString(IExample *this, char *buffer, long length)
{
   DWORD  i;

   // Let's copy IExample's buffer to the passed buffer
   i = lstrlen(this->buffer);
   --length;
   if (i > length) i = length;
   CopyMemory(buffer, this->buffer, i);
   buffer[i] = 0;

   return(0);
}
0
Comment
Question by:jtrades
  • 5
  • 3
  • 2
10 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17048484
What is IExample ?
Where it is defined ?
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17048492
Sorry I didn't read the code properly
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 250 total points
ID: 17048512
Corrected code

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct IExample_;

typedef long SetStringPtr(IExample_ *, char *);
typedef long GetStringPtr(IExample_ *, char *, long);

struct IExampleVtbl_ {
   SetStringPtr * SetString;
   GetStringPtr * GetString;
};

struct IExample_{
   IExampleVtbl_ * lpVtbl;
   DWORD          count;
   char           buffer[80];
};

typedef struct IExample_ IExample;
typedef struct IExampleVtbl_ IExampleVtbl;

long SetString(IExample *obj, char * str);
long GetString(IExample *obj, char *buffer, long length);


void
main (int argc, char *argv[])
{
     // Since the contents of IExample_Vtbl will never change, we'll
     // just declare it static and initialize it that way. It can
     // be reused for lots of instances of IExample.
     static IExampleVtbl IExample_Vtbl = {SetString, GetString};

     IExample * example;

     // Create (allocate) a IExample struct.
     example = (IExample *)GlobalAlloc(GMEM_FIXED, sizeof(IExample));

     // Initialize the IExample (ie, store a pointer to
     // IExample_Vtbl in it).
     example->lpVtbl = &IExample_Vtbl;
     example->count = 1;
     example->buffer[0] = 0;

     char buffer[80];

     example->lpVtbl->SetString(example, "Some text");
     example->lpVtbl->GetString(example, buffer, sizeof(buffer));

     exit (0);

} // main

long SetString(IExample *obj, char * str)
{
   DWORD  i;

   // Let's copy the passed str to IExample's buffer
   i = strlen(str);
   if (i > 79) i = 79;
   CopyMemory(obj->buffer, str, i);
   obj->buffer[i] = 0;

   return(0);
}

long GetString(IExample *obj, char *buffer, long length)
{
   DWORD  i;

   // Let's copy IExample's buffer to the passed buffer
   i = strlen(obj->buffer);
   --length;
   if (i > length) i = length;
   CopyMemory(buffer, obj->buffer, i);
   buffer[i] = 0;

   return(0);
}
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17048530
>> i = lstrlen(str);
Don't use lstrlen(...) for char*.
Because for unicode project it will expect the parameter to be wchar_t*.
At that time you may get compilation error.
0
 
LVL 5

Expert Comment

by:cryptosid
ID: 17048532
typedef long SetStringPtr(IExample *, char *);
typedef long GetStringPtr(IExample *, char *, long);

doesn't seem to be correct, pls comment and try recompiling.

feel a bit rusty since i haven't compiled for a long time :-)

Regards,
siddhesh
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17048539
Don't use keywords like 'this' in a program.
I know 'this' is not applicable for C.
But if you compile it in C++ compiler then you may get errors.

So, its better to be safe :-)
0
 
LVL 11

Expert Comment

by:cup
ID: 17048625
The problem is forward referencing.  Try this

struct IExampleStr;  // Add this
typedef long SetStringPtr(struct IExampleStr *, char *);  // Change this defn
typedef long GetStringPtr(struct IExampleStr *, char *, long);  // Change this defn

typedef struct {
   SetStringPtr  *SetString;
   GetStringPtr  *GetString;
} IExampleVtbl;

typedef struct IExampleStr {  // Add the structure name here
   IExampleVtbl * lpVtbl;
   DWORD          count;
   char           buffer[80];
} IExample;

I've noticed that you're using this as a parameter.  While it is legal in C, it isn't in C++.  Might be better to rename it to that so that if in the future this program gets compiled as C++, you won't get any problems.
0
 
LVL 11

Expert Comment

by:cup
ID: 17048632
Oops sorry rajeev - I didn't refresh my screen.  You've already given the solution.
0
 
LVL 11

Expert Comment

by:cup
ID: 17049182
Cryptosid,  that is the standard way of typedefing a function.  Basically it is

typedef <return type> <typedef name> (<function parameters>);

Some compilers insist that you have appropriate *s so it could be something complex like

typedef long (*SetStringPtr)(struct IExampleStr*, char*);
0
 
LVL 5

Expert Comment

by:cryptosid
ID: 17056295
Ah!

You see I have forgotten that typedef could also be used for functions.

Regards,
Siddhesh
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now