请教:
我在视图的类的头文件中创建 CRect 对象 。// Demo4View.h : interface of the CDemo4View class
//
/////////////////////////////////////////////////////////////////////////////#if !defined(AFX_DEMO4VIEW_H__0419793B_0D85_4B5E_812C_C7B25A0DBCDE__INCLUDED_)
#define AFX_DEMO4VIEW_H__0419793B_0D85_4B5E_812C_C7B25A0DBCDE__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CDemo4View : public CView
{
protected: // create from serialization only
CDemo4View();
DECLARE_DYNCREATE(CDemo4View)// Attributes
public:
CDemo4Doc* GetDocument();// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDemo4View)
public:
virtual void OnDraw(CDC* pDC);  // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CDemo4View();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endifprotected:// Generated message map functions
protected:
//{{AFX_MSG(CDemo4View)
// NOTE - the ClassWizard will add and remove member functions here.
//    DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};CRect r;#ifndef _DEBUG  // debug version in Demo4View.cpp
inline CDemo4Doc* CDemo4View::GetDocument()
   { return (CDemo4Doc*)m_pDocument; }
#endif///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_DEMO4VIEW_H__0419793B_0D85_4B5E_812C_C7B25A0DBCDE__INCLUDED_)
编译时提示的错误是:
-------------------Configuration: Demo4 - Win32 Debug--------------------
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
Demo4.cpp
MainFrm.cpp
Demo4Doc.cpp
Demo4View.cpp
Generating Code...
Linking...
Demo4View.obj : error LNK2005: "class CRect  r" (?r@@3VCRect@@A) already defined in Demo4.obj
Debug/Demo4.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.Demo4.exe - 2 error(s), 0 warning(s)各位大哥,是不是我的 
CRect r 的位置不正确啊?
谢谢!

解决方案 »

  1.   

    CRect r;  // 这是定义,应该放在.cpp文件中如果想被其他文件使用,在.h文件中应该这样写:extern CRect r;-------------------------------------------------------
    MyCSDN - CSDN离线数据浏览工具。(恒金软件出品)  
    可下载论坛文件、文档文章和全文搜索论坛数据以供离线浏览。支持签名回复。
    下载地址:http://nj.onlinedown.net/soft/6591.htm
      

  2.   

    CRect r;放到类中,为成员变量你这样是声明了一个全局变量,要在其他文件中使用,需要
    extern CRect r;
      

  3.   

    书上说:
    在可执行文件中添加颜色预定义
    #define RED RGB(255,0,0);
     zx_sanjin 大哥 和 lixiaosan 大哥这里没有可在执行文件啊 ?是写在
    Demo4View.cpp 文件
    还是写在 MainFrm.cpp 文件中呢?谢谢!
      

  4.   

    那个文件用到了就写在哪个文件中。define最好是写在.h文件中-------------------------------------------------------
    MyCSDN - CSDN离线数据浏览工具。(恒金软件出品)  
    可下载论坛文件、文档文章和全文搜索论坛数据以供离线浏览。支持签名回复。
    下载地址:http://nj.onlinedown.net/soft/6591.htm
      

  5.   

    zx_sanjin 大哥:
    您说最好是写在 .h文件中 ,可是我是在 Demo4View.cpp 中用到它,有没有好的办法解决呢?
    (写 .h文件中 ,在 .cpp 文件中用它)还请大哥帮我看看这个错误:// Demo4View.cpp : implementation of the CDemo4View class
    //#include "stdafx.h"
    #include "Demo4.h"#include "Demo4Doc.h"
    #include "Demo4View.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    #define RED RGB(255,0,0);
    #define GREEN RBG(0,255,0);
    #define BLUE RGB(0,0,255);
    #define BLACK RBG(0,0,0); /////////////////////////////////////////////////////////////////////////////
    // CDemo4ViewIMPLEMENT_DYNCREATE(CDemo4View, CView)BEGIN_MESSAGE_MAP(CDemo4View, CView)
    //{{AFX_MSG_MAP(CDemo4View)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CDemo4View construction/destructionCDemo4View::CDemo4View()
    {
    // TODO: add construction code here}CDemo4View::~CDemo4View()
    {
    }BOOL CDemo4View::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs return CView::PreCreateWindow(cs);
    }/////////////////////////////////////////////////////////////////////////////
    // CDemo4View drawingvoid CDemo4View::OnDraw(CDC* pDC)
    {
    CDemo4Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    GetClientRect(r);
    if (r.right>750 && r.right<1000 || r.bottom && r.bottom<1000)
    {
     pDC->FillSolidRect(r,RED);
    }


    // TODO: add draw code for native data here
    }/////////////////////////////////////////////////////////////////////////////
    // CDemo4View printingBOOL CDemo4View::OnPreparePrinting(CPrintInfo* pInfo)
    {
    // default preparation
    return DoPreparePrinting(pInfo);
    }void CDemo4View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add extra initialization before printing
    }void CDemo4View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add cleanup after printing
    }/////////////////////////////////////////////////////////////////////////////
    // CDemo4View diagnostics#ifdef _DEBUG
    void CDemo4View::AssertValid() const
    {
    CView::AssertValid();
    }void CDemo4View::Dump(CDumpContext& dc) const
    {
    CView::Dump(dc);
    }CDemo4Doc* CDemo4View::GetDocument() // non-debug version is inline
    {
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDemo4Doc)));
    return (CDemo4Doc*)m_pDocument;
    }
    #endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
    // CDemo4View message handlers
    编译时提示的错误是:
    -------------------Configuration: Demo4 - Win32 Debug--------------------
    Compiling...
    Demo4View.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\Demo4\Demo4View.cpp(71) : error C2143: syntax error : missing ')' before ';'
    C:\Program Files\Microsoft Visual Studio\MyProjects\Demo4\Demo4View.cpp(71) : error C2059: syntax error : ')'
    Generating Code...
    Compiling...
    Demo4.cpp
    Generating Code...
    Error executing cl.exe.Demo4.exe - 2 error(s), 0 warning(s)如果把  pDC->FillSolidRect(r,RED);  注销掉就没有错误?
    zx_sanjin 大哥,我是错在哪里啊?
    我觉得 pDC->FillSolidRect(r,RED);   没有错误啊?谢谢!
      

  6.   

    #define RED RGB(255,0,0);
    #define GREEN RBG(0,255,0);
    #define BLUE RGB(0,0,255);
    #define BLACK RBG(0,0,0);把后面的;全都去掉,建议你下找本C/C++的语法书先看看-------------------------------------------------------
    MyCSDN - CSDN离线数据浏览工具。(恒金软件出品)  
    可下载论坛文件、文档文章和全文搜索论坛数据以供离线浏览。支持签名回复。
    下载地址:http://nj.onlinedown.net/soft/6591.htm
      

  7.   

    谢谢  zx_sanjin 大哥:
    我只留下了:
    #define RED RGB(255,0,0);可是编译时,出现同样的错误!谢谢!
      

  8.   

    晕哪!分号去掉,怎么看不懂呢?-------------------------------------------------------
    MyCSDN - CSDN离线数据浏览工具。(恒金软件出品)  
    可下载论坛文件、文档文章和全文搜索论坛数据以供离线浏览。支持签名回复。
    下载地址:http://nj.onlinedown.net/soft/6591.htm