?
Solved

Constant definitions in a class

Posted on 2003-03-15
2
Medium Priority
?
279 Views
Last Modified: 2010-04-01
I tried to write a circle class and everything worked excepts for the statement:

const double PI = 3.14159;

VC++ gave me some errors that made no sense at all:

C:\Windows\Desktop\My C++\Classes\MyClasses\circle.h(36) : error C2258: illegal pure syntax, must be '= 0'
C:\Windows\Desktop\My C++\Classes\MyClasses\circle.h(36) : error C2252: 'PI' : pure specifier can only be specified for functions
C:\Windows\Desktop\My C++\Classes\MyClasses\CircleImp.cpp(43) : error C2065: 'PI' : undeclared identifier

why can't I declare a constant???
0
Comment
Question by:TrueIdiot
2 Comments
 
LVL 1

Accepted Solution

by:
vadik earned 225 total points
ID: 8143441
Try this way:

class A
{

public:
     A():PI(3.14159){};
     const double PI;

};
0
 
LVL 1

Expert Comment

by:frogger1999
ID: 8143469
or for a static memeber

class foo{

    public:

    foo();

    protected:
    static const double bar;
};

const double foo::bar = 1.0;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question