Namespaces nutzen
-
[gelöst]
Hallo,
ich bin cpp-Neuling und möchte wissen, wie ich namespaces nutze, und dabei gleiche Funktionsnamen benutzen kann. Das habe ich ausprobiert:// main.cpp #include "func1.h" #include "func2.h" #include <iostream> #include <cstdlib> using namespace std; int main(void){ int z; cout << "Geben Sie eine Zahl ein:"; cin >> z; { using namespace func1; ausgabe(z); } system("PAUSE"); return EXIT_SUCCESS; } // func1.h #ifndef FUNC1_H #define FUNC1_H namespace func1 { const int ANZAHL = 3; void ausgabe(int x); } #endif //FUNC1_H // func2.h #ifndef FUNC2_H #define FUNC2_H namespace func2 { const int ANZAHL = 4; void ausgabe(int x); } #endif //FUNC2_H // func1.cpp #include "func1.h" #include <iostream> #include <cstdlib> using namespace std; void ausgabe(int z){ cout << "Das ist die Zahl minus 1" << z-1; } // func2.cpp #include "func2.h" #include <iostream> #include <cstdlib> using namespace std; void ausgabe(int z){ cout << "Das ist die Zahl" << z; } /* Ausgabe beim Kompilieren: 1> Generating Code... 1>func1.obj : error LNK2005: "void __cdecl ausgabe(int)" (?ausgabe@@YAXH@Z) already defined in func2.obj 1>main.obj : error LNK2019: unresolved external symbol "void __cdecl func1::ausgabe(int)" (?ausgabe@func1@@YAXH@Z) referenced in function _main */Ich hoffe ihr könnt mir weiterhelfen.
Gruß
-
Dass "namespace { .. }" musst du auch in der Source-Datei nutzen.
-
Wie meinst du das?? Ich bin nämlich C++-Neuling und kenne mich nicht so gut aus.
-
beuu schrieb:
// func1.cpp #include "func1.h" #include <iostream> #include <cstdlib> using namespace std; void ausgabe(int z){ cout << "Das ist die Zahl minus 1" << z-1; }Hier definierst du die Funktion "ausgabe" im globalen Namespace. Du willst die Funktion jedoch im Namespace func1 definieren. Also wäre folgendes richtig:
namespace func 1 { void ausgabe(int z) { ... } }
-
Danke dir
