#pragma once и #ifndef #endif
От: alexsvk  
Дата: 23.08.11 09:43
Оценка:
Добрый день!
Для начала приведу код.
Привожу объявления и код с входной точкой для Windows.
StdAfx.h
#ifndef _STDAFX_H_
#define _STDAFX_H_

#include <vector>
#include <string>
#include <tchar.h>
#include <Windows.h>

#include "Winseed.h"
#include "Document.h"

TCHAR fname[256];

#endif _STDAFX_H_

Document.h
#include "StdAfx.h"

#ifndef _DOCUMENT_H_
#define _DOCUMENT_H_

class Document
{
public:
    Document(LPCTSTR);
    ~Document();
public:
    void initialize(LPTEXTMETRIC);
    void scrollSettings(HWND, int, int);
    void updateVscroll(HWND, int);
    void updateHscroll(HWND, int);
    void putText(HWND, HDC);
public:
    SCROLLINFO hsi;
    SCROLLINFO vsi;
private:
    int cxChar, yStep;
    int lineLenMax;
    int hRange;
    int vRange;
    std::vector<std::basic_string<TCHAR> >  text;
};

#endif _DOCUMENT_H_

Winseed.h
#include "StdAfx.h"

#ifndef _WINSEED_H_
#define _WINSEED_H_

class Winseed
{
public:
    Winseed(LPCTSTR lpWinName, HINSTANCE hInst, int nCmdShow, LRESULT(WINAPI *pWndProc)(HWND,UINT,WPARAM,LPARAM),\
        int width = CW_USEDEFAULT, int height = 0, int x = CW_USEDEFAULT, int y = 0, HWND hParent = NULL, \
        LPCTSTR lpMenuName = NULL, UINT uClassStyle = CS_VREDRAW|CS_HREDRAW, DWORD WinStyle = WS_OVERLAPPEDWINDOW);
    ~Winseed() {}

    HWND getHwnd() const
    {
        return hwnd;
    }

private:
    WNDCLASS wc;
    HWND hwnd;
};

#endif _WINSEED_H_



EntryPoint for Windows

#include "StdAfx.h"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    MSG msg;
    Winseed mainWnd(_T("TextViewer Adv."), hInstance, nCmdShow, WndProc, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, \
         CS_VREDRAW|CS_HREDRAW, WS_OVERLAPPEDWINDOW|WS_VSCROLL|WS_HSCROLL);

    while( GetMessage(&msg, NULL, 0, 0) )
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static Document doc(fname);
    HDC hDC;
    TEXTMETRIC tm;
    PAINTSTRUCT ps;
    int cxClient, cyClient;
    static int xInc, yInc;

    switch(uMsg)
    {
    case WM_CREATE:
        hDC = GetDC(hWnd);
        GetTextMetrics(hDC, &tm);
        doc.initialize(&tm);
        ReleaseDC(hWnd, hDC);
        break;

    case WM_SIZE:
        hDC = GetDC(hWnd);
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        doc.scrollSettings(hWnd, cxClient, cyClient);
        ReleaseDC(hWnd, hDC);
        break;

    case WM_VSCROLL:
        switch(LOWORD(wParam))
        {
        case SB_LINEUP:
            yInc = -1;
            break;
        case SB_LINEDOWN:
            yInc = 1;
            break;
        case SB_PAGEUP:
            yInc = -(int)doc.vsi.nPage;
            break;
        case SB_PAGEDOWN:
            yInc = (int)doc.vsi.nPage;
            break;
        case SB_THUMBTRACK:
            yInc = HIWORD(wParam) - doc.vsi.nPos;
            break;
        default:
            yInc = 0;
        }
        doc.updateVscroll(hWnd, yInc);
        break;

    case WM_HSCROLL:
        switch(LOWORD(wParam))
        {
        case SB_LINEUP:
            xInc = 1;
            break;
        case SB_LINEDOWN:
            xInc = -1;
            break;
        case SB_PAGEUP:
            xInc = (int)doc.hsi.nPage;
            break;
        case SB_PAGEDOWN:
            xInc = -(int)doc.hsi.nPage;
            break;
        case SB_THUMBTRACK:
            xInc = HIWORD(wParam) - doc.hsi.nPos;
            break;
        default:
            xInc = 0;
        }
        doc.updateHscroll(hWnd, xInc);
        break;

    case WM_PAINT:
        hDC = BeginPaint(hWnd, &ps);
        doc.putText(hWnd, hDC);
        EndPaint(hWnd, &ps);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return WndProc(hWnd, uMsg, wParam, lParam);
    }

    return 0;
}


Если использовать защиту от повторного включения #ifndef #endif, то MSVS10 не показывает ошибок после компиляции.
Если же использовать в заголовочных файлах #pragma once, то результат компиляции на MSVS10 следующий:

1>ClCompile:
1> TextViwerAdv.cpp
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\textviweradv.cpp(21): error C2065: 'fname' : undeclared identifier
1> Generating Code...
1> Compiling...
1> Winseed.cpp
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.h(6): error C2011: 'Winseed' : 'class' type redefinition
1> c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.h(6) : see declaration of 'Winseed'
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C2027: use of undefined type 'Winseed'
1> c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.h(6) : see declaration of 'Winseed'
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C2146: syntax error : missing ')' before identifier 'lpWinName'
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C2146: syntax error : missing ';' before identifier 'lpWinName'
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C2079: 'LPCTSTR' uses undefined class 'Winseed'
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C2377: 'LPCTSTR' : redefinition; typedef cannot be overloaded with any other symbol
1> c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(447) : see declaration of 'LPCTSTR'
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C2146: syntax error : missing ';' before identifier 'hInst'
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C2377: 'HINSTANCE' : redefinition; typedef cannot be overloaded with any other symbol
1> c:\program files\microsoft sdks\windows\v7.0a\include\windef.h(280) : see declaration of 'HINSTANCE'
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C4430: missing type specifier — int assumed. Note: C++ does not support default-int
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(4): error C2062: type 'int' unexpected
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(6): error C2059: syntax error : ')'
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(7): error C2143: syntax error : missing ';' before '{'
1>c:\users\администратор\documents\visual studio 2010\projects\schupak\ch4\textviweradv\textviweradv\winseed.cpp(7): error C2447: '{' : missing function header (old-style formal list?)
1> Generating Code...
1>
1>Build FAILED.


Где вбит гвоздь недоразумения?
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.