?
Solved

Constant definitions in a class

Posted on 2003-03-15
2
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

762 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