链接器找不到C ++静态成员 [英] linker cannot find a C++ static member

查看:205
本文介绍了链接器找不到C ++静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在X代码中写简单的C ++代码时,它显示了链接器错误。

When i write simple C++ code in X-code, it shows Linker Error.

架构x86_64的未定义符号:
Emp :: id ,引用自:
main.o中的Emp :: Emp()
ld:没有为架构x86_64找到符号
clang:error:linker命令失败,退出代码为1 -v查看调用)

Undefined symbols for architecture x86_64: "Emp::id", referenced from: Emp::Emp() in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

#include <iostream>
using namespace std;
class Emp
{

    public:
      static int id;
      int sal;

   Emp()
   {
      Emp::id =10;   // When i comment this line its working fine.
   };
};
int main(int argc, const char * argv[])
{
    Emp Ram;
    cout << Ram.sal ;
    return 0;
 }


推荐答案

$ c> id 作为静态变量。

You have declared id as a static variable. You then set it in every constructor call, which is probably not what you want to do.

对于一个'fix',你可以在main之上添加以下行:

For a 'fix', you can add the following line above main:

int Emp::id = 0;

但是,您可能不希望它是静态的。有关静态类变量的详细信息,请参见此页

However, you may not want that to be static. For more information on static class variables, see this page

这篇关于链接器找不到C ++静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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