• <small id='E47HX'></small><noframes id='E47HX'>

  • <tfoot id='E47HX'></tfoot>

      <bdo id='E47HX'></bdo><ul id='E47HX'></ul>

    <legend id='E47HX'><style id='E47HX'><dir id='E47HX'><q id='E47HX'></q></dir></style></legend>

      <i id='E47HX'><tr id='E47HX'><dt id='E47HX'><q id='E47HX'><span id='E47HX'><b id='E47HX'><form id='E47HX'><ins id='E47HX'></ins><ul id='E47HX'></ul><sub id='E47HX'></sub></form><legend id='E47HX'></legend><bdo id='E47HX'><pre id='E47HX'><center id='E47HX'></center></pre></bdo></b><th id='E47HX'></th></span></q></dt></tr></i><div id='E47HX'><tfoot id='E47HX'></tfoot><dl id='E47HX'><fieldset id='E47HX'></fieldset></dl></div>
      1. C++ 静态变量和未解决的外部错误

        时间:2023-12-02
          <legend id='J4j9C'><style id='J4j9C'><dir id='J4j9C'><q id='J4j9C'></q></dir></style></legend>

          <small id='J4j9C'></small><noframes id='J4j9C'>

            <i id='J4j9C'><tr id='J4j9C'><dt id='J4j9C'><q id='J4j9C'><span id='J4j9C'><b id='J4j9C'><form id='J4j9C'><ins id='J4j9C'></ins><ul id='J4j9C'></ul><sub id='J4j9C'></sub></form><legend id='J4j9C'></legend><bdo id='J4j9C'><pre id='J4j9C'><center id='J4j9C'></center></pre></bdo></b><th id='J4j9C'></th></span></q></dt></tr></i><div id='J4j9C'><tfoot id='J4j9C'></tfoot><dl id='J4j9C'><fieldset id='J4j9C'></fieldset></dl></div>
            <tfoot id='J4j9C'></tfoot>

                <tbody id='J4j9C'></tbody>
                <bdo id='J4j9C'></bdo><ul id='J4j9C'></ul>

                • 本文介绍了C++ 静态变量和未解决的外部错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我希望我能对类的静态变量进行一些说明.

                  I was hoping I could get some clarification on static variables of a class.

                  例如:我有两个不同的类,它们执行完全不同的功能,alpha 和 beta.在 alpha 中,我声明了一个类型为 beta 的静态变量,如下所示:

                  For example: I have two different classes which perform completely different functions, alpha and beta. Within alpha, I declare a static variable of type beta so it looks like this:

                  //alpha.h
                  
                  #pragma once
                  #include <iostream>
                  #include "beta.h"
                  
                  class alpha{
                  public: 
                      alpha(){
                  
                      }
                  
                      static beta var; 
                  
                      void func(){
                          var.setX(3);
                      }
                  
                      void output(){
                  
                      }
                  };
                  
                  //beta.h
                  
                  #pragma once
                  #include <iostream>
                  using namespace std; 
                  
                  class beta{
                  
                  public: 
                  
                      int x; 
                      char y; 
                  
                      beta(){
                          x = 0; 
                          y = ' '; 
                      }
                  
                      void setX(int _X){
                          x = _X; 
                      }
                  
                  };
                  
                  //main.cpp
                  
                  #include <iostream>
                  #include <iomanip>
                  #include "alpha.h"
                  using namespace std; 
                  
                  int main(){
                      alpha x, y, z; 
                      x.func(); 
                  }
                  

                  现在当我尝试编译这个时,我得到一个未解决的外部错误:

                  Now when I try to compile this, I get an unresolved externals error:

                  错误 LNK2001: 未解析的外部符号 "public: static class betaalpha::var" (?var@2Vbeta@@A)

                  error LNK2001: unresolved external symbol "public: static class beta alpha::var" (?var@2Vbeta@@A)

                  我不确定要更改什么或我还需要添加什么才能使这样的工作正常工作.我希望 x、y 和 z 基本上共享相同的 beta 变量.我想我可以通过将一个 beta 变量传递给每个变量来完成同样的事情.但是我想知道是否可以在这里使用 static 关键字来做同样的事情,因为类的静态成员在类的任何实例中都具有相同的值.除非我的定义有误.

                  I'm not sure what to change or what else I need to add to make something like this work. I want x, y, and z to essentially share the same beta variable. I think I can accomplish this same thing by just passing by reference one beta variable into each of them. But I want to know if it's possible to do this same thing using the static keyword here because static members of a class have the same value in any instance of a class. Unless I have that definition wrong.

                  推荐答案

                  类中的静态变量仍然必须在某处定义,就像方法一样:

                  static variables inside a class must still be defined somewhere, just like methods:

                  把它放在 main.cpp 中:

                  Put this in main.cpp:

                  beta alpha::var;
                  

                  这篇关于C++ 静态变量和未解决的外部错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++“致命错误LNK1120"未解析的静态类成员 下一篇:如何在 C++ 中初始化私有静态常量映射?

                  相关文章

                  <i id='AReHs'><tr id='AReHs'><dt id='AReHs'><q id='AReHs'><span id='AReHs'><b id='AReHs'><form id='AReHs'><ins id='AReHs'></ins><ul id='AReHs'></ul><sub id='AReHs'></sub></form><legend id='AReHs'></legend><bdo id='AReHs'><pre id='AReHs'><center id='AReHs'></center></pre></bdo></b><th id='AReHs'></th></span></q></dt></tr></i><div id='AReHs'><tfoot id='AReHs'></tfoot><dl id='AReHs'><fieldset id='AReHs'></fieldset></dl></div>
                  <legend id='AReHs'><style id='AReHs'><dir id='AReHs'><q id='AReHs'></q></dir></style></legend>
                    <bdo id='AReHs'></bdo><ul id='AReHs'></ul>

                • <small id='AReHs'></small><noframes id='AReHs'>

                    1. <tfoot id='AReHs'></tfoot>