我在myproject.cpp中定义了一个结构如下:
typedef SEND
{
int senddata[40];
}pres;
然后定义了一个全局变量。
pres mysend;
在myprojectDlg.cpp中我要引用这个全局变量。
于是我先声明了一下:
extern pres mysend;
编译时出错:
说在"extern pres mysend;"这一行前面少个";"
我看了一下并有少,真不知为什么,请大家帮帮忙,多谢了。

解决方案 »

  1.   

    typedef struct SEND
    {
    int senddata[40];
    }pres;
      

  2.   

    typedef SEND
    {
    int senddata[40];
    }; pres
      

  3.   

    也许是这样,记不清了
    typedef SEND
    {
    int senddata[40];
    }; pres ;
      

  4.   

    对不起我刚才打错了
    我是这样的:
    typedef struct SEND
    {
    int senddata[40];
    }pres;
      

  5.   

    typedef SEND
    {
    int senddata[40];
    }pres;
    将这个结构定义到stdafx.h中,
    在myprojectDlg.cpp中没有pres的定义
      

  6.   

    楼上兄弟,你如果放在stdafx.h,我试过可能会出现重复定义的消息,不知你是怎么解决的?
      

  7.   

    何处写的extern pres mysend;
    ??
      

  8.   

    亲爱的,你应该这样定义,typedef struct SEND
    {
    int senddata[40];
    }pres; 结构我就不多说了,不知道typedef SEND怎么能通过编译。
    然后全局的话在stdafx.cpp中定义
    pres mysend;在stdafx.h中导出
    extern pres mysend;
      

  9.   

    在我需要引用的地方呀写的
    extern pres mysend;
      

  10.   

    请问 winthegame(120斤重的大青蛙) 结构一定要在stdafx.h中定义吗?
      

  11.   

    不用啊?
    结构一般写程序我喜欢通用结构专门用个.h来定义,在需要用到的地方Include.
      

  12.   

    把typedef struct SEND
    {
    int senddata[40];
    }pres;
    放在myproject.h里面,
    因为你在myprojectDlg.cpp里面
    extern pres mysend;的时候,类型pres已经跑出了作用域,
    所以,再myprojectDlg.cpp里面编译器无法识别类型pres,
    所以,才发生了这个错误。
      

  13.   

    一般没有人在cpp中写类和结构体的声明,除非只给这个cpp用
      

  14.   

    问 liu_feng_fly(笑看风云 搏击苍穹 衔日月) 不用全局变量,我要在两个线程之间通信,怎么做呢?线程同步我不太懂,能讲讲否,不用担心分啦:)
      

  15.   

    同意ZhouBoTong(周伯童) ( ) 的观点,我觉得最好不要这样使用全局变量,很不好!
      

  16.   

    大家看看:
    // 科浩窑炉Dlg.cpp : implementation file
    //#include "stdafx.h"
    #include "科浩窑炉.h"
    #include "科浩窑炉Dlg.h"
    #include "DataDownLoad.h"
    #include "HisLine.h"
    #include <afxdao.h>#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    /////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App About
    CDaoDatabase m_dbColect;//只要加上下面的两个声明,就提示此处少个";"
    extern CSerialCom port;
    extern Pres SendData;class CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyDlg dialogCMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMyDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CMyDlg)
    m_coldata1 = 0;
    m_coldata2 = 0;
    m_coldata3 = 0;
    m_coldata4 = 0;
    m_coldata5 = 0;
    m_coldata6 = 0;
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }void CMyDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CMyDlg)
    DDX_Text(pDX, IDC_DATA1, m_coldata1);
    DDX_Text(pDX, IDC_DATA2, m_coldata2);
    DDX_Text(pDX, IDC_DATA3, m_coldata3);
    DDX_Text(pDX, IDC_DATA4, m_coldata4);
    DDX_Text(pDX, IDC_DATA5, m_coldata5);
    DDX_Text(pDX, IDC_DATA6, m_coldata6);
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
    //{{AFX_MSG_MAP(CMyDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(ID_MYCANCEL, OnMycancel)
    ON_BN_CLICKED(IDC_STARTCOLECT, OnStartcolect)
    ON_COMMAND(ID_MENUITEMDOWN, OnMenuitemdown)
    ON_COMMAND(ID_MENULINE, OnMenuline)
    ON_COMMAND(ID_MENUITEMKH, OnMenuitemkh)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyDlg message handlers
    BOOL CMyDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();
        m_coldata1=0;
    m_coldata2=0;
    m_coldata3=0;
    // Add "About..." menu item to system menu.
         
    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here
    SetWindowPos(NULL,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),0);
        if(!(port.OpenPort("COM1")))
    {
         MessageBox("对不起\n串行口连接或设置不正确!");
    }
        else//
    {
             if(!port.ConfigurePort(9600,8,FALSE,NOPARITY,TWOSTOPBITS))
     {
     MessageBox("对不起\n串行口设置不正确!");
     }
     else//
     {
     if(!port.SetCommunicationTimeouts(1000,10,10,10,10))
     {
                                      MessageBox("对不起\n通讯超时了!");
      port.ClosePort();
     }
    else//初始化配方数据
    {
               int i;
               for(i=0;i<40;i++)
       {
           SendData.SendData[i]=0;
       }
               //打开后台数据库
               try
       {
             m_dbColect.Open("C:\\My Documents\\back.mdb");
       }
                    catch(CDaoException *exc)
    {

                  //DisplayDaoException(exc);
               exc->Delete();
    }
                if(m_dbColect.IsOpen())
    {
             TRACE("数据库已经打开了!");
              CDaoTableDef m_table(&m_dbColect);
             try{
         m_table.Open("CONTROL");
    }
             catch(CDaoException *exc)
     {
                 //DisplayDaoException(exc);
                   exc->Delete();
     } }
               else 
       {
            TRACE("数据库还没有打开了!");
       }
                                
    }//初始化配方数据
     }// }//

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CMyDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CMyDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }void CMyDlg::OnMycancel() 
    {
    // TODO: Add your control notification handler code here
    AfxBeginThread(ColDaProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
    }
    void CMyDlg::OnStartcolect() 
    {
    // TODO: Add your control notification handler code here
    AfxBeginThread(ColDaProc,GetSafeHwnd(),THREAD_PRIORITY_NORMAL);
    }void CMyDlg::OnMenuitemdown() 
    {
    // TODO: Add your command handler code here
    CDataDownLoad datadown;
    datadown.DoModal();
    }void CMyDlg::OnMenuline() 
    {
    // TODO: Add your command handler code here
    CHisLine hisline;
    hisline.DoModal();
    }void CMyDlg::OnMenuitemkh() 
    {
    // TODO: Add your command handler code here
    CAboutDlg helpdlg;
    helpdlg.DoModal();
    }
      

  17.   

    可能由于你是在myproject.cpp定义的pres结构体,在myprojectDlg.cpp中没
    #include "myproject.cpp"
    编译器自然不认识pres结构体,
    建议:将结构体定义放入myproject.h文件