【问题标题】:Linker error in function函数中的链接器错误
【发布时间】:2012-06-13 04:32:13
【问题描述】:

第一次在这里发帖,因为我被我出色的 C++ 函数困住了。

我得到的错误是链接器错误,如下所示:

main.obj : error LNK2019: unresolved external symbol "public: void thiscall controls::printText(int,int,int,int,int,char const *,struct HWND *)" ( ?printText@controls@@QAEXHHHHHPBDPAUHWND__@@@Z) 在函数“long stdcall WndProc(struct HWND *,unsigned int,unsigned int,long)”(?WndProc@@YGJPAUHWND__@@IIJ@ Z)

C:\Users\HIDDEN\Documents\Visual Studio 2010\Projects\TimedShutdown\Debug\TimedShutdown.exe : 致命 >error LNK1120: 1 unresolved externals

基本上,我正在尝试创建一个用于创建 win32 控件和绘制文本的类,而绘制文本的功能是我的问题所在。

代码如下:

controls.h 文件段:-

void printText( int R, int G, int B, int x, int y, LPCSTR text, HWND parent);

controls.cpp 段

void printText(int R, int G, int B, int x, int y, LPCSTR text, HWND parent)
{
    HDC hdc;
    PAINTSTRUCT pss;
    hdc = BeginPaint(parent, &pss);
    SetBkMode(hdc, TRANSPARENT);
    SetTextColor(hdc, RGB(R,G,B));
    TextOut(hdc, 30, 20, text, strlen(text));       
    EndPaint(parent, &pss);
}

main.cpp 调用

controls ctrls;
ctrls->printText(255,0,0,300,50,"Test text",hWnd);

我已删除呼叫,但错误仍然存​​在。最初我也尝试将 HDC 和 PAINTSTRUCT 传递给该函数,但我在尝试识别错误源时已将其删除。

我完全迷路了,我不是一个了不起的 C++ 程序员,但我正在学习中。

批评我,我要求!

提前感谢您提供的任何帮助,非常感谢:)

【问题讨论】:

    标签: c++ function linker arguments linker-errors


    【解决方案1】:

    你忘了告诉编译器controls.cpp 中的函数printTextcontrols::printText。所以,对于编译器来说,它仍然是未定义的。

    你必须在controls.cpp做的修改:

    // This part is really important
    // It tells the compiler which function is defined
    //       |
    //   vvvvvvvvvv
    void controls::printText(int R, int G, int B, int x, int y, LPCSTR text, HWND parent)
    { // ...
    

    注意:传递给 printText 的颜色可能是 R8G8B8,即每个组件 8 位。如果我是对的,对于RGB,您应该使用unsigned char 而不是int

    【讨论】:

    • 这么简单的错误我忽略了=\非常感谢:D
    • 不客气 ;) 请不要忘记点击投票柜台底部的绿色勾号来接受 anwser。
    【解决方案2】:

    您尚未在定义它的函数名称中指定controls::。如果你不这样做,你就不能期望它表现得像控件类的成员函数。 试试这个而不是你当前的声明

    void controls::printText(int R, int G, int B, int x, int y, LPCSTR text, HWND parent)
    {
        HDC hdc;
        PAINTSTRUCT pss;
        hdc = BeginPaint(parent, &pss);
        SetBkMode(hdc, TRANSPARENT);
        SetTextColor(hdc, RGB(R,G,B));
        TextOut(hdc, 30, 20, text, strlen(text));       
        EndPaint(parent, &pss);
    }
    

    编辑:您在问题中提供的代码尚不清楚您实际上已将 printText 作为控件的成员函数,但您从代码中调用它的方式表明是你打算如何运作的。

    【讨论】:

      猜你喜欢
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多