Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
3764 connectés 

  FORUM HardWare.fr
  Programmation

  [C++] illegal function definitions

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C++] illegal function definitions

n°39329
Alload
Posté le 13-06-2001 à 11:41:59  profilanswer
 

VC++ me renvoit ça: D:\Mes Documents\Programmation\DirectX\BitmapToSurface.h(10) : error C2601: 'BitmapToSurface' : local function definitions are illegal
, quand j'essaie de compiler ce code:
 
FICHIER main.cpp:
 
#include <windows.h>
#include <ddraw.h>
 
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
 
///////////////////////////////////////////////////////////////////
 
LPDIRECTDRAW lpDirectDrawObject = NULL;
LPDIRECTDRAWSURFACE lpPrimary = NULL;
 
///////////////////////////////////////////////////////////////////
 
long CALLBACK WndProc(HWND hwnd, UINT message, UINT wParam, long lParam)
{
 switch (message)
 {
 case WM_KEYDOWN:
  {
   if (wParam == VK_ESCAPE)
   {
    DestroyWindow(hwnd);
 
    return 0;
   }
 
   else
    return 0;
  }
 
 case WM_DESTROY:
  {
   if (lpDirectDrawObject)
   {
    if (lpPrimary)
     lpPrimary->Release();
 
    lpDirectDrawObject->Release();
   }
     
   PostQuitMessage(0);
 
   return 0;
  }
 
 default:
  {
   return (DefWindowProc(hwnd, message, wParam, lParam));
  }
 }
}
 
///////////////////////////////////////////////////////////////////
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
 HWND hwnd;
 WNDCLASSEX wndclassex;
 MSG msg;
 
 
 const char szClassName[] = "Window Class";
 
 wndclassex.lpszClassName = szClassName;
 wndclassex.cbSize = sizeof(wndclassex);
 wndclassex.style = CS_HREDRAW | CS_VREDRAW;
 wndclassex.lpszMenuName = NULL;
 wndclassex.hInstance = hInstance;
 wndclassex.lpfnWndProc = WndProc;
 wndclassex.hbrBackground = NULL;
 wndclassex.cbClsExtra = 0;
 wndclassex.cbWndExtra = 0;
 wndclassex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 wndclassex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
 wndclassex.hCursor = LoadCursor(NULL, IDC_ARROW);
 
 RegisterClassEx(&wndclassex);
 
 hwnd = CreateWindowEx( WS_EX_TOPMOST,
       szClassName,
       "DirectX Project",
       WS_POPUP,
       0,
       0,
       GetSystemMetrics(SM_CXSCREEN),
       GetSystemMetrics(SM_CYSCREEN),
       NULL,
       NULL,
       hInstance,
       NULL);
 
 
 ShowWindow(hwnd, nShowCmd);
 UpdateWindow(hwnd);
 
///////////////////////////////////////////////////////////////////
 
 DirectDrawCreate(NULL, &lpDirectDrawObject, NULL);
 lpDirectDrawObject->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
 lpDirectDrawObject->SetDisplayMode(1024, 768, 32);
 
 
 DDSURFACEDESC ddsd;
 ddsd.dwSize = sizeof(ddsd);
 ddsd.dwFlags = DDSD_CAPS;
 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
 lpDirectDrawObject->CreateSurface(&ddsd, &lpPrimary, NULL);
 
 #include "BitmapToSurface.h"
 
 BitmapToSurface(lpPrimary, "image.bmp", 1024, 768, 0, 0);
 
///////////////////////////////////////////////////////////////////
 
 while (GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 
 return 0;
}
 
 
 
 
FICHIER BITMAPTOSURFACE.H:
 
#include <windows.h>
#include <ddraw.h>
 
int BitmapToSurface(LPDIRECTDRAWSURFACE lpSurfaceDest,  
      char szBitmapName[],  
      int iXSize,  
      int iYSize,  
      int iXDest,  
      int iYDest)
{
 HDC hSrcDC;           // source DC - memory device context
 HDC hDestDC;          // destination DC - surface device context
 HBITMAP hbitmap;      // handle to the bitmap resource
 BITMAP bmp;           // structure for bitmap info
 
 int nHeight, nWidth;  // bitmap dimensions
 
 // first load the bitmap resource
 if ((hbitmap = (HBITMAP)LoadImage(hInstance, szBitmapName,
                                    IMAGE_BITMAP, iXSize, iYSize,
                                    LR_CREATEDIBSECTION | LR_LOADFROMFILE)) == NULL)
  return(FALSE);
 
 // create a DC for the bitmap to use
 if ((hSrcDC = CreateCompatibleDC(NULL)) == NULL)
  return(FALSE);
 
 // select the bitmap into the DC
 if (SelectObject(hSrcDC, hbitmap) == NULL)
 {
  DeleteDC(hSrcDC);
  return(FALSE);
 }
 
 // get image dimensions
 if (GetObject(hbitmap, sizeof(BITMAP), &bmp) == 0)
 {
  DeleteDC(hSrcDC);
  return(FALSE);
 }
 
 nWidth = bmp.bmWidth;
 nHeight = bmp.bmHeight;
 
 // retrieve surface DC
 if (FAILED(lpSurfaceDest->GetDC(&hDestDC)))
 {
  DeleteDC(hSrcDC);
  return(FALSE);
 }
 
 // copy image from one DC to the other
 if (BitBlt(hDestDC, iXDest, iYDest, nWidth, nHeight, hSrcDC, 0, 0,
             SRCCOPY) == NULL)
 {
  lpSurfaceDest->ReleaseDC(hDestDC);
  DeleteDC(hSrcDC);
  return(FALSE);
 }
 
 // kill the device contexts
 lpSurfaceDest->ReleaseDC(hDestDC);
 DeleteDC(hSrcDC);
 
 // return success
 return 0;
}
 
D'où vient le problème?

mood
Publicité
Posté le 13-06-2001 à 11:41:59  profilanswer
 

n°39347
Alload
Posté le 13-06-2001 à 12:16:01  profilanswer
 

Ca veut déjà dire quoi illegal function defitions exactement?

n°39352
tgrx
My heart is pumping for love
Posté le 13-06-2001 à 12:21:12  profilanswer
 

alload> j'ai pas regarde ton source dans le detail, mais tu as l'air de definir tes fonctions dans les fichier .h, en fait tu devrais uniquement faire une declaration style :

Code :
  1. int BitmapToSurface(LPDIRECTDRAWSURFACE lpSurfaceDest, 
  2.                 char szBitmapName[], 
  3.                 int iXSize, 
  4.                 int iYSize, 
  5.                 int iXDest, 
  6.                 int iYDest);


 
dans ton .h, et ensuite definir ta fonction dans le .cpp.
 
Ensuite ton probleme vient du fait que tu fais un #include a l'interieur d'une fonction... :D.... :lol:
 
ATTENTION : les #include sont toujours au debut des fichiers (ou du moins en dehors des fonctions), donc bouge ton #include "bitmaptosurface.h" au debut de ton fichier main.cpp, et ca devrait rouler :)

n°39354
Alload
Posté le 13-06-2001 à 12:23:07  profilanswer
 

Je sais que les includes sont normalement au début, mais avec mon .h initial si je le met au début, ben hInstance n'est pas initialisé. Donc ça marche pas.
 
Bon je vais essayer ton truc et je reviens dire comment ça se passe.
:)
 
Merci.

n°39357
tgrx
My heart is pumping for love
Posté le 13-06-2001 à 12:26:52  profilanswer
 

Alload> en fait quand tu fais un #include <fichier.h>, le compilateur il ne fait rien d'autre qu'un copier-coller de fichier.h a l'interieur de ton source, donc en fait faire un include a l'interieur d'une fonction, ca revenait a definir la fonction bitmaptosurface a l'interieur d'une autre fonction... et ca c'est pas possible en C++ :)

n°39365
Alload
Posté le 13-06-2001 à 12:51:21  profilanswer
 

Maintenant ça me donne ça: Main.obj : error LNK2001: unresolved external symbol "int __cdecl BitmapToSurface(struct IDirectDrawSurface *,char const * const,int,int,int,int)" (?BitmapToSurface@@YAHPAUIDirectDrawSurface@@QBDHHHH@Z)
 
C'est quoi encore ce binz?
 
Voilà le fichier "ddmyutil.h":
 
//Fonction qui place directement un bitmap dans une surface déjà créée
int BitmapToSurface(LPDIRECTDRAWSURFACE lpSurfaceDest, //Surface destinatrice
      const char szBitmapName[],     //Fichier bitmap source
      int iXSize,              //Taille x de la surface
      int iYSize,        //Taille y de la surface
      int iXDest,        //Coordonnée x du coin gauche du bitmap
      int iYDest);        //Coordonnée y du coin gauche du bitmap
 
 
Et voici le fichier "ddmyutil.cpp":
 
#include <windows.h>
#include <ddraw.h>
 
#include "ddmyutil.h"
 
 
int BitmapToSurface(LPDIRECTDRAWSURFACE lpSurfaceDest,  
      const char szBitmapName[],  
      int iXSize,  
      int iYSize,  
      int iXDest,  
      int iYDest)
{
 HDC hSrcDC;           // source DC - memory device context
 HDC hDestDC;          // destination DC - surface device context
 HBITMAP hbitmap;      // handle to the bitmap resource
 BITMAP bmp;           // structure for bitmap info
 
 int nHeight, nWidth;  // bitmap dimensions
 
 // first load the bitmap resource
 if ((hbitmap = (HBITMAP)LoadImage(hInstance, szBitmapName,
                                    IMAGE_BITMAP, iXSize, iYSize,
                                    LR_CREATEDIBSECTION | LR_LOADFROMFILE)) == NULL)
 {
  MessageBox(hwnd, "Erreur pendant le chargement d'un bitmap.", "Loading Bitmap Error", MB_OK);
  return(FALSE);
 }
 
 // create a DC for the bitmap to use
 if ((hSrcDC = CreateCompatibleDC(NULL)) == NULL)
 {
  MessageBox(hwnd, "Erreur pendant la création d'un Device Context pour l'image source.", "Loading Bitmap Error", MB_OK);
  return(FALSE);
 }
 
 // select the bitmap into the DC
 if (SelectObject(hSrcDC, hbitmap) == NULL)
 {
  DeleteDC(hSrcDC);
  MessageBox(hwnd, "Erreur pendant la copie d'un bitmap vers son Device Context.", "Loading Bitmap Error", MB_OK);
  return(FALSE);
 }
 
 // get image dimensions
 if (GetObject(hbitmap, sizeof(BITMAP), &bmp) == 0)
 {
  DeleteDC(hSrcDC);
  MessageBox(hwnd, "Erreur pendant l'acquisiton des informations d'un bitmap", "Loading Bitmap Error", MB_OK);
  return(FALSE);
 }
 
 nWidth = bmp.bmWidth;
 nHeight = bmp.bmHeight;
 
 // retrieve surface DC
 if (FAILED(lpSurfaceDest->GetDC(&hDestDC)))
 {
  DeleteDC(hSrcDC);
  MessageBox(hwnd, "Erreur pendant l'initialisation de la surface destinatrice.", "Loading Bitmap Error", MB_OK);
  return(FALSE);
 }
 
 // copy image from one DC to the other
 if (BitBlt(hDestDC, iXDest, iYDest, nWidth, nHeight, hSrcDC, 0, 0,
             SRCCOPY) == NULL)
 {
  lpSurfaceDest->ReleaseDC(hDestDC);
  DeleteDC(hSrcDC);
  MessageBox(hwnd, "Erreur pendant la copie du bitmap vers la surface.", "Loading Bitmap Error", MB_OK);
  return(FALSE);
 }
 
 // kill the device contexts
 lpSurfaceDest->ReleaseDC(hDestDC);
 DeleteDC(hSrcDC);
 
 // return success
 return 0;
}

n°39367
tgrx
My heart is pumping for love
Posté le 13-06-2001 à 12:56:16  profilanswer
 

Bien ! au niveau .h, .cpp :)
 
Sauf que... ton fichier .h doit etre independant... et la ce n'est pas le cas car tu utilises LPDIRECTDRAWSURFACE, qui n'est pas defini dans le .h
 
Solution : tu ajoutes les lignes  
#include <windows.h>
#include <ddraw.h> dans le .h
 
 
et le debut de ton fichier ddmyutil.cpp, tu mets simplement (ainsi que dans main.cpp) :
#include "ddmyutil.h"
 
... comme ca c'est clean... :)

 

[edit]--Message édité par tgrx--[/edit]

n°39372
Alload
Posté le 13-06-2001 à 13:06:49  profilanswer
 

Oki, merci de m'aider pour le codage.
:)
 
Par contre la compilation me renvoit toujours la même erreur:
Main.obj : error LNK2001: unresolved external symbol "int __cdecl BitmapToSurface(struct IDirectDrawSurface *,char const * const,int,int,int,int)" (?BitmapToSurface@@YAHPAUIDirectDrawSurface@@QBDHHHH@Z)
 
 
Ca vient peut-être de la façon dont j'utilise la fonction:
BitmapToSurface(lpPrimary, "image.bmp", 1024, 768, 0, 0);
 
Quelqu'un à une idée?

n°39374
tgrx
My heart is pumping for love
Posté le 13-06-2001 à 13:10:21  profilanswer
 

L'erreur c'est qu'au moment du link il ne trouve pas ta fonction BitmapToSurface... c'est bizarre, normalement il devrait la trouver.
 
Essaie d'ajouter ddmyutil dans les parametres du linker... :??:

n°39378
tgrx
My heart is pumping for love
Posté le 13-06-2001 à 13:13:22  profilanswer
 

Surtout que le prototype a l'air de correspondre avec l'appel que tu en fais... :??:

mood
Publicité
Posté le 13-06-2001 à 13:13:22  profilanswer
 

n°39391
Alload
Posté le 13-06-2001 à 13:24:09  profilanswer
 

Si quelqu'un veut downloader les fichiers sources:
 
http://www.multimania.com/final6/DDproblem.zip
 
C'est quoi cette merde... Arf...

n°39403
tgrx
My heart is pumping for love
Posté le 13-06-2001 à 13:39:53  profilanswer
 

Est-ce que ca marche si tu mets (dans main.cpp)
 
#include "ddmyutils.cpp"
 
a la place de  
 
#include "ddmyutils.h" ??

n°39404
Alload
Posté le 13-06-2001 à 13:41:25  profilanswer
 

Si je fais ça, le handle de la fenêtre hwnd et le hande d'instanse hInstance ne sont pas définis donc ça marche pas.

n°39406
tgrx
My heart is pumping for love
Posté le 13-06-2001 à 13:42:10  profilanswer
 

ah bon ?

n°39407
Alload
Posté le 13-06-2001 à 13:46:05  profilanswer
 

Ben hInstance et hwnd sont définis que dans la fonction WinMain qui est après les includes donc ça marche pas.

n°39409
tgrx
My heart is pumping for love
Posté le 13-06-2001 à 13:52:58  profilanswer
 

ah ok, mais c'est parce que ton code est mal foutu...
 
Normalement ddmyutil.h ne devrait pas acceder "directement" a hInstance et hwnd qui sont  des variables globales... :sarcastic:... beurk.
En plus elles sont locales, car definies uniquement dans WinMain... donc ca ne fonctionnera jamais dans BitmapToSurface...
 
Solution : passer hInstance et hwnd en parametre de ta fonction BitmapToSurface (en pointeur ou reference comme tu veux).
 
int BitmapToSurface(LPDIRECTDRAWSURFACE lpSurfaceDest,const char szBitmapName[],int iXSize,int iYSize,int iXDest,int iYDest,HINSTANCE& hInstance,HWND& hwnd);
 
et dans main.cpp :
BitmapToSurface(lpPrimary, "image.bmp", 1024, 768, 0, 0, hInstance, hwnd);

n°39412
tgrx
My heart is pumping for love
Posté le 13-06-2001 à 13:59:19  profilanswer
 

et apparemment (je regarde les callback dans ton fichier main.cpp), HISTANCE et HWND sont des pointeurs, donc tu peux virer les references... sert a rien.

n°39414
tgrx
My heart is pumping for love
Posté le 13-06-2001 à 14:01:42  profilanswer
 

et pour finir, je pense que VC++ n'a jamais compile le fichier ddmyutil.cpp, car normalement tu devrais avoir plein d'erreurs a cause des hwnd et hInstance qui n'existent pas...
 
donc je sais pas ou c'est (j'ai pas Visual C++ sous la main), mais ajoute ddmyutil.cpp a la liste des fichiers a compiler, ca devrait resoudre ton probleme de link.
Un truc du genre : projet > ajouter fichiers ??

n°39415
Alload
Posté le 13-06-2001 à 14:02:40  profilanswer
 

Thanks ça marche maintenant.
:)


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation

  [C++] illegal function definitions

 

Sujets relatifs
c++ : "illegal pure syntax, must be '= 0'"function et vb script
error C2071: '<Unknown>' : illegal storage class 
Plus de sujets relatifs à : [C++] illegal function definitions


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)