Solved

Compiler errors

Posted on 2006-07-05
10
356 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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
 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C: GetDiskFreeSpaceEx() for default drive 14 107
How to get time_t value that is a week old 6 168
negation in C function 14 153
smtp c source code 7 68
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

770 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