Solved

#include for a class I created

Posted on 2000-02-26
7
229 Views
Last Modified: 2013-11-18
I am creating a simple drawing program in Visual C++.  The program is a garden variety Win32 type program (no MFC).

I have a custom made class I am trying to include in the main source code.

I am getting lots of errors that don't make sense to me:

--------------------Configuration: MenuDemo - Win32 Debug--------------------
Compiling...
MenuDemo.c
c:\windows\desktop\csis 3330\program 3\objectsdrawn.h(12) : error C2061: syntax error : identifier 'ObjectsDrawn'
c:\windows\desktop\csis 3330\program 3\objectsdrawn.h(12) : error C2059: syntax error : ';'
c:\windows\desktop\csis 3330\program 3\objectsdrawn.h(13) : error C2449: found '{' at file scope (missing function header?)
c:\windows\desktop\csis 3330\program 3\objectsdrawn.h(20) : error C2059: syntax error : '}'
c:\windows\desktop\csis 3330\program 3\menudemo.c(27) : error C2065: 'WndProc' : undeclared identifier
c:\windows\desktop\csis 3330\program 3\menudemo.c(27) : warning C4047: '=' : 'long (__stdcall *)(struct HWND__ *,unsigned int ,unsigned int ,long )' differs in levels of indirection from 'int '
c:\windows\desktop\csis 3330\program 3\menudemo.c(62) : error C2373: 'WndProc' : redefinition; different type modifiers
Error executing cl.exe.

MenuDemo.exe - 6 error(s), 1 warning(s)

I will paste the source code for the main program and the class separately.

Thanks!
0
Comment
Question by:knowlton
  • 5
  • 2
7 Comments
 
LVL 5

Author Comment

by:knowlton
ID: 2561093
Source code for MAIN program.  When I comment out the "#include "ObjectsDrawn.h" the program compiles just fine.



/*-----------------------------------------
   MENUDEMO.C -- Menu Demonstration
                 (c) Charles Petzold, 1998
  -----------------------------------------*/

#include <windows.h>
#include "resource.h"
#include "ObjectsDrawn.h"
#define ID_TIMER 1





LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

TCHAR szAppName[] = TEXT ("MenuDemo") ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     HWND     hwnd ;
     MSG      msg ;
     WNDCLASS wndclass ;
     
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = szAppName ;
     wndclass.lpszClassName = szAppName ;
     
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, TEXT ("Menu Demonstration"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     static int idColor [5] = { WHITE_BRUSH,  LTGRAY_BRUSH, GRAY_BRUSH,
                                DKGRAY_BRUSH, BLACK_BRUSH } ;
     static int iSelection = IDM_BKGND_WHITE ;
     HMENU      hMenu ;
     
     switch (message)
     {
     case WM_COMMAND:
          hMenu = GetMenu (hwnd) ;
         
          switch (LOWORD (wParam))
          {
          case IDM_CLEAR:
                     //MessageBox (hwnd, TEXT ("Clear function not yet implemented!"),
               //            szAppName, MB_ICONEXCLAMATION | MB_OK) ;
               return 0 ;
               
          case IDM_APP_EXIT:
               SendMessage (hwnd, WM_CLOSE, 0, 0) ;
               return 0 ;
               
          case IDM_EDIT_UNDO:
          case IDM_EDIT_CUT:
          case IDM_EDIT_COPY:
          case IDM_EDIT_PASTE:
          case IDM_EDIT_CLEAR:
               MessageBeep (0) ;
               return 0 ;
               
          case IDM_BKGND_WHITE:         // Note: Logic below
          case IDM_BKGND_LTGRAY:        //   assumes that IDM_WHITE
          case IDM_BKGND_GRAY:          //   through IDM_BLACK are
          case IDM_BKGND_DKGRAY:        //   consecutive numbers in
          case IDM_BKGND_BLACK:         //   the order shown here.
               
               CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;
               iSelection = LOWORD (wParam) ;
               CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;
               
               SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG)
                    GetStockObject
                             (idColor [LOWORD (wParam) - IDM_BKGND_WHITE])) ;
               
               InvalidateRect (hwnd, NULL, TRUE) ;
               return 0 ;
               
          case IDM_TIMER_START:
               if (SetTimer (hwnd, ID_TIMER, 1000, NULL))
               {
                    EnableMenuItem (hMenu, IDM_TIMER_START, MF_GRAYED) ;
                    EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_ENABLED) ;
               }
               return 0 ;
               
          case IDM_TIMER_STOP:
               KillTimer (hwnd, ID_TIMER) ;
               EnableMenuItem (hMenu, IDM_TIMER_START, MF_ENABLED) ;
               EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_GRAYED) ;
               return 0 ;
               
          case IDM_APP_HELP:
               MessageBox (hwnd, TEXT ("Help not yet implemented!"),
                           szAppName, MB_ICONEXCLAMATION | MB_OK) ;
               return 0 ;
               
          case IDM_APP_ABOUT:
               MessageBox (hwnd, TEXT ("Menu Demonstration Program\n")
                                 TEXT ("(c) Charles Petzold, 1998"),
                           szAppName, MB_ICONINFORMATION | MB_OK) ;
               return 0 ;
          }
          break ;
         
     case WM_TIMER:
          MessageBeep (0) ;
          return 0 ;
               
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
0
 
LVL 5

Author Comment

by:knowlton
ID: 2561095
H file for the OjbectsDrawn class

// ObjectsDrawn.h: interface for the ObjectsDrawn class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_OBJECTSDRAWN_H__204F6F06_EC28_11D3_B73C_9DAC4BE0123B__INCLUDED_)
#define AFX_OBJECTSDRAWN_H__204F6F06_EC28_11D3_B73C_9DAC4BE0123B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class ObjectsDrawn  
{
private:
      int a;
public:
      ObjectsDrawn();
      virtual ~ObjectsDrawn();

};

#endif // !defined(AFX_OBJECTSDRAWN_H__204F6F06_EC28_11D3_B73C_9DAC4BE0123B__INCLUDED_)
0
 
LVL 5

Author Comment

by:knowlton
ID: 2561097
CPP for the ObjectsDrawn class

// ObjectsDrawn.cpp: implementation of the ObjectsDrawn class.
//
//////////////////////////////////////////////////////////////////////

#include "ObjectsDrawn.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ObjectsDrawn::ObjectsDrawn()
{

}

ObjectsDrawn::~ObjectsDrawn()
{

}
0
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.

 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 2561113
Your problem seems to be the name of the file 'MENUDEMO.C' - the compiler assumes this to be plain 'C' source and doesn't recognize C++ classes - change the name of the file to read 'MENUDEMO.CPP', this should remedy your problems.


Feel free to ask if you need more information!
0
 
LVL 5

Author Comment

by:knowlton
ID: 2561200
This did the trick!  Thank you so much!

Why doesn't VC just default the file extension to .cpp????  Wouldn't that be easier?  Then both C and C++ source code would be handled.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2561342
It's a cenvention files with the .c extension are treated as plain 'C' (by every C++ compiler)

BTW: VC defaults the extension to .cpp when saving a newly created file - but as you apparently took the sample 'as is', you ran into trouble... there's however a workaround when the problem is 'the other way round' - code can be guarded by using

#ifdef __cplusplus
// C++ specific code here
#endif
0
 
LVL 5

Author Comment

by:knowlton
ID: 2573238
Oh, okay, cool!

Thanks again for the help and the info!

Tom
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

Suggested Solutions

Title # Comments Views Activity
VS2015 compilation and missing DLLs 9 119
How to copy an image file into clipboard C/C++? 1 154
Contact Forms for Website 6 78
Add values of each row in an array 3 50
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

863 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

24 Experts available now in Live!

Get 1:1 Help Now