help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Compilation issues with std::string


From: wazz
Subject: Compilation issues with std::string
Date: 12 Feb 2007 05:41:42 -0800
User-agent: G2/1.0

Hi,

I'm trying to define a simple class which extends std::string, as well
as provides a method for C-style typecast. The code snippet is:
------------test.cpp----------------
#include <iostream>
#include <string>

class foo : public std::string
{
    public:
    foo(const char * str):std::string(str){}

    operator const char*() const { return c_str(); }
};

int main()
{
    foo s("test");
    std::cout<<s[2]<<std::endl;

    const char* ptr = (const char*)s;
    std::cout<<ptr<<std::endl;
}

------------test.cpp----------------

This code works fine for the following cmdline:
g++ test.cpp

...but, fails to compile when I compile for 32-bit platform:
g++ -m32 test.cpp

..with the following error:

test.cpp: In function `int main()':
test.cpp:18: error: ISO C++ says that these are ambiguous, even though
the worst conversion for the first is better than the worst conversion
for the second:
/usr/lib/gcc/x86_64-redhat-linux/3.4.4/../../../../include/c++/3.4.4/
bits/basic_string.h:642: note: candidate 1: typename _Alloc::reference
std::basic_string<_CharT, _Traits, _Alloc>::operator[](typename
_Alloc::size_type) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]
test.cpp:18: note: candidate 2: operator[](const char*, int) <built-
in>

I'm confused with the error above, which gives no hints about the
actual issue. It looks to me a typical template related compile time
error which are really hard to understand just by their description.

Is it a bug in g++ or am I missing something?

Any solutions would be of great help.

Here is the system info I'm using:

L^ggne0092:~>g++ -v
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.4/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
disable-checking --with-system-zlib --enable-__cxa_atexit --disable-
libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.4 20050721 (Red Hat 3.4.4-2)
L^ggne0092:~>uname -srvmpio
Linux 2.6.9-22.EL #1 Mon Sep 19 17:49:49 EDT 2005 x86_64 x86_64 x86_64
GNU/Linux

Thanks,
Wasif



reply via email to

[Prev in Thread] Current Thread [Next in Thread]