user2413497
user2413497

Reputation: 211

how to assign typedef variables as static

can anyone please tell me the mistake in the below program.

#include <iostream>
using namespace std;

class A
{
        public:
        typedef int count;
        static count cnt ;

};

count A::cnt = 0;

int main()
{
    return 0;
}

error

count does not name a type

Upvotes: 4

Views: 323

Answers (2)

Bathsheba
Bathsheba

Reputation: 234785

You would have to use A::count A::cnt = 0; as your typedef is defined within the scope of class A.

i.e. either move the typedef outside the class or use scope resolution as above.

Upvotes: 13

bash.d
bash.d

Reputation: 13207

Your typedef is inside your class and as such not globally available.

You would need to

#include <iostream>
using namespace std;

typedef int count;


class A
{
        public:
        static count cnt ;

};

count A::cnt = 0;

int main()
{
    return 0;
}

Upvotes: 2

Related Questions