habe ein problem mit std::string



  • benutze Linux 2.6.32... g++ 4.4.5
    habe folgendes Pogramm geschrieben mit dem Hintergrund künftig die Menüs als String array zu übergeben, damit ich die Ausgabe der Menüs nicht jeweils extra tippen muss :).

    die Meldungen beim Comp. bzw. linken:

    g++ -o vergleich probieren.cc
    /tmp/ccZzgYKN.o: In function main': probieren.cc:(.text+0xeb): undefined reference tohauptmenue_select(int, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
    collect2: ld returned 1 exit status
    kjs@kjs-laptop:~/Dokumente/programmieren/programme_ks/artikelverwaltung$ gcc -c probieren.cc
    kjs@kjs-laptop:~/Dokumente/programmieren/programme_ks/artikelverwaltung$

    => Compilierung fehlerfrei, aber beim linken ist wohl was falsch, aber ich finde nicht was, also hier der source-Code:

    [code]
    // Program to organize a list of articel, which get buyed,
    // get worked with and sold as product or component of another product

    #ifndef _ARTICEL_H_
    #define _ARTICEL_H_

    #include <cstdlib>
    #include <cstring>
    #include <unistd.h>
    #include <iostream>

    int hauptmenue_select(const int MIN, const int MAX, const std::string menue);
    std::string menueitems[] = {
    " 0 = B E E N D E N\n" ,
    " 1. Neuen Artikel eingeben" ,
    " 2. Artikel verändern" ,
    " 3. Artikelliste ansehen, drucken" ,
    " 4. Verkaufsliste anlegen" ,
    " 5. Preise neu Kalkulieren, alte Liste sichern!" ,
    " 6. Kalkulationsfaktoren eingeben" ,
    " 7. Gemeinkostenzuschläge berechnen" ,
    "Bitte eine Aktion auswählen: ",
    "END"
    };

    int
    main()
    {
    const int SELECTION_MIN = 0; // Minium number to choose 0 to end the program
    const int SELECTION_MAX = 7; // Maximum nuber to choose, at the Moment 7
    int groesse = sizeof(menueitems);
    std::cout << std::endl << "größe: " << groesse << std::endl;
    std::string& menuestrings = menueitems[0];

    int selection = 0; // selection initialize with 0, needed in for and switch

    std::cout << menueitems[0] << std::endl;
    std::cout << menueitems[1] << std::endl;
    hauptmenue_select(SELECTION_MIN, SELECTION_MAX, menueitems[sizeof(menueitems)]);

    // Test if the selection is a possible value
    #if 0
    for(selection = hauptmenue_select(SELECTION_MIN, SELECTION_MAX, menueitems); SELECTION_MIN > selection || selection > SELECTION_MAX; )
    {
    if( selection <= SELECTION_MIN || SELECTION_MAX < selection)
    {
    std::cout << "Nur Werte zwischen " << SELECTION_MIN << " und " << SELECTION_MAX << " eingeben!" << std::endl; // Error message if selection is a wrong value
    sleep(5);
    selection = hauptmenue_select(SELECTION_MIN, SELECTION_MAX, menuestrings); // Try again to give an right value
    }
    }
    #endif
    }

    #if 0
    int hauptmenue_select(const int MIN, const int MAX, std::string menue)
    {

    system("clear"); // delete screen in Linux

    int choose = 0;
    int i = 0;

    // print options to choose for the user
    for( i; menue[i].compare("END"); ++i)
    {
    std::cout << "\n" << menue[i] << std::endl;
    }
    std::cin >> choose;

    return choose;
    }
    #endif
    #endif

    Schätze mal da ist irgendwo ein fehler in der Übergabe des Feldes, aber gleichgültig ob ich eine Referenz übergebe, Zeiger auf zeiger ähnlich *argv[] haut's nicht hin.

    Wenn jemand weiß wo mein Denkfehler liegt bitte ich um Meldung.

    Vielen Dank
    KS



  • #if 0 // <--
    int hauptmenue_select(const int MIN, const int MAX, std::string menue)
    {
      // ...
    }
    #endif
    


  • std::string für Zeichenkettenkonstanten ist suboptimal. Für Zeichenkettenkonstanten kannst Du auch einfach char-Arrays nehmen. std::string erfordert eine dynamische Initialisierung, die char-arrays nicht.

    Und wegen

    #if 0
    dieser text wird vom compiler ignoriert
    #endif
    

    fehlt Dir auch die Definition der einen Funktion.



  • [code]
    }

    int hauptmenue_select(const int MIN, const int MAX, std::string menue)
    {

    system("clear"); // delete screen in Linux

    int choose = 0;
    int i = 0;
    std::string name = menue[0];

    // print options to choose for the user
    for( ++i; name.compare("END"); ++i)
    {
    std::cout << "\n" << name << std::endl;
    name = menue[i];
    }
    std::cin >> choose;

    return choose;
    }

    probieren.cc: In function ‘int hauptmenue_select(int, int, std::string)’:
    probieren.cc:62: error: invalid conversion from ‘char’ to ‘const char*’
    probieren.cc:62: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’



  • menue[0] ist ein char, einen Konstruktor string(char) gibt es aber nicht.
    Wie du dann einen string mit einem einzelnen "char" mit "END" vergleichen willst ist mir auch nicht klar.
    Kannst du bitte mal erklären, was das ganze soll?



  • [code]
    // Program to organize a list of articel, which get buyed,
    // get worked with and sold as product or component of another product

    #ifndef _ARTICEL_H_
    #define _ARTICEL_H_

    #include <cstdlib>
    #include <cstring>
    #include <unistd.h>
    #include <iostream>

    int hauptmenue_select(const int MIN, const int MAX, const std::string menue[]);
    std::string menueitems[] = {
    " 0 = B E E N D E N\n" ,
    " 1. Neuen Artikel eingeben" ,
    " 2. Artikel verändern" ,
    " 3. Artikelliste ansehen, drucken" ,
    " 4. Verkaufsliste anlegen" ,
    " 5. Preise neu Kalkulieren, alte Liste sichern!" ,
    " 6. Kalkulationsfaktoren eingeben" ,
    " 7. Gemeinkostenzuschläge berechnen" ,
    "Bitte eine Aktion auswählen: ",
    "END"
    };

    int
    main()
    {
    const int SELECTION_MIN = 0; // Minium number to choose 0 to end the program
    const int SELECTION_MAX = 7; // Maximum nuber to choose, at the Moment 7
    int groesse = sizeof(menueitems);
    std::cout << std::endl << "größe: " << groesse << std::endl;
    const std::string *name = menueitems;

    int selection = 0; // selection initialize with 0, needed in for and switch

    std::cout << *name << std::endl;
    std::cout << *(name + 1) << std::endl;
    hauptmenue_select(SELECTION_MIN, SELECTION_MAX, name);

    #if 0
    // Test if the selection is a possible value

    for(selection = hauptmenue_select(SELECTION_MIN, SELECTION_MAX, menueitems); SELECTION_MIN > selection || selection > SELECTION_MAX; )
    {
    if( selection <= SELECTION_MIN || SELECTION_MAX < selection)
    {
    std::cout << "Nur Werte zwischen " << SELECTION_MIN << " und " << SELECTION_MAX << " eingeben!" << std::endl; // Error message if selection is a wrong value
    sleep(5);
    selection = hauptmenue_select(SELECTION_MIN, SELECTION_MAX, menuestrings); // Try again to give an right value
    }
    }
    #endif
    }

    int hauptmenue_select(const int MIN, const int MAX, const std::string menue[])
    {

    system("clear"); // delete screen in Linux

    int choose = 0;
    int i = 0;
    const std::string *name = menue;

    // print options to choose for the user
    //for( ++i; *name.compare("END"); ++i)
    {
    std::cout << "\n" << *name << std::endl;
    name = name + i;
    }
    // std::cin >> choose;

    return choose;
    }
    #endif

    Der Fehler lag in const std::string, bzw. bei der Implementation std::string ohne const...

    100 mal gelesen und nicht gesehen...
    Trotzdem besten Dank, ohne Eure tips wär ich noch ein jahr auf der Stelle getrampelt 🙂



  • Benutz doch in Zukunft bitte [ cpp ] Tags. Mit der Vorschau kannst du schauen obs geklappt hat.


Log in to reply