error: 'X' was not declared in this scope
-
Hi, bin neu hier und habe ein Problem. Schreibe gerade ein Programm und bekomme dauernd eine Fehler. Es sind eigentlich mehrere aber erstmal interessiert mich nur der Eine.
Der Code lautet:
#include <iostream> #include <math.h> using namespace std; int main (){ point *A,*B,*C; circle *K1,*K2,*K3; double r,r1,r2,r3; double x,y; //... //Punkt A cout << "Bitte x y für A eingeben : "; cin >> x >> y; A = new point(x,y); //Punkt B cout << "Bitte x y für B eingeben : "; cin >> x >> y; B = new point(x,y); //Punkt C cout << "Bitte x y für C eingeben : "; cin >> x >> y; C = new point(x,y); //Radien r1 = sqrt( pow( A.getX(),2 ) + pow( A.getY(),2 ) ); r2 = sqrt( pow( B.getX(),2 ) + pow( B.getY(),2 ) ); r3 = sqrt( pow( C.getX(),2 ) + pow( C.getY(),2 ) ); if(r1 > r2 && r1 > r3){ r = r1; } if(r2 > r1 && r2 > r3){ r = r2; // } else{ r = r3; } //Hilfskreise K1 = new circle(A, r); K2 = new circle(B, r); K3 = new circle(C, r); //... delete A; delete B; delete C; return 0; }nun kommt immer:
main.cpp:6:2: error: 'point' was not declared in this scope main.cpp:6:9: error: 'A' was not declared in this scope main.cpp:6:12: error: 'B' was not declared in this scope main.cpp:6:15: error: 'C' was not declared in this scope main.cpp:7:2: error: 'circle' was not declared in this scope main.cpp:7:10: error: 'K1' was not declared in this scope main.cpp:7:14: error: 'K2' was not declared in this scope main.cpp:7:18: error: 'K3' was not declared in this scopeAlso ich habe schon den Fehler gegooglet, aber meistens bezieht sich der Fehler darauf, dass std fehlt.
point und circle sind Klassen, die sich im selben Verzeichnis befinden. Wenn ich mit gedrückten Strg drauf klicke, springt es in die Klasse.
Ich benutze NetBeans IDE 7.2.1 mit MinGW.Mache ich was grundsätzlich falsch oder ist das ein IDE/Compiler Problem.
Ich hoffe die Infos reichen und mir kann eine helfen.
Gruß
POC
-
Es reicht nicht, dass sie sich im gleichen Verzeichnis befinden. Die Header müssen schon mit include in die Übersetzungseinheit eingebunden werden.
-
point und circle sind Klassen, die sich im selben Verzeichnis befinden
Das reicht leider nicht. Du musst die Datei(en) auch noch in den Programm per
#includeeinbinden, z.B.#include "point.h"
-
Erst einmal vielen Dank.
Habe noch nie Header erstellt und irgendwie gibt es da oder verschiedene Möglichkeiten, oder ich blicke das noch nicht so ganz.Ich habe meine Klasse point
#include <iostream> #include <math.h> #include "point.h" class point{ private: double x; double y; public: point(double x, double y){ this->x = x; this->y = y; } double getX(){ return x; } double getY(){ return y; } };Nun habe ich versucht eine Header Datei zu erstellen:
#ifndef POINT_H #define POINT_H #include <iostream> #include <math.h> double getX(); double getY(); #endif /* POINT_H */Also das kann nicht richtig sein. Sieht auch irgendwie aus, als ob in die Header Datei die Klasse kommt und die Funktionen in der cpp ausgeschrieben werden. Wie sollten die Dateien den aussehen, damit das funktioniert?
Gruß
POC
-
Das kannst du wie folgt machen (ich habs nicht kompiliert und getestet):
Point.hpp, das Header File:
#ifndef POINT_HPP #define POINT_HPP class Point { private: double _x, _y; public: Point(double x, double y); double get_x() const; double get_y() const; }; #endifPoint.cpp, die Implementierung:
#include "Point.hpp" Point::Point(double x, double y) : _x(x), _y(y) {} double Point::get_x() const { return _x; } double Point::get_y() const { return _y; }Falls du nicht verstehst wofuer das 'const' da ist, dann kannst du das fuers erste ignorieren, funktioniert auch ohne. Wenn du die Syntax bei der Implementierung des Konstruktors nicht verstehst, dann google mal nach "Initialization List".
-
Entweder so (alles inline):
point.h:
// #include <iostream> Wozu? // #include <math.h> 1. Wozu? 2. Das heißt cmath! math.h ist C. class point{ private: double x; double y; public: point(double x, double y): x(x), y(y){} // Nimm lieber eine Initialisierungsliste double getX() const{ // const-correctness beachten! return x; } double getY() const{ return y; } };Keine point.cpp
Oder so (nichts inline):
point.h:
// #include <iostream> Wozu? // #include <math.h> 1. Wozu? 2. Das heißt cmath! math.h ist C. class point{ private: double x; double y; public: point(double x, double y); double getX() const; double getY() const; };Mit point.cpp:
#include "point.h" point::point(double x, double y): x(x), y(y) { } double point::getX() const { return x; } double point::getY() const { return y; }Oder irgendetwas zwischen diesen beiden Extremen. Solche Einzeiler würde ich eher inline machen.
Alles ungetestet, können Flüchtigkeitsfehler drin sein.
edit: Zu langsam…
-
Vielen Dank, funktioniert xD.