【问题标题】:Weird unresolved external errors奇怪的未解决的外部错误
【发布时间】:2011-11-29 05:24:33
【问题描述】:

我遇到了一些未解决的外部错误,我似乎无法找到解决方案:

1>MyApp.obj : error LNK2019: unresolved external symbol "public: void __thiscall Path::AddPoint(struct Point2D const &)" (?AddPoint@Path@@QAEXABUPoint2D@@@Z) referenced in function "public: static long __stdcall MyApp::WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@MyApp@@SGJPAUHWND__@@IIJ@Z)
1>MyApp.obj : error LNK2019: unresolved external symbol "public: void __thiscall Path::ClrPath(void)" (?ClrPath@Path@@QAEXXZ) referenced in function "public: static long __stdcall MyApp::WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@MyApp@@SGJPAUHWND__@@IIJ@Z)
1>MyApp.obj : error LNK2019: unresolved external symbol "public: enum TOOL __thiscall Path::GetTool(void)" (?GetTool@Path@@QAE?AW4TOOL@@XZ) referenced in function "public: static long __stdcall MyApp::WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@MyApp@@SGJPAUHWND__@@IIJ@Z)
1>MyApp.obj : error LNK2019: unresolved external symbol "public: void __thiscall Path::SetTool(enum TOOL)" (?SetTool@Path@@QAEXW4TOOL@@@Z) referenced in function "public: static long __stdcall MyApp::WndProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WndProc@MyApp@@SGJPAUHWND__@@IIJ@Z)

奇怪的是,我很确定我正确地包含了所有内容。此外,该类总是有一个函数有效:DrawTo()。

我试图删除其他函数的“内联”声明,但这似乎并不重要。

我也尝试过重建一次,只是在两个函数之间添加了一些额外的结束线,然后它就编译好了!下次我尝试编译它并没有再次工作......

所以我不完全确定我做错了什么,或者它是否是编译器......(默认 VS2010 编译器)(实际上,那个编译器的名称是什么?[评论或编辑])

有人知道这意味着什么吗?

这是相关代码:(如果您需要查看更多内容,请发表评论,我会编辑)


路径.h

#pragma once
#ifndef PATH_H
#define PATH_H

#include "Defines.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#include "MyVector.h"

#include "Point2D.h"
#include "Tool.h"

class Path
{
public:
    Path();
    virtual ~Path();

    bool DrawTo(HDC hDC);

    inline void AddPoint(const Point2D& rkPoint);
    inline Point2D GetPointAt(int iIndex) const;
    inline int GetPointCount() const;

    inline TOOL GetTool();
    inline void SetTool(TOOL t);
    inline void SetColor1(COLORREF);
    inline void SetColor2(COLORREF);

    inline void ClrPath();

private:
    MyVector<Point2D> m_PointVector;
    TOOL m_Tool;

    COLORREF m_Colour1;
    COLORREF m_Colour2;
};

#endif

路径.cpp

#include "Path.h"

Path::Path()
{
    m_Tool = Tool_Pen;

    m_Colour1 = RGB(0,0,0);
    m_Colour2 = RGB(255,255,255);
}

Path::~Path()
{}

bool Path::DrawTo(HDC hDC)
{
    if(hDC == NULL || m_PointVector.GetLength() <= 0) {
        return false;
    }

    switch (m_Tool) {
    case Tool_Pen:
        {
            //////
            break;
        }
    case Tool_Line:
        {
            /////
            break;
        }
    case Tool_Ellipse:
        {
            //////
            break;
        }
    case Tool_Rectangle:
        {
            //////
            break;
        }
    case Tool_LineTrack:
        {
            //////
            break;
        }
    }

    return true;
}

Point2D Path::GetPointAt(int iIndex) const {
    return m_PointVector.At(iIndex);
}

int Path::GetPointCount() const {
    return m_PointVector.GetLength();
}

void Path::AddPoint(const Point2D& rkPoint) {
    m_PointVector.PushBack(rkPoint);
}

TOOL Path::GetTool() {
    return m_Tool;
}

void Path::SetTool(TOOL t) {
    m_Tool = t;
}

void Path::SetColor1(COLORREF col) {
    m_Colour1 = col;
}

void Path::SetColor2(COLORREF col) {
    m_Colour2 = col;
}

void Path::ClrPath() {
    m_PointVector.ClrAll();
}

MyApp.h

#pragma once
#ifndef MYAPP_H
#define MYAPP_H

#include "Defines.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <string>
#include <iostream>

//app includes:
#include "MyHelperFuncs_Win32.h"
#include "BitmapPainter.h"
#include "Path.h"
//-------------

class MyApp 
{
public:
    MyApp();
    virtual ~MyApp();
    static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    void Paint();

private:
    void InitWindows();

    HWND m_hWnd;
    HDC m_hDC;
    PAINTSTRUCT m_PaintStruct;

    //app-specific:
    BitmapPainter* m_pBitmapPainter;
    Path* m_pPath;
    //------------
};

#endif

MyApp::WndProc()

LRESULT MyApp::WndProc(HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    if(iMsg == WM_CREATE)
    {
        CREATESTRUCT *pCS = (CREATESTRUCT*)lParam;
        SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG)pCS->lpCreateParams);
    }
    else
    {
        //retrieve the stored "this" pointer
        MyApp* pApp = (MyApp*)GetWindowLongPtr(hWnd, GWLP_USERDATA);

        switch (iMsg)
        {
            case WM_PAINT:
                {
                pApp->Paint();
                return 0;
                }

            case WM_COMMAND:
            {
                int wmId    = LOWORD(wParam);
                int wmEvent = HIWORD(wParam);

                // Parse the menu selections:
                switch (wmId)
                {
                case IDM_NEW:
                    {
                    //////
                    return 0;
                case IDM_LOAD:
                    {
                    //////
                    return 0;
                    }
                case IDM_SAVE:
                    {
                    //////
                    return 0;
                    }
                case IDM_SAVEAS:
                    {
                    //////
                    return 0;
                    }
                case IDM_EXIT:
                    {
                    DestroyWindow(hWnd);
                    return 0;
                    }
                case IDM_COLOURMAIN:
                    {
                    //////
                    return 0;
                    }
                case IDM_COLOURSECONDARY:
                    {
                    //////
                    return 0;
                    }
                case IDM_PEN:
                    {
                        pApp->m_pPath->SetTool(Tool_Pen);
                        return 0;
                    }
                case IDM_LINE:
                    {
                        pApp->m_pPath->SetTool(Tool_Line);
                        return 0;
                    }
                case IDM_ELLIPSE:
                    {
                        pApp->m_pPath->SetTool(Tool_Ellipse);
                        return 0;
                    }
                case IDM_RECTANGLE:
                    {
                        pApp->m_pPath->SetTool(Tool_Rectangle);
                        return 0;
                    }
                case IDM_LINETRACK:
                    {
                        pApp->m_pPath->SetTool(Tool_LineTrack);
                        return 0;
                    }
                default:
                    {
                    //////
                    return 0;
                    }
                }
            }

            case WM_LBUTTONUP:
                {
                    OutputDebugString(_T("Left Button Up\n "));
                    if(wParam & MK_CONTROL)OutputDebugString(_T("The CTRL key is down.\n "));
                    int x = LOWORD(lParam);
                    int y = HIWORD(lParam);

                    switch(pApp->m_pPath->GetTool()) {
                        case Tool_Pen:
                            {
                            pApp->m_pPath->DrawTo(pApp->m_hDC);
                            pApp->m_pPath->ClrPath();
                            InvalidateRect(pApp->m_hWnd,NULL,true);
                            }
                    }

                    return 0;
                }

            case WM_LBUTTONDOWN:
                {
                    OutputDebugString(_T("Left Button Down\n "));
                    if(wParam & MK_CONTROL)OutputDebugString(_T("The CTRL key is down.\n "));
                    int x = LOWORD(lParam);
                    int y = HIWORD(lParam);

                    return 0;
                }

            case WM_RBUTTONUP:
                {
                    OutputDebugString(_T("Right Button Up\n "));
                    if(wParam & MK_CONTROL)OutputDebugString(_T("The CTRL key is down.\n "));
                    int x = LOWORD(lParam);
                    int y = HIWORD(lParam);

                    return 0;
                }
            case WM_MOUSEMOVE:
                {
                    OutputDebugString(_T("Mouse Moved\n "));
                    if(wParam & MK_CONTROL)OutputDebugString(_T("The CTRL key is down.\n "));
                    int x = LOWORD(lParam);
                    int y = HIWORD(lParam);
                    std::cout <<"Mouse Position: x="  << x << " y=" << y << "\n";

                    if (wParam & MK_LBUTTON) {
                        switch(pApp->m_pPath->GetTool()) {
                        case Tool_Pen:
                            {
                            Point2D p;
                            p.x = x;
                            p.y = y;
                            pApp->m_pPath->AddPoint(p);
                            InvalidateRect(pApp->m_hWnd,NULL,true);
                            }
                        }
                    }

                    return 0;
                }

            case WM_DESTROY:
                {
                PostQuitMessage(0);
                return 0;
                }
        }
    }
    return DefWindowProc (hWnd, iMsg, wParam, lParam) ;
}

【问题讨论】:

    标签: c++ winapi gdi unresolved-external


    【解决方案1】:

    取出那些“内联”声明(因为代码不会内联,除非它实际上在头文件中)。然后进行干净的构建,然后进行完整的构建。

    【讨论】:

    • 这似乎无关紧要(就像我写的那样)我之前完全像这样编译它,并且它有效。现在,当我尝试删除内联声明时,我仍然遇到同样的错误(抱歉,回复晚了,我睡着了:p)
    • 更新:最近,我删除了内联声明并重新编译。它没有用。之后,我在 Path.cpp 的两个函数体之间添加了一个换行符并编译了它(没有任何意义吧?)。在那之后,我重新添加了内联声明,它编译了……这里的 * 是什么意思?
    • 我的精神力量告诉我,你的构建状态很脏,只需要“重建干净”。或者您的 path.h 文件可能包含在 path.cpp 中,但不是 Visual Studio 中的项目文件之一。可能您的预编译头文件已过时。如有疑问,请从命令行执行“del /s *.obj”(对于 *.pch、*.lib、*.dll、*.exe 也是如此。)或者只是删除您的输出目录(调试和发布目录)。当我遇到没有意义的链接器问题时,这总是对我有用。
    【解决方案2】:

    唯一有效的函数是未内联的函数。

    如果你仔细想想,你会发现它是有道理的。当编译器编译 MyApp.cpp 时,它发现您正在调用一个声明为 inline 的方法。所以编译器需要复制该方法的代码,而不是仅仅调用它。问题是,编译器如何知道该方法是在哪里定义的?编译器可以看到它的唯一方法是内联方法的实现是在 Path.h 中还是在 MyApp.cpp 包含的其他一些头文件中。

    因此,最简单的解决方案是将所有这些内联方法的实现移动到 Path.h 文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-16
      • 2015-01-11
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 2014-05-07
      相关资源
      最近更新 更多