structs und Funktionen mit Structs



  • Hallo, ich komme hier einfach nit weiter. Es gibt stets einen Fehler. Syntax sollte soweit stimmen. Ich arbeite eben an Funktionen vor dem eigentlichen Programm. Aber irgendwas kann ned stimmen. Ich würde mich sehr freuen, wenn ihr mir da weiterhelfen könnt.

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    enum farbe {schwarz = 0, braun = 1, rot = 2, orange = 3, gelb = 4, gruen = 5, blau = 6, violett = 7, grau = 8, weiss = 9, gold = 10, silber = 11, keine = 12};
    
    struct widerstand
    {	
    	farbe ring1;
    	farbe ring2;
    	farbe ring3;
    	farbe ring4;
    };
    
    struct nennwert
    {	
    	double Ohm;
    	int prozent;
    };
    nennwert decode_widerstand (widerstand resistence) {
    
        switch(resistence.ring4) { //4.Ring
    		case braun:
    			return 1;
    			break;
    		case rot:
    			return 2;
    			break;
    		case gold:
    			return 2;
    			break;
    		case silber:
    			return 10;
    			break;
    		case keine:
    			return 20;	}}
    


  • Just-Me schrieb:

    Es gibt stets einen Fehler.

    Das ist eine sehr unzureichende Fehlerbeschreibung. Die genaue Fehlermeldung (und die Stelle im Code) mitzuliefern, ist immer hilfreich.

    Aber zu deinem Code: Deine Funktion decode_widerstand soll laut Signatur einen Nennwert zurückliefern. Du lieferst aber überall nur einen int. Der Compiler weiß nicht, wie er aus dem int einen nennwert bauen soll.

    P.S.: Gibt es einen Sinnunterschied zwischen "nit" und "ned"? Warum nicht "nicht"?



  • Just-Me schrieb:

    Es gibt stets einen Fehler.

    Echt? Welcher denn?

    Just-Me schrieb:

    Syntax sollte soweit stimmen.

    Super! Behauptest du das oder dein Compiler?



  • tut mri leid, es gibt mehrere Fehler. Und ich hab das als einen ganzen betrachtet, da der Fehler mehrere Seiten lang war. Nun hab ich hier ins Forum einige Funktionen mal rausgeschnitten, da ich denke dass ich etwas ganz grundlegendes bei meiner Programmierung falsch verstanden habe. Der Fehler lautet folgendermassen:

    coder.cpp: In function `nennwert decode_widerstand(widerstand)':
    coder.cpp:40: error: expected primary-expression before ')' token
    coder.cpp:42: error: conversion from `int' to non-scalar type `nennwert' requested
    coder.cpp:45: error: conversion from `int' to non-scalar type `nennwert' requested
    coder.cpp:48: error: conversion from `int' to non-scalar type `nennwert' requested
    coder.cpp:51: error: conversion from `int' to non-scalar type `nennwert' requested
    coder.cpp:54: error: conversion from `int' to non-scalar type `nennwert' requested
    coder.cpp: At global scope:
    coder.cpp:64: error: expected unqualified-id before "return"
    coder.cpp:64: error: expected `,' or `;' before "return"
    coder.cpp:70: error: expected declaration before '}' token
    

    zum Programm:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    enum farbe {schwarz = 0, braun = 1, rot = 2, orange = 3, gelb = 4, gruen = 5, blau = 6, violett = 7, grau = 8, weiss = 9, gold = 10, silber = 11, keine = 12};
    
    struct widerstand
    {	
    	farbe ring1;
    	farbe ring2;
    	farbe ring3;
    	farbe ring4;
    };
    
    struct nennwert
    {	
    	double Ohm;
    	int prozent;
    };
    nennwert decode_widerstand (widerstand resistence) {
    
        switch(resistence.ring4 == farbe) { //4.Ring
    		case braun:
    			return 1;
    			break;
    		case rot:
    			return 2;
    			break;
    		case gold:
    			return 2;
    			break;
    		case silber:
    			return 10;
    			break;
    		case keine:
    			return 20;	}	}
    

    PS: bin Schweizer, deswegen benutz ich nid, nit, ned usw. 😃

    edit: die Nummer zu bei den Fehlern sind nicht korrekt, da wie gesagt, es rausgeschnitten wurde. Ich hoffe, dies macht euch nicht allzugrosse Umstände, würde es sonst noch ändern, falls das erwünscht ist.

    edit 2: bei der Funktion nennwert habe ich noch die restlichen Switch-Cases, also z.b. ring3 ring 2 und ring 1. Aber bei dieser Programmierung werden die Fehler Seitenlang:

    switch (r.ring1)
    		case schwarz;
    			decode_widerstand.Ohm = 0*10;
    			break;
    		case braun;
    			decode_widerstand.Ohm = 1*10;
    			break;
    		case rot;
    			decode_widerstand.Ohm = 2*10;
    			break;
    		case orange;
    			decode_widerstand.Ohm = 3*10;
    			break;
    		case gelb;
    			decode_widerstand.Ohm = 4*10;
    			break;
    		case grün;
    			decode_widerstand.Ohm = 5*10;
    			break;
    		case blau;
    			decode_widerstand.Ohm = 6*10;
    			break;
    		case violett;
    			decode_widerstand.Ohm = 7*10;
    			break;
    		case grau;
    			decode_widerstand.Ohm = 8*10;
    			break;
    		case weiss;
    			decode_widerstand.Ohm = 9*10;
    			break;
    

    edit 4: natürlich sollte nach jedem case "farbe" ein Doppelpunkt und kein Strichpunkt stehen (trotzdem falsch)



  • So ich hab nun nochmals den ganzen Code jetzt nochmals richtig geschrieben...darunter folgt die Fehlerbezeichnungen, welche die Stelle diesmal richtig markiert.

    Die Zeilen 9 bis 65 des Quellcodes waren vorgegeben.

    /***
     * 
     *
     * Dieses Programm liest vom Benutzer den Nennwert und die Toleranz
     * eines Widerstandes im Farb-Code ein, dekodiert ihn, und gibt das
     * Ergebnis als Ohm-Wert und Toleranz in % aus.
     */
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    enum farbe {schwarz = 0, braun = 1, rot = 2, orange = 3, gelb = 4, gruen = 5, blau = 6, violett = 7, grau = 8, weiss = 9, gold = 10, silber = 11, keine = 12};
    
    string translate_farbe(farbe farbe) {
        switch(farbe) {
            case schwarz: return "schwarz";
            case braun: return "braun";
            case rot: return "rot";
            case orange: return "orange";
            case gelb: return "gelb";
            case gruen: return "gruen";
            case blau: return "blau";
            case violett: return "violett";
            case grau: return "grau";
            case weiss: return "weiss";
            case gold: return "gold";
            case silber: return "silber";
            default: return "keine";
        }
    }
    
    farbe translate_farbe(string farbe) {
        if (farbe == "schwarz") {
            return schwarz;
        } else if (farbe == "braun") {
            return braun;
        } else if (farbe == "rot") {
            return rot;
        } else if (farbe == "orange") {
            return orange;
        } else if (farbe == "gelb") {
            return gelb;
        } else if (farbe == "gruen") {
            return gruen;
        } else if (farbe == "blau") {
            return blau;
        } else if (farbe == "violett") {
            return violett;
        } else if (farbe == "grau") {
            return grau;
        } else if (farbe == "weiss") {
            return weiss;
        } else if (farbe == "gold") {
            return gold;
        } else if (farbe == "silber") {
            return silber;
        } else {
            return keine;
        }
    }
    
    struct widerstand
    {	
    	farbe ring1;
    	farbe ring2;
    	farbe ring3;
    	farbe ring4;
    };
    
    struct nennwert
    {	
    	double Ohm;
    	int prozent;
    };
    
    nennwert decode_widerstand(widerstand r) {
    
        switch (r.ring1){
    		case schwarz:
    			decode_widerstand.Ohm = 0*10.0;
    			break;
    		case braun:
    			decode_widerstand.Ohm = 1*10.0;
    			break;
    		case rot:
    			decode_widerstand.Ohm = 2*10.0;
    			break;
    		case orange:
    			decode_widerstand.Ohm = 3*10.0;
    			break;
    		case gelb:
    			decode_widerstand.Ohm = 4*10.0;
    			break;
    		case grün:
    			decode_widerstand.Ohm = 5*10.0;
    			break;
    		case blau:
    			decode_widerstand.Ohm = 6*10.0;
    			break;
    		case violett:
    			decode_widerstand.Ohm = 7*10.0;
    			break;
    		case grau:
    			decode_widerstand.Ohm = 8*10.0;
    			break;
    		case weiss:
    			decode_widerstand.Ohm = 9*10.0;
    			break;
    		default:
    			cout << "Ungültige Eingabe";
    			return 0; // löschen?
    			}
    
        switch (r.ring2){ //2. Ring
    		case schwarz:
    			decode_widerstand.Ohm = decode_widerstand.Ohm+ 0.0;
    			break;
    		case braun:
    			decode_widerstand.Ohm = decode_widerstand.Ohm +1.0;
    			break;
    		case rot:
    			decode_widerstand.Ohm = decode_widerstand.Ohm+2.0;
    			break;
    		case orange:
    			decode_widerstand.Ohm = decode_widerstand.Ohm+3.0;
    			break;
    		case gelb:
    			decode_widerstand.Ohm = decode_widerstand.Ohm+4.0;
    			break;
    		case grün:
    			decode_widerstand.Ohm = decode_widerstand.Ohm+5.0;
    			break;
    		case blau:
    			decode_widerstand.Ohm = decode_widerstand.Ohm+6.0;
    			break;
    		case violett:
    			decode_widerstand.Ohm = decode_widerstand.Ohm+7.0;
    			break;
    		case grau:
    			decode_widerstand.Ohm = decode_widerstand.Ohm+8.0;
    			break;
    		case weiss:
    			decode_widerstand.Ohm = decode_widerstand.Ohm+9.0;
    			break;
    		default:
    			cout << "Ungültige Eingabe";
    			return 0; // löschen?			
    			}
    
        switch (r.ring3){ //3. Ring
    		case schwarz:
    			decode_widerstand.Ohm = decode_widerstand.Ohm;
    			break;
    		case braun:
    			decode_widerstand.Ohm = decode_widerstand.Ohm*10.0;
    			break;
    		case rot:
    			decode_widerstand.Ohm = decode_widerstand.Ohm*100.0;
    			break;
    		case orange:
    			decode_widerstand.Ohm = decode_widerstand.Ohm*1000.0;
    			break;
    		case gelb:
    			decode_widerstand.Ohm = decode_widerstand.Ohm*10000.0;
    			break;
    		case grün:
    			decode_widerstand.Ohm = decode_widerstand.Ohm*100000.0;
    			break;
    		case blau:
    			decode_widerstand.Ohm = decode_widerstand.Ohm*1000000.0;
    			break;
    		case gold:
    			decode_widerstand.Ohm = decode_widerstand.Ohm*0.1;
    			break;
    		case silber:
    			decode_widerstand.Ohm = decode_widerstand.Ohm+0.01;
    			break;
    		default:
    			cout << "Ungültige Eingabe";
    			return 0; // löschen?		
    
    			}
    
        switch (r.ring4){ //4.Ring
    			break;
    		case braun:
    			decode_widerstand.prozent =1 ;
    			break;
    		case rot:
    			decode_widerstand.prozent = 2;
    			break;
    		case gold:
    			decode_widerstand.prozent = 5;
    			break;
    		case silber:
    			decode_widerstand.prozent = 10;
    			break;
    		default:
    			decode_widerstand.prozent = 20;	
    				}
    
    }
    
    int main ()
    {
    
        //...
    }
    

    Fehler:

    farbdecoder.cpp: In function `nennwert decode_widerstand(widerstand)':
    farbdecoder.cpp:87: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:90: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:93: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:96: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:99: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:101: error: stray '\252' in program
    farbdecoder.cpp:101: error: `gr' undeclared (first use this function)
    farbdecoder.cpp:101: error: (Each undeclared identifier is reported only once for each function it appears in.)
    farbdecoder.cpp:101: error: expected `:' before "n"
    farbdecoder.cpp:102: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:105: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:108: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:111: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:114: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:118: error: conversion from `int' to non-scalar type `nennwert' requested
    farbdecoder.cpp:123: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:123: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:126: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:126: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:129: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:129: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:132: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:132: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:135: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:135: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:137: error: stray '\252' in program
    farbdecoder.cpp:137: error: expected `:' before "n"
    farbdecoder.cpp:137: error: duplicate label `n'
    farbdecoder.cpp:138: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:138: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:141: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:141: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:144: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:144: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:147: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:147: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:150: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:150: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:154: error: conversion from `int' to non-scalar type `nennwert' requested
    farbdecoder.cpp:160: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:160: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:163: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:163: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:166: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:166: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:169: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:169: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:172: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:172: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:174: error: stray '\252' in program
    farbdecoder.cpp:174: error: expected `:' before "n"
    farbdecoder.cpp:174: error: duplicate label `n'
    farbdecoder.cpp:175: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:175: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:178: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:178: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:181: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:181: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:184: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:184: error: request for member `Ohm' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:188: error: conversion from `int' to non-scalar type `nennwert' requested
    farbdecoder.cpp:195: error: request for member `prozent' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:198: error: request for member `prozent' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:201: error: request for member `prozent' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:204: error: request for member `prozent' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:207: error: request for member `prozent' in `decode_widerstand', which is of non-class type `nennwert ()(widerstand)'
    farbdecoder.cpp:101: warning: label `n' defined but not used
    


  • C++ ist nicht VBScript. Du kannst nicht einfach in der Funktion decode_widerstand so tun, als gäbe es eine lokale Variable, die genauso heißt und automatisch zurückgegeben wird.

    Du musst schon ein Nennwert-Objekt anlegen, das in den Zuweisungen benutzen, und es am Ende auch explizit zurückgeben.


Log in to reply