g++: 'const' qualifiers cannot be applied to...

D

DanielESFA

Hey guys :)

This is a bit of a funny one... We're four guys working on the same project,
everybody using KDevelop and g++ on Linux. Three of us are using Mandrake,
with g++ 3.4.3 and 3.4.1. Project compiles nicely on these three puters.

On the fourth computer, running SUSE 9.3 using KDevelop also, but g++ 3.3.5,
and on the school's Debian server (which is the one that'll actually be
compiling the code eventually), also running g++ 3.3.5, we get a series of
"'const' qualifiers cannot be applied to..." errors when compiling.

This is the start of the .h file of the class that causes the error. The
error refers to the last line, i.e. the constructor declaration:

<code snippet>

#ifndef STDFILEMGR_H
#define STDFILEMGR_H

#include <dirent.h>
#include <fcntl.h>
#include <string>
#include "reporterfunctiontype.typedef"

namespace std
{
class FileManager
{
public:
FileManager(const string &root_, int bufferSize_, const
reporterFunctionType* reporter_);

</code snippet>

And the error (the reference to line 31 is actually the "FileManager..."
line above, I've not included the comment at the top of the .h file here)

<error>

g++ -c filemanager.cpp
In file included from filemanager.cpp:12:
filemanager.h:31: error: `const' qualifiers cannot be applied to `void (
(const
std::string&)'
filemanager.cpp:18: error: `const' qualifiers cannot be applied to `void
()(const std::string&)'
make: *** [filemanager.o] Error 1

</error>

Is this a known problem with this version of the g++ compiler? Or is it just
us doing something silly that the newer version of the g++ compiler accepts
even though it's not correct?

TIA,
Daniel :)
 
D

DanielESFA

DanielESFA said:
Is this a known problem with this version of the g++ compiler? Or is it
just us doing something silly that the newer version of the g++ compiler
accepts even though it's not correct?

It's us doing something that's not "correct". It was the const on the
function pointer that did it. No problem anymore, sorry ;)

Cheers,
Daniel :)
 
A

Andre Kostur

Hey guys :)

This is a bit of a funny one... We're four guys working on the same
project, everybody using KDevelop and g++ on Linux. Three of us are
using Mandrake, with g++ 3.4.3 and 3.4.1. Project compiles nicely on
these three puters.

On the fourth computer, running SUSE 9.3 using KDevelop also, but g++
3.3.5, and on the school's Debian server (which is the one that'll
actually be compiling the code eventually), also running g++ 3.3.5, we
get a series of "'const' qualifiers cannot be applied to..." errors
when compiling.

This is the start of the .h file of the class that causes the error.
The error refers to the last line, i.e. the constructor declaration:

<code snippet>

#ifndef STDFILEMGR_H
#define STDFILEMGR_H

#include <dirent.h>
#include <fcntl.h>
#include <string>
#include "reporterfunctiontype.typedef"

namespace std
{
class FileManager
{
public:
FileManager(const string &root_, int bufferSize_,
const
reporterFunctionType* reporter_);

</code snippet>

Uh.. you're not supposed to be adding anything to the std namespace....
 
D

DanielESFA

Andre said:
Uh.. you're not supposed to be adding anything to the std namespace....

LOL :D Forgive me for laughing, but without it you'd have to prepend "std::"
to just about anything you reference! :)
 
K

Karl Heinz Buchegger

DanielESFA said:
LOL :D Forgive me for laughing, but without it you'd have to prepend "std::"
to just about anything you reference! :)

Which is not a big problem in header files.
For source code files you can use 'using namspace std;'
 
L

Larry I Smith

DanielESFA said:
LOL :D Forgive me for laughing, but without it you'd have to prepend "std::"
to just about anything you reference! :)

What you are doing is putting YOUR code into the 'std' namespace
(by enclosing it in the 'namespace std {...}' block). That's not
normally done, and can be risky.

Remove the 'namespace std {...}' syntax and replace it with a
'using namespace std;' statement to accomplish what you want, e.g.:
 
L

Lionel B

DanielESFA said:
LOL :D Forgive me for laughing, but without it you'd have to prepend "std::"
to just about anything you reference! :)

No, you misunderstand. You're *adding your own code* into the "std" namespace. This is not usually done, as "std" is
reserved for the C++ standard library. It certainly runs the risk of confusing someone who had to use your code (as in
"What th'...? I don't recall there being a class called FileManager in the standard library?!?")

If you want to avoid having to prepend std:: to every standard library call you can use:

using namespace std;

However, it is generally frowned upon to use this in a *header* file, since you then enforce the usage on any
compilation unit which includes that header. So for your header files, perhaps you're stuck with prepending a lot of
std::'s ... been there ... get over it.
 
D

DanielESFA

Lionel said:
No, you misunderstand. You're *adding your own code* into the "std"
namespace. This is not usually done, as "std" is reserved for the C++
standard library. It certainly runs the risk of confusing someone who had
to use your code (as in "What th'...? I don't recall there being a class
called FileManager in the standard library?!?")

If you want to avoid having to prepend std:: to every standard library
call you can use:

using namespace std;

Well, forgive me for shutting up then :D I thought it was the same thing.
Never actually used the "namespace std { //code };" way of doing it until
using kdevelop which automatically does it when I create classes. Guess
what it means by the namespace question is which namespace I'd like to add
it to... Ahem... Silly me. Makes a lot of sense, really. Apologies for
laughing :/ And thanks to Andre for pointing it out. And thanks to the rest
of you for correcting me :) Better change the code before we hand in our
project, then ;) Hehe!
However, it is generally frowned upon to use this in a *header* file,
since you then enforce the usage on any
compilation unit which includes that header.

You mean that by including "using namespace std" in the header, I'd not only
have to be careful myself of name collisions, but other people using my
class would have to, too?
 
D

DanielESFA

Larry said:
What you are doing is putting YOUR code into the 'std' namespace
(by enclosing it in the 'namespace std {...}' block). That's not
normally done, and can be risky.

Remove the 'namespace std {...}' syntax and replace it with a
'using namespace std;' statement to accomplish what you want, e.g.:

Thanks, I will :) Was the old way of doing it, going back now ;)

Daniel
 
D

DanielESFA

Karl said:
Which is not a big problem in header files.
For source code files you can use 'using namspace std;'

Howcome it's okay in the cpp file but not in the h file? Is that because the
cpp file's scope is strictly within the class, so the std namespace would
be encapsuled here?
 
A

Andre Kostur

Lionel B wrote:


You mean that by including "using namespace std" in the header, I'd
not only have to be careful myself of name collisions, but other
people using my class would have to, too?

Yep... you'd be forcing your choice to loft all of std:: into the global
namespace onto everyone who #include's your header file.

See item #59 in "C++ Coding Standards" by Herb Sutter and Andrei
Alexandrescu. Nutshell, don't use a "using" statement before any
#include.... (and since your header file can't know what other header files
may have been #included before you, don't use "using"s in a header file at
all....)
 
A

Andre Kostur

Howcome it's okay in the cpp file but not in the h file? Is that
because the cpp file's scope is strictly within the class, so the std
namespace would be encapsuled here?

If it's only in your cpp file, then only _you_ need to deal with the
potential name clashes. By putting in a header file, then you, and
everbody who includes your header file would have to deal with it....
 
I

Ioannis Vranos

DanielESFA said:
namespace std
{
class FileManager
{
public:
FileManager(const string &root_, int bufferSize_, const
reporterFunctionType* reporter_);

</code snippet>


Don't ever place your facilities in the namespace std. Use "using std::" and "using
namespace std" statements, and place your facilities (both declarations and definitions)
inside a new namespace of yours:


namespace FileManagerApp
{
using std::cout;
using std::endl;

// ...
}


or


namespace FileManagerApp
{
using namespace std;

// ...
}
 
I

Ioannis Vranos

Larry said:
What you are doing is putting YOUR code into the 'std' namespace
(by enclosing it in the 'namespace std {...}' block). That's not
normally done, and can be risky.

Remove the 'namespace std {...}' syntax and replace it with a
'using namespace std;' statement to accomplish what you want, e.g.:

.
.
.
#include "reporterfunctiontype.typedef"

using namespace std;

class FileManager
{
...
}
.
.
.


This is not suitable for header files either. The usual (recommended) approach is to place
our own facilities in a new application namespace. Also this enables extension, we can
even use our application as a library/component of another application in the future.
 
I

Ioannis Vranos

Lionel said:
However, it is generally frowned upon to use this in a *header* file, since you then enforce the usage on any
compilation unit which includes that header. So for your header files, perhaps you're stuck with prepending a lot of
std::'s ... been there ... get over it.


using std:: statements in the global scope inside a header file, has the same global
namespace pollution effects. See my other messages on the recommended way. :)
 
L

Larry I Smith

Ioannis said:
This is not suitable for header files either. The usual (recommended)
approach is to place our own facilities in a new application namespace.
Also this enables extension, we can even use our application as a
library/component of another application in the future.

Yes, in my haste I failed to note that we were dealing with a header.
Sorry.

Regards,
Larry
 
I

Ioannis Vranos

Larry said:
Yes, in my haste I failed to note that we were dealing with a header.
Sorry.


Actually we should make what I mentioned for both our header and implementation files. In
all cases, we should place our own facilities inside a new application namespace. And then
we can use both "using std::" and "using namespace std" statements inside our application
namespace. But in general, we should still stick with local std:: statements in header
files, even inside in our namespace.
 
D

DanielESFA

Ioannis said:
Don't ever place your facilities in the namespace std. Use "using std::"
and "using namespace std" statements, and place your facilities (both
declarations and definitions) inside a new namespace of yours:


namespace FileManagerApp
{
using std::cout;
using std::endl;

// ...
}


or


namespace FileManagerApp
{
using namespace std;

// ...
}

Thank you for that tip! This will be the way I'll do it from now on :)
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top