再次“未定义的符号”在运行时,c++

发布于 2024-11-26 23:11:41 字数 759 浏览 1 评论 0原文

我有一个数据类型,我可以实例化该类型的变量。像这样:

FetchAddr faddr(VirtualMemoryAddress( 0x0a ));

FetchAdr 的定义是:

struct FetchAddr {
   VirtualMemoryAddress theAddress;
   FetchAddr(VirtualMemoryAddress anAddress)
     : theAddress(anAddress)
   { } 
};

现在我有一个类,faddr 是一个私有(或公共)变量

class FLEXUS_COMPONENT(BPred)  {
   static FetchAddr faddr;
   public:
     FLEXUS_COMPONENT_CONSTRUCTOR(BPred)
        : base( FLEXUS_PASS_CONSTRUCTOR_ARGS )
     {
        faddr = VirtualMemoryAddress( 0x0a );
     }
   ...
}

假设宏定义正确。

代码编译和链接没有任何问题。然而,当我启动程序时,它说:

 "undefined symbol: _ZN6nBPred14BPredComponent8faddr"

它说没有 faddr 的符号。

有什么想法吗?

I have a data type and I can instantiate a variable of that type. like this:

FetchAddr faddr(VirtualMemoryAddress( 0x0a ));

The definition of FetchAdr is:

struct FetchAddr {
   VirtualMemoryAddress theAddress;
   FetchAddr(VirtualMemoryAddress anAddress)
     : theAddress(anAddress)
   { } 
};

Now I have a class that faddr is a private (or public) variable

class FLEXUS_COMPONENT(BPred)  {
   static FetchAddr faddr;
   public:
     FLEXUS_COMPONENT_CONSTRUCTOR(BPred)
        : base( FLEXUS_PASS_CONSTRUCTOR_ARGS )
     {
        faddr = VirtualMemoryAddress( 0x0a );
     }
   ...
}

Assume the macros are defined properly.

The code compiles and links without any problem. However when I start the program, it says:

 "undefined symbol: _ZN6nBPred14BPredComponent8faddr"

it says there no symbol for faddr.

any idea about that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2024-12-03 23:11:41

当您声明静态成员时,您还必须在某个地方定义它,例如在 .cpp 文件中。并且还要记住链接到该文件。


问题 2 - FetchAddr 没有默认构造函数。

如果您需要将 faddr 作为类的静态成员,则还需要在定义时为其指定一个值,例如:

FetchAddr FLEXUS_COMPONENT(BPred)::faddr(VirtualMemoryAddress( 0x0a ));

创建一个共享的 faddr由所有 FLEXUS_COMPONENT(BPred) 对象。

如果您希望每个对象都有自己的 faddr 变量副本,则可以将其设为非静态并在构造函数中对其进行初始化:

class FLEXUS_COMPONENT(BPred)  {
   FetchAddr faddr;
   public:
     FLEXUS_COMPONENT_CONSTRUCTOR(BPred)
        : base( FLEXUS_PASS_CONSTRUCTOR_ARGS ),
          faddr(VirtualMemoryAddress( 0x0a ))
     { }
   // ...
};

When you declare a static member you also have to define it somewhere, like in a .cpp file. And also remember to link to this file.


Problem number 2 - FetchAddr doesn't have a default constructor.

If you need to have faddr as a static member of the class, you also need to give it a value when it is defined, like:

FetchAddr FLEXUS_COMPONENT(BPred)::faddr(VirtualMemoryAddress( 0x0a ));

That creates an faddr that is shared by all FLEXUS_COMPONENT(BPred) objects.

If you rather have it that each object has its own copy of the faddr variable, you can make it non-static and initialize it in the constructor:

class FLEXUS_COMPONENT(BPred)  {
   FetchAddr faddr;
   public:
     FLEXUS_COMPONENT_CONSTRUCTOR(BPred)
        : base( FLEXUS_PASS_CONSTRUCTOR_ARGS ),
          faddr(VirtualMemoryAddress( 0x0a ))
     { }
   // ...
};
っ左 2024-12-03 23:11:41

您必须在其他地方定义静态变量。

FetchAddr FLEXUS_COMPONENT(BPred) :: faddr;

在单个 TU 中。

You must define the static variable elsewhere.

FetchAddr FLEXUS_COMPONENT(BPred) :: faddr;

In a single TU.

音栖息无 2024-12-03 23:11:41

尝试使用 -Wl,--no-undefined 进行编译,这样,如果库或任何其他依赖项中存在未定义的单个符号,链接器将拒绝完成链接。

faddr 尚未正确链接,并且在没有看到更多程序的情况下,很难判断还发生了什么。

Try compiling with the -Wl,--no-undefined so that the linker will refuse to complete the link if there is even a single symbol which is not defined in a library or any other dependencies.

faddr has not been properly linked and without seeing more of your program, it is hard to tell what else is going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文