C++ 中的问题具有静态变量和函数的类

发布于 2024-11-29 18:26:22 字数 693 浏览 3 评论 0原文

有人可以告诉我下面的类有什么问题吗,g++ 在 ubuntu 上给出错误:

class FibonacciGenerator
{
    private:
        static int num1, num2, counting;

    public:

        static void Reset()
        {
            num1 = 0; num2 = 1;
            counting = 1;
        }

        static int GetCount()
        {
            return counting;
        }

        static int GetNext()
        {
            int val = 0;
            if(counting == 1) val = num1;
            else if(counting == 2) val = num2;
            else 
            {
                val = num1 + num2;
                num1 = num2;
                num2 = val;
            }
            counting ++;

            return val;
        }
};

Can someone tell me what is problem in the following class, g++ is giving errors on ubuntu:

class FibonacciGenerator
{
    private:
        static int num1, num2, counting;

    public:

        static void Reset()
        {
            num1 = 0; num2 = 1;
            counting = 1;
        }

        static int GetCount()
        {
            return counting;
        }

        static int GetNext()
        {
            int val = 0;
            if(counting == 1) val = num1;
            else if(counting == 2) val = num2;
            else 
            {
                val = num1 + num2;
                num1 = num2;
                num2 = val;
            }
            counting ++;

            return val;
        }
};

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

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

发布评论

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

评论(2

乞讨 2024-12-06 18:26:22

类定义中的语句static int num1, num2,counting;并没有定义这些变量,它只是声明它们。如果使用它们,也必须对其进行定义。

完整的示例如下:

//Begin FibonacciGenerator.hpp
#ifndef FIBONACCI_GENERATOR_HPP
#define FIBONACCI_GENERATOR_HPP
class FibonacciGenerator
{
private:
    static int num1, num2, counting;

public:
/* as above */
};
#endif //FIBONACCI_GENERATOR_HPP
//End FibonacciGenerator.hpp

//Begin FibonacciGenerator.cpp
#include "FibonacciGenerator.h"
int FibonacciGenerator::num1;
int FibonacciGenerator::num2;
int FibonacciGenerator::counting;
//End FibonacciGenerator.cpp

如果 FibonacciGenerator 在名称空间中声明,则这些静态成员定义也必须位于该名称空间中。

像这样使用静态成员可能是一个非常糟糕的主意。最好将它们设置为实例变量,这样您就可以在代码的不同部分中拥有多个独立的斐波那契生成器。

The statement static int num1, num2, counting; in a class definition does not define those variables, it only declares them. If they are used they must also be defined.

A complete example of this is as follows:

//Begin FibonacciGenerator.hpp
#ifndef FIBONACCI_GENERATOR_HPP
#define FIBONACCI_GENERATOR_HPP
class FibonacciGenerator
{
private:
    static int num1, num2, counting;

public:
/* as above */
};
#endif //FIBONACCI_GENERATOR_HPP
//End FibonacciGenerator.hpp

//Begin FibonacciGenerator.cpp
#include "FibonacciGenerator.h"
int FibonacciGenerator::num1;
int FibonacciGenerator::num2;
int FibonacciGenerator::counting;
//End FibonacciGenerator.cpp

If FibonacciGenerator is declared in a namespace, then these static member definitions must also be in that namespace.

Using static members like this is probably a very bad idea. It would be better to make them instance variables, so that you could have multiple independent FibonacciGenerators in separate parts of the code.

瀞厅☆埖开 2024-12-06 18:26:22

您只声明了静态成员。 ,您还应该在 cpp 文件中定义它们

int FibonacciGenerator::num1 /*= 0*/;
int FibonacciGenerator::num2 /*= 1*/;
int FibonacciGenerator::counting /*= 0*/;

当您在类中编写时

static T v; //if static were removed, this would also be a definition

,它只是一个声明,但每个程序都必须包含所使用的每个变量的一个定义(根据“单一定义规则”)。哈

You have only declared the static members. You should also define them in a cpp file like this

int FibonacciGenerator::num1 /*= 0*/;
int FibonacciGenerator::num2 /*= 1*/;
int FibonacciGenerator::counting /*= 0*/;

when you write

static T v; //if static were removed, this would also be a definition

inside a class, it is only a declaration, but every program must contain exactly one definition of every variable that is used (as per the One-Definition-Rule). hth

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