By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

problem using sets strings and namespaces

P: n/a
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?

Jul 29 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"JBorges" <jo*************@gmail.com> schrieb im Newsbeitrag
news:11**********************@g14g2000cwa.googlegr oups.com...
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).
Jul 29 '05 #2

P: n/a
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.

Jul 29 '05 #3

P: n/a
JBorges wrote:

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.
Jul 29 '05 #4

P: n/a
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
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 29 '05 #5

P: n/a
I was wrong. You're right.
thank you all. Maybe from now I can go on. Thanks again.

Jul 29 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.