簡體   English   中英

為什么 my.cpp 文件無法識別包含在 .h 文件中的變量?

[英]Why is my .cpp file can't recognize variables from included in .h file?

我創建了兩個對話框類“OpenFileDiag”和“Search_diag”,似乎 Search_diag.cpp 無法識別來自 OpenFileDiag.h 的變量,反之亦然(OpenFileDiag.cpp 無法識別來自 Search_diag.h 的變量),即使我將其包含在 OpenFileDiag.cpp 文件中。

請注意,本文檔只是我的測試文檔,一些變量賦值沒有邏輯意義,但我只是想證明,我文檔中的這兩個文件無法識別彼此之間的變量。

OpenFileDiag.h 文件

#pragma once
#include <vector>

// OpenFileDiag dialog

class OpenFileDiag : public CDialog
{
    DECLARE_DYNAMIC(OpenFileDiag)

public:
    OpenFileDiag(CWnd* pParent = nullptr);   // standard constructor
    virtual ~OpenFileDiag();

// Dialog Data
#ifdef AFX_DESIGN_TIME
    enum { IDD = ID_OPENFILEDIAG };
#endif

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnBnClickedButtonOpenfile();
    class Zam
    {
    public:
        CString name;
        CString adress;
        int number;
    };
    std::vector<Zam> z;
    enum document_type {None, Text};
    document_type m_document_type{ document_type::None };
    
    afx_msg void OnBnClickedButtonVyhladatDiag();
};

Search_diag.cpp 文件

// Search_diag.cpp : implementation file
//

#include "pch.h"
#include "MFCOpenTest.h"
#include "Search_diag.h"
#include "afxdialogex.h"
#include "OpenFileDiag.h"


// Search_diag dialog

IMPLEMENT_DYNAMIC(Search_diag, CDialog)

Search_diag::Search_diag(CWnd* pParent /*=nullptr*/)
    : CDialog(ID_SEARCH_DIAG, pParent)
{

}

Search_diag::~Search_diag()
{
}

void Search_diag::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_SELECT_ATRIBUTE, m_selected_atribute);
    DDX_Control(pDX, ID_SELECTED_CRITERION, m_selected_criterion);
}


BEGIN_MESSAGE_MAP(Search_diag, CDialog)
//  ON_CBN_SELCHANGE(IDC_SELECT_ATRIBUTE, &Search_diag::OnCbnSelchangeSelectAtribute)
ON_CBN_SELCHANGE(IDC_SELECT_ATRIBUTE, &Search_diag::OnSelchangeSelectAtribute)
END_MESSAGE_MAP()


// Search_diag message handlers
void Search_diag::OnSelchangeSelectAtribute()
{
    number_of_atribute = m_selected_atribute.GetCurSel();
    z[0].number = number_of_atribute;
    // TODO: Add your control notification handler code here
}

問題在於“z[0].number = number_of_atribute;”這一行 Search_diag.cpp 無法從 OpenFileDiag.h 文件中識別變量“z”,即使它包含在內。

這是我得到的錯誤: Search_diag.cpp(47,2): error C2065: 'z': undeclared identifier

我也試過添加這樣的東西: OpenFileDiag::z[0].name = number_of_atribute; 或 OpenFileDiag::Zam::z[0].name = number_of_atribute; 但這沒有幫助。

class OpenFileDiag : public CDialog
{

// ...

  std::vector<Zam> z;

顯示的z是名為OpenFileDiag的 class 的成員。

void Search_diag::OnSelchangeSelectAtribute()
{

// ...

    z[0].number = number_of_atribute;
    // TODO: Add your control notification handler code here
}

其他一些名為Search_diag的 class 的方法顯然正在嘗試訪問名為z的 class 成員。 雖然顯示的代碼沒有定義這個 class,但大概它沒有任何名為z的成員。 僅僅因為有一個不同的 class 具有該名稱的成員,並不意味着可以像這樣訪問它。

Search_diag.cpp 無法從 OpenFileDiag.h 文件中識別變量“z”,即使它包含在內。

在 C++ 中,無論如何,在哪個文件中聲明某些內容都沒有關系。 重要的是在哪些類中聲明了某些東西,或者沒有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM