Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

A const Problem

Posted on 2004-04-09
6
Medium Priority
?
327 Views
Last Modified: 2013-11-20
Hi

I have 2 functions declared in a class CCueSheet

int SongPercentage(const int TrackNum)const;
int GetTrackStart(const int nTrackNum, CString* pszStartTime = NULL);

Their definitation are

int CCueSheet::SongPercentage(const int TrackNum)const
{
  int nStart = GetTrackStart(TrackNum);
  return ((1000000/GetDurationSecs();) * nStart) / 10000;
}

int CCueSheet::GetTrackStart(const int nTrackNum, CString* pszStartTime)
{
  if (nTrackNum > m_naStartTimes.GetUpperBound())
    return -1;

  int nStart = m_naStartTimes.GetAt(nTrackNum);
  if (pszStartTime != NULL)      {
    *pszStartTime = CCommonFunctions::SecondsToHMSmS(nStart);
    *pszStartTime = pszStartTime->Left(8);
  }
  return nStart;
}

Now when I try to compile this I get the following error

error C2662: 'GetTrackStart' : cannot convert 'this' pointer from 'const class CCueSheet' to 'class CCueSheet &'
        Conversion loses qualifiers

But if I declare the SongPercentage function as
  int SongPercentage(const int TrackNum);

It works fine, why is this
0
Comment
Question by:Dj_Fx8
6 Comments
 
LVL 4

Expert Comment

by:bkfirebird
ID: 10794991
Not too sure but when you specify a member function as being const it means it promises not to alter the member data of an object of that class. Also it means that you're saying you won't call any functions which aren't also specified as being const.

When you call a const function on a non-const object the compiler won't allow it. The object will be passed as the 'this' pointer to the function. The 'this' will be non-const but the function will expect a const 'this' pointer. Hence the error message.

Member functions that modify the object would be non-const and functions that return data but don't modify anything are normally declared const.

0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10795255
bkfirebird is right, the const method is causing the problem because you are calling a non-const method.

There is one way to modify a member variable in a const method: If you declare the variable as "mutable" it can be modified. I think it's bad programming style, but it's in the C++ standard.
0
 
LVL 4

Expert Comment

by:mblat
ID: 10797450
try this

in .h

class CCueSheet{
...
int GetTrackStart(const int nTrackNum, CString* pszStartTime) const;
};

in .cpp
int CCueSheet::GetTrackStart(const int nTrackNum, CString* pszStartTime) const;


Of cause as prev. poster pointed out you won't be able to modfy any varables withing this class, but from the look of it you don't do it anyway.


Hope it helps...
0
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!

 
LVL 9

Accepted Solution

by:
_ys_ earned 800 total points
ID: 10816189
const member functions can only call other const member functions ...

Either declare them both as const (preferred as you don't actually changes any member variables) or declare them both as non-const.
0
 
LVL 7

Assisted Solution

by:vijay_visana
vijay_visana earned 600 total points
ID: 10820415
I think your are missing very simple rule of c++ a const memberfunction can not call non-const member function. make GetTrackStart const function and error will not show up.

Vijay
0
 

Author Comment

by:Dj_Fx8
ID: 10844674
Hi

Sorry for the delay, I got side tracked, having quickly looked over the comments provided ys and vijay gave what I feel as the simplest and most straight forward answers, so I split the points between them, thanks for all your replies.

Sometimes when self teaching you miss little basic things which come back to haunt you later :-?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

971 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