HeaderFiles und CPPFiles



  • Ich arbeite gerade mein Buch durch, und dabei habe ich gerade das Thema Headerfiles und CPP Files.

    Ich will in mein Projekt in MS VS 2010 nun also in meine Win 32 Konsolen anwendung, ein headerfile hinzufügen, dass die methode abs hat, dann brauche ich ja noch ein cpp file, und eine main datei.

    Aber es kommt eine Fehlermeldung beim erstellen, kann mir einer helfen?

    Main.cpp

    // main.cpp
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include "math.h"
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {	
    	cout << "Wir werden die minus zahl jetzt positiv machen ! : ";
    	int zahl = 0;
    	cin >> zahl;
    	Math math;
    	zahl = math.abs(zahl);
    	cout << "Zahl wurde geändert, hier: "+zahl;
    
    	cout << "Drücken Sie eine beliebige Taste..." << endl;
    	cin.get();
    
    	return 0;
    }
    

    math.h

    //math.h
    
    #ifndef _MATH_H_
    
    class Math
    {
    public:
    	int abs(int number);
    };
    
    #endif
    

    Math.cpp

    // Math.cpp
    #include "math.h"
    
    int Math::abs(int number)
    {
    	int zahl = number;
    
    	if(zahl<0)
    		zahl /= -1;
    	else
    		;
    
    	return zahl;
    }
    

    Fehlermeldung:

    1>------ Build started: Project: C++ von A bis Z, Configuration: Debug Win32 ------
    1>Build started 28.05.2010 18:22:46.
    1>InitializeBuildStatus:
    1>  Touching "Debug\C++ von A bis Z.unsuccessfulbuild".
    1>ClCompile:
    1>  All outputs are up-to-date.
    1>  Math.cpp
    1>f:\c++ projekte\c++ von a bis z\c++ von a bis z\math.cpp(2): warning C4627: '#include "math.h"': skipped when looking for precompiled header use
    1>          Add directive to 'StdAfx.h' or rebuild precompiled header
    1>f:\c++ projekte\c++ von a bis z\c++ von a bis z\math.cpp(15): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
    1>  Generating Code...
    1>  Compiling...
    1>  C++ von A bis Z.cpp
    1>f:\c++ projekte\c++ von a bis z\c++ von a bis z\c++ von a bis z.cpp(15): error C2065: 'Math' : undeclared identifier
    1>f:\c++ projekte\c++ von a bis z\c++ von a bis z\c++ von a bis z.cpp(15): error C2146: syntax error : missing ';' before identifier 'math'
    1>f:\c++ projekte\c++ von a bis z\c++ von a bis z\c++ von a bis z.cpp(15): error C2275: 'math' : illegal use of this type as an expression
    1>          f:\c++ projekte\c++ von a bis z\c++ von a bis z\math.h(6) : see declaration of 'math'
    1>f:\c++ projekte\c++ von a bis z\c++ von a bis z\c++ von a bis z.cpp(16): warning C4832: token '.' is illegal after UDT 'math'
    1>          f:\c++ projekte\c++ von a bis z\c++ von a bis z\math.h(6) : see declaration of 'math'
    1>f:\c++ projekte\c++ von a bis z\c++ von a bis z\c++ von a bis z.cpp(16): error C2275: 'math' : illegal use of this type as an expression
    1>          f:\c++ projekte\c++ von a bis z\c++ von a bis z\math.h(6) : see declaration of 'math'
    1>f:\c++ projekte\c++ von a bis z\c++ von a bis z\c++ von a bis z.cpp(16): error C2228: left of '.abs' must have class/struct/union
    1>  Generating Code...
    1>
    1>Build FAILED.
    1>
    1>Time Elapsed 00:00:00.79
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    

    Und ja, alle dateien sind im selben ordner bei VS 2010.

    Ich danke euch für jede antwort,
    🙂 😃 🙂 😃 🙂 😃

    Mitfreundlichen Grüßen
    😮 🙄 🙄 😮
    Developer_X



  • Steht doch in der Fehlermeldung (die dir der Compiler ja nicht ohne Grund gibt)

    Did you forget to add '#include "StdAfx.h"' to your source
    

    BTW: Du programmierst C++, da heißt das Ding

    #include <cmath>
    

    und nicht "math.h".



  • Hast du mal gemacht was dein Compiler dir da sagt?
    Fehlermeldungen sind zum Lesen da.

    Did you forget to add '#include "StdAfx.h"' to your source?

    So ein netter Compiler...
    Nachdem du das verbesserst hst, wirst du aber wahrschienlich noch feststellen, dass math.h kein guter Name für deinen Header ist.
    http://cplusplus.com/reference/clibrary/cmath/



  • Ich glaube math.h gibt es schon, du solltest das vielleicht umbenennen.


  • Mod

    l'abra d'or schrieb:

    BTW: Du programmierst C++, da heißt das Ding

    #include <cmath>
    

    und nicht "math.h".

    Nein, tut es nicht. Seine Headerdatei heißt math.h.

    @Threadersteller: Der Name ist etwas ungünstig gewählt, da es auch einen Standardheader namens math.h gibt. Dank des Unterschieds zwischen #include"math.h" und #include<math.h> ist das hier zwar kein Problem, aber wie du siehst stiftet das Verwirrung. Außerdem bekommst du Probleme, falls die math.h mal nicht im aktuellen Verzeichnis liegt.



  • Das Problem ist, wie du dein Projekt erstellt hast. Gewöhn dich daran, immer ein Win32 Konsole zu erstellen, welches Leer und ohne Precompiled Header arbeitet.

    #ifndef _MATH_HPP_
    #define _MATH_HPP_
    
    class Math 
    { 
    public: 
        int abs(int); 
    };
    
    #endif
    
    #include "math.hpp"
    
    int Math::abs(int number) 
    {
    	int zahl = number; 
    
        if(zahl<0) 
            zahl /= -1; 
        else 
            ; 
    
        return zahl;
    }
    
    #include <iostream> 
    #include "math.hpp"
    
    using namespace std;
    
    int main(){
        cout << "Wir werden die minus zahl jetzt positiv machen ! : "; 
        int zahl = 0; 
        cin >> zahl; 
        Math math; 
        zahl = math.abs(zahl); 
        cout << "Zahl wurde geändert, hier: "+zahl; 
    
        cout << "Drücken Sie eine beliebige Taste..." << endl; 
        cin.get();
    
    	return 0;
    }
    

    1>------ Build started: Project: cxx01, Configuration: Debug Win32 ------
    1> math.cpp
    1> main.cpp
    1> Generating Code...
    1> cxx01.vcxproj -> E:\workspace\cxx01\Debug\cxx01.exe
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

    Btw dein Include-Guard ist nicht richtig, falls es einer sein soll.



  • Ausserdem:
    Falsch:

    cout << "Zahl wurde geändert, hier: "+zahl;
    

    Richtig:

    cout << "Zahl wurde geändert, hier: " << zahl << endl;
    


  • Zeus schrieb:

    Ausserdem:
    Falsch:

    cout << "Zahl wurde geändert, hier: "+zahl;
    

    Richtig:

    cout << "Zahl wurde geändert, hier: " << zahl << endl;
    

    och ganz übersehen, danke, ich bin noch an Java gewöhnt.



  • Danke an alle, es funktioniert jetzt.
    Ab sofort, werde ich nicht mehr solche Namen verwenden, danke sehr!

    m.f.G. Developer_X


Anmelden zum Antworten