【问题标题】:Passing a class pointer as a function argument将类指针作为函数参数传递
【发布时间】:2021-12-18 12:54:33
【问题描述】:

目前我正在尝试创建一个函数来在下面的代码中实现模块化。

[代码 1]

float CBitmapWaterMark::DrawPrintInfoText(HDC hDC)
{
    StringFormat* stringFormat = new StringFormat();   
    // I'm trying to pass a stringFormat object to a member function of a class(CBitmapWaterMark). 
    stringFormat->SetAlignment(StringAlignmentNear);
    stringFormat->SetLineAlignment(StringAlignmentNear);
 

    TestFunction(stringFormat) //help point 1
}

VOID CBitmapWaterMark::TestFunction(StringFormat* stringFormat) // help point 2
{
    // implement 
}

=> 语法错误标识符 StringFormat

但我不知道如何将它作为函数参数传递以及如何接收它。


<gdiplusstringformat.h> 中声明的StringFormat 似乎存在问题。

这是一个最小的完整可重现示例:

#include <windows.h>
#include <gdiplus.h>
#include <gdiplusstringformat.h>

StringFormat fmt;

错误日志:

1>C:\Project3 C++\main.cpp(5,17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Project3 C++\main.cpp(5,14): error C2146: syntax error: missing ';' before identifier 'fmt'

【问题讨论】:

  • 您的代码并没有特别不同。 StringFormat 是在哪里定义的,你是否包含了必要的标题?
  • @bot StringFormat 不需要定义。编译器抱怨它没有被声明。
  • 您的代码看起来不错,但没有声明类(或结构?)StringFormat,可能是因为您忘记包含声明它的标头。
  • 那可能是Gdiplus::StringFormat。您必须给编译器或我们提供一些线索。
  • 它是 Gdiplus::StringFormat 并且包含头文件。

标签: c++ windows gdi+


【解决方案1】:

您需要using Gdiplus::StringFormat。名称“StringFormat”相当通用,因此 C++ 具有 namespaces 以防止 2 个库之间的名称冲突。 GDI+ 将其名称放在namespace Gdiplus

【讨论】:

  • 我认为应该只是using Gdiplus; 而不是using Gdiplus::StringFormat;。或者使用Gdiplus::StringFormat 而不是StringFormat
  • @Jabberwocky:如果有的话,那就是using namespace Gdiplus。但就像using namespace std 一样,这是一种相当不精确的方法。
  • 是的,当然是using namespace Gdiplus;,这是一个错字。但无论如何我的评论毫无意义,我会删除它,包括这个。紫外线是我的。
  • 非常感谢,已经解决了!!
猜你喜欢
  • 2020-11-08
  • 2013-08-11
  • 1970-01-01
  • 2013-03-16
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 2012-11-28
相关资源
最近更新 更多