第一次在这里发帖,因为我坚持使用我出色的 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) referenced in function "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 : fatal >error LNK1120: 1 unresolved externals
基本上我正在尝试创建一个用于创建 win32 控件和绘制文本的类,而绘制文本的函数正是我的问题所在。
代码如下:
The controls.h file segment:-
void printText( int R, int G, int B, int x, int y, LPCSTR text, HWND parent);
The controls.cpp segment
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);
}
The main.cpp call
controls ctrls;
ctrls->printText(255,0,0,300,50,"Test text",hWnd);
我已经删除了调用,但错误仍然存在。最初我也试图将 HDC 和 PAINTSTRUCT 传递给该函数,但我在尝试识别错误源时删除了它。
我完全迷路了,我不是一个了不起的 C++ 程序员,但我正在学习中。
批评我,我要求!
在此先感谢您提供的任何帮助,非常感谢:)
最佳答案
您忘记告诉编译器 controls.cpp
中的函数 printText
是 controls::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 位。如果我是对的,对于 R
、G
和 ,您应该使用
.unsigned char
而不是 int
B
关于c++ - 函数中的链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10960299/