我的C ++类无法链接到另一个.cpp文件中的函数 [英] My C++ class is having trouble linking to a function in another .cpp file

查看:352
本文介绍了我的C ++类无法链接到另一个.cpp文件中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



将类函数分解为多个.cpp文件



如果我进入类标题,右键单击该功能,单击转到定义,它需要我正确的我的功能在我的其他.CPP文件。它看到它,可以链接到它,仍然我会得到错误,表明我看不到它。



任何人有任何建议吗?



这是错误了。


zdll .lib(d000050.o):warning LNK4078:multiple '。text'使用不同属性找到的部分(E0300020)



WLD.obj:error LNK2019:unresolved external symbol 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 ++。我应该尝试创建一个新的解决方案并导入文件?

编辑:这是代码:

  #includeWLD.h

inline void WLD :: fragment_03(uchar * location,int frag_num)

//将结构体读入内存并创建一个临时指针
struct_frag03 temp03;
memcpy(& temp03,location,sizeof(struct_frag03));
uchar * temp_p = location;

//将指针指向编码字节(filename)
temp_p + = sizeof(long)+ sizeof(short);

//获取编码的文件名并解码它
uchar * f_filename = new uchar [sizeof(temp03.nameLen + 1)];
memcpy(f_filename,temp_p,temp03.nameLen + 1);
decode(f_filename,temp03.nameLen);

//将此位图的详细信息添加到数组
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文件名:%s,bmp_array [current_bmp] .filename);
//errorLog.OutputSuccess(\"0x03 Name length:%i,bmp_array [current_bmp] .nameLength);
//errorLog.OutputSuccess(\"0x03参考:%i,bmp_array [current_bmp] .reference03);

//将位图添加到计数
current_bmp ++;
}

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

  case 0x03:
fragment_03(wld + file_pos + sizeof(struct_wld_basic_frag),i);
break;

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

  public:
inline void fragment_03(uchar * location,int frag_num);


解决方案

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



现代编译器的内联真的意味着使用内部链接 - 编译器将自己编写自己的意义,他们通常比人类做出更好的决策)



EDIT :从技术上讲,标准使用的语言表示内联函数具有外部链接;然而,它还说在每个使用它的翻译单元中定义一个内联函数。在标准的第3.2节第3节,以及可以有一个以上的定义...内联函数
与外部链接(7.1.2)...给定一个实体名为D在多个翻译单元中定义...每个定义D在第5段中应该由相同的标记序列
组成。因此,虽然从技术上讲,您声明为内联的名称可以从给定的翻译单元之外访问,但这样做是为了引起C ++的未定义行为。 / p>

I asked the same question yesterday and the answer was not applicable.

Breaking up class functions into multiple .cpp files

If I go into the class header, right click on the function and click "Go to Definition" and it takes me right to my function in my other .CPP file. It sees it, can link to it and still I get errors that indicate I cannot see it.

Anyone have any suggestions? I'll try anything.

Here is the error again.

zdll.lib(d000050.o) : warning LNK4078: multiple '.text' sections found with different attributes (E0300020)

WLD.obj : error LNK2019: unresolved external symbol "public: void __thiscall WLD::fragment_03(unsigned char *,int)" (?fragment_03@WLD@@QAEXPAEH@Z) referenced in function "public: bool __thiscall WLD::init(unsigned char *)" (?init@WLD@@QAE_NPAE@Z)

Edit: Also, I am using MSVC++. Should I try creating a new solution and importing the files? May help as I feel I am out of options...

Edit: Here is the code:

#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++;
}

And here is where the code is called in the WLD class:

case 0x03:
    fragment_03(wld + file_pos + sizeof(struct_wld_basic_frag), i);
    break;

Here is the header file declaration: in (WLD.h):

public:
    inline void fragment_03(uchar* location, int frag_num);

解决方案

inline means that the function effectively has internal linkage (that is, it must exist inside the translation unit where it is used). Either move the definition of the function into the header, or remove inline.

(inline for modern compilers really means "use internal linkage" -- compilers will inline where it makes sense to by themselves, and they typically make better decisions than humans)

EDIT: Technically speaking, the language the standard uses here says that inline functions have external linkage; however, it also says An inline function shall be defined in every translation unit in which it is used. in section 3.2 paragraph #3 of the standard, and also 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 in paragraph 5. So while technically speaking the name you declared inline is accessible from outside a given translation unit, to do so is to cause undefined behavior from C++.

这篇关于我的C ++类无法链接到另一个.cpp文件中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆