473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

I get compile errors when compiling this class

I am trying to compile a string class which was working before but
subsequent attempts to make it more 'object orientated' (privatizing
the member variables and implenting query functions) it refuses to
compile in Visual C++.

Here is the code: http://www.rbf.org.uk/xstring.h

When compiling I get the following:

\xstring.h(87) : error C2662: 'query_length' : cannot convert 'this'
pointer from 'const class string' to 'class string &'
Conversion loses qualifiers
\xstring.h(101) : error C2662: 'query_length' : cannot convert 'this'
pointer from 'const class string' to 'class string &'
Conversion loses qualifiers
\xstring.h(114) : error C2662: 'query_length' : cannot convert 'this'
pointer from 'const class string' to 'class string &'
Conversion loses qualifiers
\xstring.h(126) : error C2662: 'query_value' : cannot convert 'this'
pointer from 'const class string' to 'class string &'
Conversion loses qualifiers
\xstring.h(138) : error C2662: 'query_value' : cannot convert 'this'
pointer from 'const class string' to 'class string &'
Conversion loses qualifiers
\xstring.h(153) : error C2662: 'query_length' : cannot convert 'this'
pointer from 'const class string' to 'class string &'
Conversion loses qualifiers
\xstring.h(154) : error C2662: 'query_value' : cannot convert 'this'
pointer from 'const class string' to 'class string &'
Conversion loses qualifiers

and so on and so on...
c:\documents and settings\rob\my documents\my programming
projects\xlibs\xstringtest\xstringtest.cpp(13) : error C2248: '=' :
cannot access private member declared in class 'string'
c:\documents and settings\rob\my documents\my programming
projects\includes\xstring.h(169) : see declaration of '='

What's going on? I can only rectify this problem by making the
variables public and removing the 'const' from the function
parameters. However it should not be trying to modify any of these
variables, so why is it causing errors? It doesn't make any sense to
me...
Jul 22 '05 #1
3 2180
Rob F wrote:
I am trying to compile a string class which was working before but
subsequent attempts to make it more 'object orientated' (privatizing
the member variables and implenting query functions) it refuses to
compile in Visual C++.

Here is the code: http://www.rbf.org.uk/xstring.h

When compiling I get the following:

\xstring.h(87) : error C2662: 'query_length' : cannot convert 'this'
pointer from 'const class string' to 'class string &'
Conversion loses qualifiers
....
c:\documents and settings\rob\my documents\my programming
projects\xlibs\xstringtest\xstringtest.cpp(13) : error C2248: '=' :
cannot access private member declared in class 'string'
c:\documents and settings\rob\my documents\my programming
projects\includes\xstring.h(169) : see declaration of '='
The compiler is right. Your operator= is declared private. Btw, this
operator should really return a reference to your string. As it is now,
it will always copy your string just to destroy that copy afterwards.

What's going on? I can only rectify this problem by making the
variables public
It's not the variables you need to make public, but all your operators.
When you made the member variables public, you made the operators
public too, since there is no other access specifier between them.
and removing the 'const' from the function parameters.
You need to tell your compiler, which member functions do not modify the
object, so that it knows that it can call them on const objects.
query_length() is declared as:

unsigned long query_length()

So your query_length() function is declared as modifying the string.
Try:

unsigned long query_length() const

This will tell the compiler that query_length doesn't modify the string
and thus can be called on a const string object.

However it should not be trying to modify any of these
variables, so why is it causing errors? It doesn't make any sense to
me...


Btw: comparison of strings usually means lexographically comparing them.
You are just comparing the length. And the comparison operators should
actually return a bool, not an int. And it seems that your string class
is missing a copy constructor, so actually, copying your string won't
work as it should.

Jul 22 '05 #2
You have some const correctness errors. You just need to change you code to this :

char * query_value() const
{
DBM("Querying string value.");
return contents;
}

unsigned long query_length() const
{
DBM("Querying string length.");
return length;
}

However, are you sure query_value should return char * instead of char const *?
Jul 22 '05 #3
> You need to tell your compiler, which member functions do not modify the
object, so that it knows that it can call them on const objects.
query_length() is declared as:

unsigned long query_length()

So your query_length() function is declared as modifying the string.
Try:

unsigned long query_length() const

This will tell the compiler that query_length doesn't modify the string
and thus can be called on a const string object.
I wasn't aware of this.

Btw: comparison of strings usually means lexographically comparing them.
You are just comparing the length.
Well I did for the == operator, how exactly would I determine if an
object is 'less than' or 'greater than' an object if not by length?
And the comparison operators should
actually return a bool, not an int.
I had considered this but I thought that since !!expression is always
an integer then the return value should be.
And it seems that your string class
is missing a copy constructor, so actually, copying your string won't
work as it should.
I forgot all about copy constructors! Thanks.

However, are you sure query_value should return char * instead of char const *?


I think it should return non-const value since the variable is meant
to be modified...
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Peter _Grafenberger | last post by:
hi, i've some troubles compiling Bjarne Strostrup's Matrix class example http://www.research.att.com/~bs/matrix.c under ms visual c++ 6.0 (sp4). i get a lot off errors like ...
4
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a...
3
by: Andrew Luke | last post by:
Hi all you C++ guru's! I'm 'very, very' new to C++ and I'm having a little trouble configuring my VS environment I think - when I try and compile some sample code I'm getting the following...
10
by: Chris LaJoie | last post by:
Our company has been developing a program in C# for some time now, and we haven't had any problems with it, but just last night something cropped up that has me, and everyone else, stumped. I...
2
by: Qiao Yun | last post by:
I used vc++.net (visual studio .net ) to open a project which can work well in vc++6.0. I succeeded in compiling the project in vc++.net in release mode . But when I tried to compile the project...
4
by: Seok Bee | last post by:
Dear Experts, I've completed my console application and now wants to compile the application and an executable file to be run at another location. However, I've tried compiling that program in...
0
by: ufnuceda | last post by:
Hello everyone, I was wondering if any of you have some experience with the boost library. I am having trouble compiling code with it. Since boost is being used a lot these days I thought some...
2
by: donkeyboy | last post by:
All, I've tried the jythonc compiler to try and create an applet to see how it works, but I get a number of Java compile errors that are way above my knowledge. Does anyone know what any of the...
2
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.