You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在C++ Builder开发闪卡GUI应用时遇ilink32链接错误求助

C++ Builder闪卡应用链接错误解决办法

问题描述

使用C++ Builder开发带GUI的闪卡应用,当前GUI仅包含一个Label,尝试初始化显示英文单词时出现链接错误,无法完成编译链接。

错误信息

Checking project dependencies...
Compiling Project1.cbproj (Debug, Win32)
...bcc32c command line for "Project1PCH1.h"
[ilink32 Error] Error: Unresolved external '__fastcall TForm1::Label1Click(System::TObject 
*)' referenced from F:\C_PROJECTS\GUI_TESTFLASHCARD\WIN32\DEBUG\UNIT1.OBJ
[ilink32 Error] Error: Unable to perform link

Failed

Elapsed time: 00:00:03.5

现有代码

//---------------------------------------------------------------------------

#include <fmx.h>
#pragma hdrstop
#include<queue>

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"



class FlashCards
{
public:
    char* Word;
    char* Translation;

    FlashCards(){}

    FlashCards(char* word, char* translation){
        Word = word;
        Translation = translation;
    }
};

std::queue<FlashCards> LoadCards(){
    FlashCards q1 = FlashCards("hello", "bonjour");
    FlashCards q2 = FlashCards("what", "quoi");
    FlashCards q3 = FlashCards("who", "qui");

    std::queue<FlashCards> flashcards;
    flashcards.push(q1);
    flashcards.push(q2);
    flashcards.push(q3);

    return flashcards;

}

std::queue<FlashCards> flashcards;
FlashCards currentcard;

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    flashcards = LoadCards();
    currentcard =  flashcards.front();
    Label1->Text = currentcard.Word;
}

问题原因与解决方法

核心问题

链接错误的根源是:你在Form设计器中给Label1的OnClick事件绑定了Label1Click处理函数,但代码文件中没有实现这个函数,导致链接器找不到该函数的定义。

解决方式二选一:

  • 方式一:移除未实现的事件绑定
    打开C++ Builder的Form设计界面,选中Label1,打开右侧的对象查看器(Object Inspector),切换到Events标签页,找到OnClick事件,清空当前绑定的Label1Click(或选择下拉菜单中的None),保存后重新编译即可。

  • 方式二:实现Label的点击事件
    如果你需要Label点击后执行逻辑(比如切换显示翻译),在Unit1.cpp中添加Label1Click函数的实现,示例如下:

    //---------------------------------------------------------------------------
    void __fastcall TForm1::Label1Click(TObject *Sender)
    {
        // 示例:切换显示当前卡片的翻译
        Label1->Text = currentcard.Translation;
    }
    

    同时要确保Unit1.h中已经有该函数的声明(通常设计器会自动添加,如果没有,手动在TForm1类中添加:void __fastcall Label1Click(TObject *Sender);)。

额外优化建议

你的FlashCards类中使用char*存储字符串字面量,C++中字符串字面量是const char*类型,直接赋值会引发编译警告,建议将类成员修改为const char*:

class FlashCards
{
public:
    const char* Word;
    const char* Translation;

    FlashCards(){}

    FlashCards(const char* word, const char* translation){
        Word = word;
        Translation = translation;
    }
};

内容的提问来源于stack exchange,提问作者George

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 22:55:17