Anzeige:
Ergebnis 1 bis 4 von 4

Thema: STL und Wechsel gcc 2.9.5->3.x Problem

  1. #1
    Skipper
    Gast

    STL und Wechsel gcc 2.9.5->3.x Problem

    Hallo,
    ich habe hier ein Programm, dass unter gcc 2.95 problemlos kompiliert. Mit gcc 3.x bricht es mit folgender Fehlermeldung ab:
    Code:
    xf86configpath.h:39: 'string' is used as a type, but is not defined as a
    type. xf86configpath.cpp: In constructor
    `XF86ConfigPath::XF86ConfigPath()': xf86configpath.cpp:39: `Path'
    undeclared (first use this function) xf86configpath.cpp:39: (Each
    undeclared identifier is reported only once for each function it appears 
    in.)
    Der Header sieht so aus:
    Code:
    #ifndef XF86CONFIGPATH_H
    #define XF86CONFIGPATH_H
    
    #include <string>
    
    class XF86ConfigPath {
      
    public: 
      XF86ConfigPath();
      ~XF86ConfigPath();
    
      /** Returns Path variable */
      const char* get();
    
    private: // Private attributes
      /** Contains the path of XF86Config file */
      string Path;
    };
    
    #endif
    Und der Koerper so:
    Code:
    #include <stdlib.h>
    #include <unistd.h>
    
    #include <list>
    
    #include "xf86configpath.h"
    
    using namespace std;
    
    XF86ConfigPath::XF86ConfigPath(){
        list <string> searchPaths;
        searchPaths.push_back("/etc/X11/XF86Config-4");
        searchPaths.push_back("/etc/X11/XF86Config");
        searchPaths.push_back("/etc/XF86Config");
        searchPaths.push_back("/usr/X11R6/etc/X11/XF86Config-4");
        searchPaths.push_back("/usr/X11R6/etc/X11/XF86Config");
        searchPaths.push_back("/usr/X11R6/lib/X11/XF86Config-4");
        searchPaths.push_back("/usr/X11R6/lib/X11/XF86Config");
    
        list<string>::iterator it = searchPaths.begin();
        for (; it != searchPaths.end(); ++it )
          if ( !access( (Path = *it).c_str(), F_OK ) ) break;
    }
    
    XF86ConfigPath::~XF86ConfigPath(){
    }
    
    /** Returns path */
    const char* XF86ConfigPath::get(){
      return( Path.c_str() );
    }
    In dem Zusammenhang gleich noch eine Frage:
    Wie kann ich "#include <string>" im Header durch eine Forward Declaration ersetzen? Einfach "class string;" hinschreiben bringt nur neue Fehler:
    Code:
    f86configpath.cpp || echo './'`xf86configpath.cpp 
    In file included from xf86configpath.cpp:23: 
    xf86configpath.h:39: field `Path' has incomplete type 
    /usr/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/stl_list.h: In instantiation of
    `_List_node<string>': 
    /usr/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/stl_alloc.h:232: instantiated from
    `simple_alloc<_List_node<string>,__default_alloc_template<true,0>
    >::deallocate(_List_node<string> *, unsigned int)' 
    /usr/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/stl_list.h:169: instantiated from
    `_List_alloc_base<string,allocator<string>,>::_M_put_node(_List_node<string> *)'
    /usr/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/stl_list.h:193: instantiated from
    `_List_base<string,allocator<string> >::~_List_base()' 
    /usr/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/stl_list.h:437: instantiated from
    `list<string,allocator<string> >::~list()' 
    xf86configpath.cpp:28: instantiated from here 
    /usr/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/stl_list.h:43: invalid use of
    undefined type `class string' 
    xf86configpath.h:21: forward declaration of `class string' 
    /usr/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include/g++/stl_list.h:47: confused by earlier
    errors, bailing out 
    make[3]: *** [xf86configpath.lo] Error 1
    Ich hoffe, das war jetzt nicht zu viel auf einmal...

  2. #2
    Registrierter Benutzer
    Registriert seit
    10.04.2001
    Ort
    Bremen
    Beiträge
    339
    Entweder du fügst im Header using namespace std; ein oder schreibst anstatt string std::string

  3. #3
    Skipper
    Gast
    Danke, das funktioniert!

  4. #4
    Administrator Avatar von anda_skoa
    Registriert seit
    17.11.2001
    Ort
    Graz, Österreich
    Beiträge
    5.477
    Original geschrieben von tkortkamp
    Entweder du fügst im Header using namespace std; ein oder schreibst anstatt string std::string
    Ersteres solte man nicht machen, weil der Namespace sonst in allen Dateien offen ist, wo der Header inkludiert wurde.

    Das würde den Namespace std praktisch wieder zu eine globalen Namespace machen.

    Ciao,
    _
    Qt/KDE Entwickler
    Debian Benutzer

Lesezeichen

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •