include Problem
-
Hi,
mein Problem ist folgender Compiler-Fehler:
g++ -I/home/kerschbaum/PARO/additionalLibs/tree/tree-1.117 -I/home/kerschbaum/PARO/additionalLibs/gmp/gmp-4.1.4 -I/home/kerschbaum/PARO/additionalLibs/polylib/polylib-5.20.0/include -O0 -g -Wall -DLINEAR_VALUE_IS_LONGLONG prgtestmap.cc -c
In file included from prgtestmap.cc:1:
CTestMap.h:13: error: Syntaxfehler before;' token CTestMap.h:14: error: \
string' undeclared in namespace `std'
CTestMap.h:14: error: Syntaxfehler before `;' token
make: *** [prgtestmap.o] Fehler 1Ich habe folgende 3 Dateien: CTestMap.h, CTestMap.cc und prgtestmap.cc. Das Problem ist ziemlich einfach zu lösen, indem ich einfach nur die Systemheader "<string>", "<map>" und "<iostream>" im Header CTestMap.h includiere. Dies möchte ich allerdings nicht! Ich will im Header nichts includieren, sondern erst in der dazugehörigen CTestMap.cc. Ich habe nun schon einiges ausprobiert, aber ich stehe irgendwie auf dem Schlauch... wäre super, wenn jemand einen Lösungsansatz hätte. Tnx! Im folgenden die drei der Dateien:
// CTestMap.h #ifndef _ABCD_TESTER #define _ABCD_TESTER class CTestMap { public: CTestMap(); ~CTestMap(); void ttt(); private: std::string m_sss; std::map<std::string, int> m_map; }; #endif
//CTestMap.cc #include <iostream> #include <string> #include <map> #include "CTestMap.h" CTestMap::CTestMap() { // do nothing } CTestMap::~CTestMap() { // do nothing } void CTestMap::ttt() { std::string strTEST = "test"; std::string strSVN = "svn"; std::string strMAP = "map"; m_map[strTEST] = 43; m_map[strSVN] = -11; m_map[strMAP] = 100; std::cout << "svn --> " << m_map["svn"] << std::endl; std::cout << "map --> " << m_map["map"] << std::endl; }
//prgtestmap.cc #include "CTestMap.h" int main(void) { CTestMap* mm = new CTestMap(); mm->ttt(); return 0; }
Danke im Voraus,
svkers
-
Das Problem ist ziemlich einfach zu lösen, indem ich einfach nur die Systemheader "<string>", "<map>" und "<iostream>" im Header CTestMap.h includiere
<iostream> brauchst du nicht. Aber da CTestMap sowohl einen String, als auch eine Map als Member enthält muss die Definition von std::string und std::map hier sichtbar sein. Du musst also <string> und <map> inkludieren.
Im Normalfall könntest du auf Pointer-Member umsteigen. Dafür reicht eine Vorwärts-Deklaration. Allerdings können std-Klassen nicht vorwärts deklariert werden.
Wenn du wirklich unbedingt auf das Inkludieren der Header verzichten musst, hilft dir nur noch eine vollständige Isolierung der Member - PIMPL.class CTestMap { public: CTestMap(); ~CTestMap(); void ttt(); private: class Impl; Impl* impl_; }; // cpp-Datei class CTestMap::Impl { public: void ttt(); private: std::string m_sss; std::map<std::string, int> m_map; ... }; CTestMap::CTestMap() : impl_(new Impl()) {} void CTestMap::tt() { impl_->tt(); }
-
Hi HummeSikkins,
danke, was du schreibst klingt einleuchtend! Weshalb ich gefragt habe, liegt daran, dass ich beim includen von "<map>" in einem meiner Header (anderes Projekt) dann folgende mir nicht verständliche Compiler-Fehler bekomme...
/usr/include/g++/bits/stl_map.h:109:35: macro "value_compare" requires 2 arguments, but only 1 given
/usr/include/g++/bits/stl_map.h:109: error: invalid use of memberstd::map<\_Key, \_Tp, \_Compare, \_Alloc>::value_compare::comp' /usr/include/g++/bits/stl_map.h:109: error: parse error before
{' token
/usr/include/g++/bits/stl_map.h:109: error: missing ';' before right brace
/usr/include/g++/bits/stl_map.h:110: error: semicolon missing after declaration
ofstd::map<\_Key, \_Tp, \_Compare, \_Alloc>::value_compare' /usr/include/g++/bits/stl\_map.h:120: error: '\_Rep_type' is used as a type, but is not defined as a type. /usr/include/g++/bits/stl_map.h:122: error: parse error before
public'
/usr/include/g++/bits/stl_map.h:126: error:\_Rep_type' is not a class or namespace /usr/include/g++/bits/stl_map.h:126: error:
reference' is not a class or
namespace
/usr/include/g++/bits/stl_map.h:126: error: usingtypename' outside of template /usr/include/g++/bits/stl\_map.h:126: error:
_Rep_type' fails to be a typedef
or built in type
/usr/include/g++/bits/stl_map.h:127: error:\_Rep_type' is not a class or namespace /usr/include/g++/bits/stl\_map.h:127: error:
const_reference' is not a class or
namespace
/usr/include/g++/bits/stl_map.h:127: error: usingtypename' outside of template /usr/include/g++/bits/stl\_map.h:127: error:
_Rep_type' fails to be a typedef
or built in type
/usr/include/g++/bits/stl_map.h:128: error:\_Rep_type' is not a class or namespace /usr/include/g++/bits/stl_map.h:128: error:
iterator' is not a class or
namespace
/usr/include/g++/bits/stl_map.h:128: error: usingtypename' outside of template /usr/include/g++/bits/stl\_map.h:128: error:
_Rep_type' fails to be a typedef
or built in type
/usr/include/g++/bits/stl_map.h:129: error:\_Rep_type' is not a class or namespace /usr/include/g++/bits/stl\_map.h:129: error:
const_iterator' is not a class or
namespace
/usr/include/g++/bits/stl_map.h:129: error: usingtypename' outside of template /usr/include/g++/bits/stl\_map.h:129: error:
_Rep_type' fails to be a typedef
or built in type
/usr/include/g++/bits/stl_map.h:130: error:\_Rep_type' is not a class or namespace /usr/include/g++/bits/stl\_map.h:130: error:
size_type' is not a class or
namespace
/usr/include/g++/bits/stl_map.h:130: error: usingtypename' outside of template /usr/include/g++/bits/stl\_map.h:130: error:
_Rep_type' fails to be a typedef
or built in type
/usr/include/g++/bits/stl_map.h:131: error:\_Rep_type' is not a class or namespace /usr/include/g++/bits/stl\_map.h:131: error:
difference_type' is not a class or
namespace
/usr/include/g++/bits/stl_map.h:131: error: usingtypename' outside of template /usr/include/g++/bits/stl\_map.h:131: error:
_Rep_type' fails to be a typedef
or built in type
/usr/include/g++/bits/stl_map.h:132: error:\_Rep_type' is not a class or namespace /usr/include/g++/bits/stl_map.h:132: error:
pointer' is not a class or
namespace
/usr/include/g++/bits/stl_map.h:132: error: usingtypename' outside of template /usr/include/g++/bits/stl\_map.h:132: error:
_Rep_type' fails to be a typedef
or built in type
/usr/include/g++/bits/stl_map.h:133: error:\_Rep_type' is not a class or namespace /usr/include/g++/bits/stl\_map.h:133: error:
const_pointer' is not a class or
namespace
/usr/include/g++/bits/stl_map.h:133: error: usingtypename' outside of template /usr/include/g++/bits/stl\_map.h:133: error:
_Rep_type' fails to be a typedef
or built in type
/usr/include/g++/bits/stl_map.h:134: error:\_Rep_type' is not a class or namespace /usr/include/g++/bits/stl\_map.h:134: error:
reverse_iterator' is not a class
or namespace
/usr/include/g++/bits/stl_map.h:134: error: usingtypename' outside of template /usr/include/g++/bits/stl\_map.h:134: error:
_Rep_type' fails to be a typedef
or built in type
/usr/include/g++/bits/stl_map.h:135: error:\_Rep_type' is not a class or namespace /usr/include/g++/bits/stl\_map.h:135: error:
const_reverse_iterator' is not a
class or namespace
/usr/include/g++/bits/stl_map.h:135: error: usingtypename' outside of template /usr/include/g++/bits/stl\_map.h:135: error:
_Rep_type' fails to be a typedef
or built in type
/usr/include/g++/bits/stl_map.h: In functionint map()': /usr/include/g++/bits/stl_map.h:144: error: only constructors take base initializers /usr/include/g++/bits/stl\_map.h:144: error:
_Compare' undeclared (first use
this function)
/usr/include/g++/bits/stl_map.h:144: error: (Each undeclared identifier is
reported only once for each function it appears in.)
/usr/include/g++/bits/stl_map.h:144: error: `allocator_type' undeclared (first
use this function)
/usr/include/g++/bits/stl_map.h:144: confused by earlier errors, bailing out
make: *** [prog.o] Fehler 1Irgendwelche Ideen dazu???
svkers
-
Das Problem hat sich nun endlich in Luft aufgelöst... ich hab beim includen bockmist gebaut ;-)... leider kann ich es nicht näher erklären, da ich sonst zuviel Source-Code posten müsste und ihr dann bei der riese Menge (vermutlich) auch keine Lust mehr hättet, es anzusehen...
Danke!
svkers
-
nur für den fall, daß es doch noch hilft: erstens mußt du auch in prgtestmap.cc <map>, <string> und <iostream> includen. außerdem hast du nach den includes das using namespace std; vergessen.
svkers schrieb:
Das Problem ist ziemlich einfach zu lösen, indem ich einfach nur die Systemheader "<string>", "<map>" und "<iostream>" im Header CTestMap.h includiere. Dies möchte ich allerdings nicht!
warum nicht
der von deinem compiler gemeldete fehler ist genau der grund, warum die profis es immer so machen. wenn deine befürchtung ist, daß das kompilieren damit zu lange dauert, dann kann ich dich beruhigen. das
#ifndef _ABCD_TESTER #define _ABCD_TESTER #endif
sorgt nicht nur dafür, daß das mehrfache includen beim kompilieren zu keinem fehler fürt, zumindest der gcc erkennt dieses konstrukt und versucht dann erst garnicht die datei ein zweites mal zu includen. und wenn du mal mit was anderem als dem gcc arbeiten solltest, kannst du zusätzlich zu der #ifndef-klammer auch noch #pragma once einfügen. das wird von fast allen compilern unterstützt.
grundsätzlich sollte man in jeder quelltext-datei alle header includen, auf die in der datei unmittelbar bezug genommen wird. das gilt auch für selbstgeschriebene header-dateien. und das sebst dann, wenn es zu mehrfachen includes führt! die einzige ausnahme sind kleine programme mit einer zentralen header-datei.