PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : STL und Wechsel gcc 2.9.5->3.x Problem



Skipper
03-01-2003, 19:07
Hallo,
ich habe hier ein Programm, dass unter gcc 2.95 problemlos kompiliert. Mit gcc 3.x bricht es mit folgender Fehlermeldung ab:


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:


#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:


#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:


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...

tkortkamp
03-01-2003, 22:47
Entweder du fügst im Header using namespace std; ein oder schreibst anstatt string std::string

Skipper
04-01-2003, 05:37
Danke, das funktioniert!

anda_skoa
06-01-2003, 11:34
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,
_