【问题标题】:How can I use variable as global? [closed]如何将变量用作全局变量? [关闭]
【发布时间】:2016-03-01 17:48:42
【问题描述】:

我可以使用类成员变量作为全局变量吗?

我想使用类成员变量作为全局变量,如下所示。

// mfc_test5Dlg.h : header file
//

#pragma once
#include "afxcmn.h"
#include "Testview.h"
#include "atltypes.h"

Image m_image;<------------------HERE!!!
Image m_blurgray;<------------------HERE!!!


// Cmfc_test5Dlg dialog
class Cmfc_test5Dlg : public CDialogEx
{
...

// Testview.cpp : implementation file
//

#include "stdafx.h"
#include "mfc_test5.h"
#include "mfc_test5Dlg.h"

extern  char * fileposition;
extern int slider_val;

// CTestview
CTestview *global_TestView; 

IMPLEMENT_DYNCREATE(CTestview, CScrollView)

    CTestview::CTestview()
{
    global_TestView = this;
}

CTestview::~CTestview()
{
}


BEGIN_MESSAGE_MAP(CTestview, CScrollView)
END_MESSAGE_MAP()


// CTestview drawing

void CTestview::OnInitialUpdate()
{
    CScrollView::OnInitialUpdate();

    CSize sizeTotal;
    // TODO: calculate the total size of this view
    sizeTotal.cx = sizeTotal.cy = 100;
    SetScrollSizes(MM_TEXT, sizeTotal);


}

void CTestview::OnDraw(CDC* pDC)
{
    CDocument* pDoc = GetDocument();
    // TODO: add draw code here

        CRect rcWin;
GetWindowRect( &rcWin );
int ff;
ff=rcWin.Width();
rcWin.Height();

m_image.read("file"); <------------------HERE!!!
    DoDisplayImage();

但是我有一些错误如下。

------ Build started: Project: mfc_test5, Configuration: Release Win32 ------

InitializeBuildStatus:
  Touching "Release\mfc_test5.unsuccessfulbuild".
ClCompile:
  All outputs are up-to-date.
  All outputs are up-to-date.
  All outputs are up-to-date.
ResourceCompile:
  All outputs are up-to-date.
mfc_test5Dlg.obj : error LNK2005: "class Magick::Image m_image" (?m_image@@3VImage@Magick@@A) already defined in mfc_test5.obj
mfc_test5Dlg.obj : error LNK2005: "class Magick::Image m_blurgray" (?m_blurgray@@3VImage@Magick@@A) already defined in mfc_test5.obj
Testview.obj : error LNK2005: "class Magick::Image m_image" (?m_image@@3VImage@Magick@@A) already defined in mfc_test5.obj
Testview.obj : error LNK2005: "class Magick::Image m_blurgray" (?m_blurgray@@3VImage@Magick@@A) already defined in mfc_test5.obj
C:\work\mfc_test5\mfc_test5\Release\mfc_test5.exe : fatal error LNK1169: one or more multiply defined symbols found

Build FAILED.

Time Elapsed 00:00:01.94
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

正如您在上面的代码中看到的,我想使用 m_image 作为全局类成员变量。

我该如何解决这个问题?

【问题讨论】:

  • 当变量显然不是你的类的成员时,你为什么称它们为类成员?
  • 也许你想要静态成员变量而不是全局变量?
  • @TommyA 有什么问题吗?
  • 问题是标题令人困惑,与您的问题不匹配。你的类没有任何成员变量(至少我没有找到)
  • @tobi303 那么他们把这些变量称为什么 Image m_image;

标签: c++ imagemagick


【解决方案1】:

使用 extern 关键字在标题中声明您的变量

extern Image m_image;
extern Image m_blurgray;

并在 .cpp(源)文件中定义这些变量。

Image m_image;
Image m_blurgray;

【讨论】:

  • 知道了,那么我可以在全局中使用上述变量吗?如果我想在另一个 cpp 中使用相同的变量,这可能吗?
  • @cabot 是的,因为这些变量将成为全局变量。
猜你喜欢
  • 2013-12-30
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 2017-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多