problem using sets strings and namespaces

J

JBorges

I created a namespace Directory:

#include <set>
#include <string>

namespace Directory {
bool putInSet( const char *filename, std::set< std::string >
&setstrings );
}

the namespace has other functions, but they're irrelevant for now...
so, when I compile a file:

int main()
{
std::set< std::string > setstrings;
std::string filename( "anyfile" );
Directory::putInSet( filename.c_str(), setstrings );

return 0;
}


the gcc 3.3.5 ( using Linux ) shows the following error:

filesServer.cpp:(.text+0x192): undefined reference to
`Directory::putInSet(char const*, std::set<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::less<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >&)'
collect2: ld returned 1 exit status

We know that string::c_str() returns a const char *. Why, in the
message error the compiler shows char const * ? Anyone can give me a
solution?
 
C

Christian Meier

JBorges said:
I created a namespace Directory:

#include <set>
#include <string>

namespace Directory {
bool putInSet( const char *filename, std::set< std::string >
&setstrings );
}

the namespace has other functions, but they're irrelevant for now...
so, when I compile a file:

int main()
{
std::set< std::string > setstrings;
std::string filename( "anyfile" );
Directory::putInSet( filename.c_str(), setstrings );

return 0;
}


the gcc 3.3.5 ( using Linux ) shows the following error:

filesServer.cpp:(.text+0x192): undefined reference to
`Directory::putInSet(char const*, std::set<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >,
std::less<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >&)'
collect2: ld returned 1 exit status

We know that string::c_str() returns a const char *. Why, in the
message error the compiler shows char const * ? Anyone can give me a
solution?

You have no implementation for the function Directory::putInSet().
const char * is the same as char const * (but not the same as char * const).
 
J

JBorges

oops, I didn't write the implementation of the function but I did it in
my code. I only forget to write it in the post. So, consider the
function implemented in a file. In my code I did:
in the "directory.h" i put the declaration of the namespace Directory
and in the "directory.cpp" I put the implementation of the functions of
the namespace.

const char * IS NOT the same as char const *.

const char * ensures that the CONTENT of the pointer cannot be
modified, but the pointer can be modified. If we have
const char *s = "test";
we can do
while( *s != '\0' ) {
cout << *s;
++s;
}

but we cannot do
*s = 'q';

char const * ensures that the pointer cannot be modified ( it can only
point to a place where it was initialized )
char const *s = "test";
we cannot do
while (*s != '\0' ) ++s;
but we can do
*s = 'q';

Thank you, but my problem is still unresolved, and I don't know if it
is my error or a compiler error.
 
R

red floyd

JBorges said:
const char * IS NOT the same as char const *.

const char * ensures that the CONTENT of the pointer cannot be
modified, but the pointer can be modified. If we have
char const * ensures that the pointer cannot be modified ( it can only
point to a place where it was initialized )

Wrong!

const char * = char const * = pointer to const char
char * const = const pointer to char.
 
T

Tobias Blomkvist

JBorges sade:
const char * IS NOT the same as char const *.

As a complement to red floyd's answer, consult the
C++ Standard §8.3 Meaning of Declarators

Tobias
 
J

JBorges

I was wrong. You're right.
thank you all. Maybe from now I can go on. Thanks again.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top