我的C++类无法链接到另一个.cpp文件中的函数

Pla*_*oks 1 c++ linker class

我昨天问了同样的问题,答案不适用.

/sf/ask/433620491/

如果我进入类标题,右键单击该函数并单击"转到定义",这使我在我的其他.CPP文件中使用了我的函数.它看到它,可以链接到它,但我仍然得到错误,表明我看不到它.

有人有什么建议吗?我会尝试任何事情.

这是错误.

zdll.lib(d000050.o):警告LNK4078:'.text'找到具有不同属性的多个部分(E0300020)

WLD.obj:错误LNK2019:"public: void __thiscall WLD::fragment_03(unsigned char *,int)" (?fragment_03@WLD@@QAEXPAEH@Z)函数中引用的未解析的外部符号"public: bool __thiscall WLD::init(unsigned char *)" (?init@WLD@@QAE_NPAE@Z)

编辑:另外,我正在使用MSVC++.我应该尝试创建新解决方案并导入文件吗?可能会有所帮助,因为我觉得我没有选择......

编辑:这是代码:

#include "WLD.h"

inline void WLD::fragment_03(uchar* location, int frag_num)
{
    // Read the struct into memory and create a temporary pointer
    struct_frag03 temp03;
    memcpy(&temp03, location, sizeof(struct_frag03));
    uchar* temp_p = location;

    // Advance the pointer to the encoded bytes (filename)
    temp_p += sizeof(long) + sizeof(short);

    // Grab the encoded filename and decode it
    uchar* f_filename = new uchar [sizeof(temp03.nameLen + 1)];
    memcpy(f_filename, temp_p, temp03.nameLen + 1);
    decode(f_filename, temp03.nameLen);

    // Add the details about this bitmap to the array
    bmp_array[current_bmp].filename = f_filename;
    bmp_array[current_bmp].nameLength = temp03.nameLen;
    bmp_array[current_bmp].reference03 = frag_num;

    // 0x03 Debug
    //errorLog.OutputSuccess("0x03 Filename: %s", bmp_array[current_bmp].filename);
    //errorLog.OutputSuccess("0x03 Name length: %i",bmp_array[current_bmp].nameLength);
    //errorLog.OutputSuccess("0x03 Reference: %i", bmp_array[current_bmp].reference03);

    // Add the bitmap to the count
    current_bmp++;
}
Run Code Online (Sandbox Code Playgroud)

这里是WLD类中调用代码的地方:

case 0x03:
    fragment_03(wld + file_pos + sizeof(struct_wld_basic_frag), i);
    break;
Run Code Online (Sandbox Code Playgroud)

这是头文件声明:in(WLD.h):

public:
    inline void fragment_03(uchar* location, int frag_num);
Run Code Online (Sandbox Code Playgroud)

Bil*_*eal 7

inline意味着该函数有效地具有内部链接(即,它必须存在于使用它的翻译单元内).将函数的定义移动到标题中,或者删除inline.

(inline对于现代编译器而言,实际上意味着"使用内部链接" - 编译器将内联它自己有意义的内容,并且它们通常比人类做出更好的决策)

编辑:从技术上讲,标准使用的语言说内联函数有外部链接; 但是,它也在An inline function shall be defined in every translation unit in which it is used.标准的第3.2节第3段和There can be more than one definition of a ... inline function with external linkage (7.1.2) ... Given such an entity named D defined in more than one translation unit ... each definition of D shall consist of the same sequence of tokens第5 段中说过.因此从技术上讲,你所声明的内联名称可以从给定的翻译单元外部访问,这样做会导致C++中出现未定义的行为.


sbi*_*sbi 6

这与包含无关.你有什么链接器错误.包含所有关于编译的内容,这需要引用标识符的声明.
您遇到的错误意味着链接器无法在传递给它的任何目标文件中找到函数的定义,该文件由一个或多个调用.(请参阅此答案,了解什么是声明,什么是定义以及它们需要什么.)

你需要传递给链接器进行编译创建的目标文件的所有您的.cpp文件.如果您正在使用某种IDE,那么如果您将所有.cpp文件添加到项目中,它应该为您执行此操作.如果您正在使用makefile,.cpp请将所有文件列为目标的依赖项.如果通过手动调用编译器(然后调用链接器)进行.cpp编译,则在同一调用中将所有文件传递给它.