I get compile errors when compiling this class

R

Rob F

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...
 
R

Rolf Magnus

Rob said:
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.
 
K

Keith H Duggar

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 *?
 
R

Rob F

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top