如何将typedef变量赋值为静态

how to assign typedef variables as static

本文关键字:赋值 静态 变量 typedef      更新时间:2023-10-16

谁能告诉我下面程序中的错误?

#include <iostream>
using namespace std;
class A
{
        public:
        typedef int count;
        static count cnt ;
};
count A::cnt = 0;
int main()
{
    return 0;
}

错误

count没有指定类型

您必须使用A::count A::cnt = 0;,因为您的typedef是在类a的范围内定义的。

。要么将类型定义移出类,要么如上所述使用范围解析。

您的typedef在您的类中,因此不是全局可用的。

你需要

#include <iostream>
using namespace std;
typedef int count;

class A
{
        public:
        static count cnt ;
};
count A::cnt = 0;
int main()
{
    return 0;
}