Solved

A const Problem

Posted on 2004-04-09
6
316 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Accepted Solution

by:
_ys_ earned 200 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 150 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video discusses moving either the default database or any database to a new volume.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now