myGully.com

myGully.com (https://mygully.com/index.php)
-   Programmierung (https://mygully.com/forumdisplay.php?f=67)
-   -   [C/C++] String Array als Parameter in Funktion :/ (Neues Problem) (https://mygully.com/showthread.php?t=2163963)

ck2k 16.11.10 14:35

[C/C++] String Array als Parameter in Funktion :/ (Neues Problem)
 
Das Neue Problem findet ihr hier im Edit:
[Link nur für registrierte und freigeschaltete Mitglieder sichtbar. Jetzt registrieren...]

Ich hab grad mit C bzw. C++ angefangen und bastel grad an einem Programm dass jede Stunde den Wallpaper ändert.
Ich habe die Pfade zu dem Bild der jeweiligen Stunde in einem String array gespeichert.
und möchte dann mit hilfe des arrays, der akutellen Stunde, und der ChangeBackground funktion jetzt den Hintergrund ändern, doch er meckert:

Zitat:

55 C:\Programme\Dev-Cpp\tut\Unbenannt4.cpp:45 cannot convert 'std::string' to 'const char*' for argument '1' to 'void ChangeBackground(const char*)'
Vllt. kann mir ja jemand helfen.. google spuckt zwar viel aus, aber nicht das mir konkret hilft.. hab schon versch. dinge ausprobiert.. :(
Code:

//hier sind noch überall die <> rum, aber iwie zeigt der Wörter die in <>
// geschrieben wurden in der CODE funktion nicht an .. :o
#include iostream
#include time.h
#include windows.h
#include string

using namespace std;

void ChangeBackground( const char * const Background )
{
..
}

int main (int argc, char *argv[])
{
int iStunde;
string aFiles[24];
..
aFiles[14] = "C:\\WINDOWS\\Feder.bmp";
aFiles[15] = "C:\\WINDOWS\\Kaffeetasse.bmp";
..

.. // iStunde entähält die akutelle Stunde, hab den code jetzt mal rausgelassen, da dieser Code Funktioniert

ChangeBackground(aFiles[iStunde]);
}


CrazyXBMax 16.11.10 18:46

void ChangeBackground( const char * const Background )
bedeutet das die Funktion ChangeBackground einen const char Zeiger erwartet. Du übergibts im aber ein string. Das ist doch logisch das das nicht geht oder? Du kannst aber folgendes benutzen:
ChangeBackground(reinterpret_cast<const char *>(aFiles[iStunde]));

CrazyXBMax 16.11.10 18:47

sry mein Internet laggt

germgerm 16.11.10 20:49

Die Klasse string sollte die Methode c_str() beinhalten:

Code:

ChangeBackground(aFiles[iStunde].c_str());

ck2k 16.11.10 21:08

habs jetzt Spontan so gelöst:

Eine Funktion zum umwandeln:
Code:

char* string_convert ( string str ) {
    char * cstr, *p;
    cstr = new char [str.size()+1];
    strcpy (cstr, str.c_str());
    return cstr;
}

und aufrufen:
Code:

char* cBild;
cBild = string_convert(aFiles[iStunde]);

ChangeBackground(cBild);

Tortzdem Danke, werde das s***** mit hilfe eurer hinweise dann verbessern :)

[EDIT] Problem2: Das Programm muss jetzt theoretisch in einer endlosschleife wiederholt werden, da aber nur jede Stunde eine Veränderung auftritt, ist das abrufen der Uhrzeit, schneller als der Sekundentakt, unsinn und arbeitspeicher fressend.
Hat jemand einen Tipp, wie ich mein Programm ohne viel arbeitspeicher fressen zu lassen still und leise "im hintergrund" laufen lassen kann? (Im Moment ist es eine Konsolen anwendung)

Nava001 18.11.10 20:16

Du musst die Uhrzeit überwachen und zwar in einer Endlosschleife.
Aber überprüfe die Uhrzeit nur alle z.B. 30 Sekunden.

Code:

The unix functions sleep and usleep are not available in their simple form. The windows function Sleep() may be used (note the capital S) but it introduces complexities. It requires two heavyweight include files that define a lot of global names, and the chances for a name clash are high. Unfortunately I do not know of any other way of making a pause in processing without using a tight loop that keeps the CPU fully occupied.


  void Sleep(int ms)
      include: <windows.h>
      include: <winbase.h>
      The parameter is the number of milliseconds to sleep, and is actually
      declared as a DWORD, but that is functionally equivalent to int.
      This function actually causes the calling Thread to sleep for the
      specififed time, not the whole process. The sleep time is fairly accurate, and
      is not affected by differences in CPU speed.
        The following is the example that was provided for _getch() and _kbhit(), but
      in a more friednly and efficient form:

          while (1)
          { if (_kbhit())
            { int x=_getch();
              if (x>='a' && x<='z') x-=32;
              else if (x>='A' && x<='Z') x+=32;
              printf("[%c]", x); }
            else
            { Sleep(1000);
              printf("."); } }



Alle Zeitangaben in WEZ +1. Es ist jetzt 12:05 Uhr.

Powered by vBulletin® (Deutsch)
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.