Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (WinAPI, Konsole) » string undeclared, first time use... äinclude <string> drin!

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
25.12.2006, 16:19 Uhr
weed



Also, ich habe hier jetzt ein seltsames Problem...


C++:
#include <windows.h>
#include <stdio.h>
#include <string>

bool file_exists (char* fnam) {
  FILE * f = fopen (fnam, "r");
  if (!f) {
    return false;
  } else {
    fclose(f);
    return true;
  }
}

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int nCommands) {
                        
    // .....  Suchen nach testX.txt-Dateien.
    int i = 1;
    while (true) {
      string ziel = 'test'+i+'.txt';
      if (file_exists(ziel.c_str()) == true) {
            MessageBox (NULL, "Datei existiert!", "Test", MB_ICONINFORMATION);
            break;
      }
      i++;
    }
    return 0;
}


Undzwar compiliert Dev-C++ es bei mir nicht und wirft
folgende Fehlermeldungen:


Zitat:
C:\Dokumente und Einstellungen\X\Desktop\Test\test.cpp In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':
23 C:\Dokumente und Einstellungen\X\Desktop\Test\test.cpp `string' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
23 C:\Dokumente und Einstellungen\X\Desktop\Test\test.cpp expected `;' before "ziel"
23:21 C:\Dokumente und Einstellungen\X\Desktop\Test\test.cpp [Warning] multi-character character constant
23:30 C:\Dokumente und Einstellungen\X\Desktop\Test\test.cpp [Warning] multi-character character constant
24 C:\Dokumente und Einstellungen\X\Desktop\Test\test.cpp `ziel' undeclared (first use this function)
C:\Dokumente und Einstellungen\X\Desktop\Test\Makefile.win [Build Error] [test.o] Error 1


Ich wüsste aber nicht, wo in meinem Code hier ein Semikolon fehlt
und verstehe nicht, wieso string undeclared ist!
#include <string> ist doch drin!
--
lol Signatur lol

Dieser Post wurde am 25.12.2006 um 16:21 Uhr von weed editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
25.12.2006, 16:38 Uhr
xXx
Devil


weil string normal nen bestandteil des std namespaces is t... d.h. muss es std::string heißen ...
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
25.12.2006, 17:05 Uhr
weed



ahja danke!

/* edit */

ARGH!


C++:
#include <windows.h>
#include <stdio.h>
#include <string>
using namespace std;

bool file_exists (char* fnam) {
  FILE * f = fopen (fnam, "r");
  if (!f) {
    return false;
  } else {
    fclose(f);
    return true;
  }
}

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int nCommands) {
                        
    // .....  Suchen nach testX.txt-Dateien.
    int i = 1;
    while (true) {
      string ziel = 'test'+i+'.txt';
      if (file_exists(ziel.c_str()) == true) {
            MessageBox (NULL, "Datei existiert!", "Test", MB_ICONINFORMATION);
            break;
      }
      i++;
    }
    return 0;
}


C:\Dokumente und Einstellungen\X\Desktop\Test\main.cpp In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':
24 C:\Dokumente und Einstellungen\X\Desktop\Test\main.cpp invalid operands of types `const char*' and `const char[5]' to binary `operator+'
25 C:\Dokumente und Einstellungen\X\Desktop\Test\main.cpp invalid conversion from `const char*' to `char*'
25 C:\Dokumente und Einstellungen\X\Desktop\Test\main.cpp initializing argument 1 of `bool file_exists(char*)'
C:\Dokumente und Einstellungen\X\Desktop\Test\Makefile.win [Build Error] [main.o] Error 1
--
lol Signatur lol

Dieser Post wurde am 25.12.2006 um 17:08 Uhr von weed editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
003
25.12.2006, 18:58 Uhr
xXx
Devil


hmm ...

C++:
#include <windows.h>
#include <stdio.h>
#include <sstream>
#include <string>

bool file_exists (LPSTR lpszFileName);

int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)
{
    for (unsigned int nFile = 1; nFile < 6000; ++nFile)
    {
         std::ostringstream sstr;
         sstr << "test" << nFile << ".txt";
         std::string sFileName = sstr.str();
  
         if (file_exists(sFileName.c_str()) == true)
         {
             MessageBox (NULL, TEXT("Die Datei existiert!"), sFileName.c_str(), MB_OK | MB_ICONINFORMATION);
             break;
         }
    }
    return 0;
}

bool file_exists (LPSTR lpszFileName)
{
    FILE* pFile = fopen (lpszFileName, TEXT("r"));
    if (pFile == NULL)
        return false;
    
    fclose(pFile);
    
    return true;
}
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
004
26.12.2006, 00:02 Uhr
weed



@xXx danke:

C:\Dokumente und Einstellungen\X\Desktop\Test\main.cpp In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':
16 C:\Dokumente und Einstellungen\X\Desktop\Test\main.cpp invalid conversion from `const char*' to `CHAR*'
16 C:\Dokumente und Einstellungen\X\Desktop\Test\main.cpp initializing argument 1 of `bool file_exists(CHAR*)'
C:\Dokumente und Einstellungen\X\Desktop\Test\Makefile.win [Build Error] [main.o] Error 1

Sind jetzt ein paar Fehlermeldungen weniger.
Compiliert aber trotzdem nicht...
--
lol Signatur lol

Dieser Post wurde am 26.12.2006 um 00:06 Uhr von weed editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
005
26.12.2006, 00:54 Uhr
xXx
Devil


sry hatte es nicht ausprobiert sondern einfach reingetippt ... der Fehler ist halt das std::string::c_str() nen const char* und keinen char* retuniert ... d.h.

C++:
#include <windows.h>
#include <stdio.h>
#include <sstream>
#include <string>

bool file_exists (LPCSTR lpszFileName);

int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)
{
    for (unsigned int nFile = 1; nFile < 6000; ++nFile)
    {
         std::ostringstream sstr;
         sstr << "test" << nFile << ".txt";
         std::string sFileName = sstr.str();
  
         if (file_exists(sFileName.c_str()) == true)
         {
             MessageBox (NULL, TEXT("Die Datei existiert!"), sFileName.c_str(), MB_OK | MB_ICONINFORMATION);
             break;
         }
    }
    return 0;
}

bool file_exists (LPCSTR lpszFileName)
{
    if (lpszFileName == NULL)
        return false;

    FILE* pFile = fopen (lpszFileName, "r");

    if (pFile == NULL)
        return false;
    
    fclose(pFile);
    
    return true;
}
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
006
28.12.2006, 00:48 Uhr
weed



Ja danke, klappt.
sstream kenn ich nur bloß nicht...
--
lol Signatur lol
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
007
28.12.2006, 09:58 Uhr
BoBtheREapER
kein job für nen BoB


www.cppreference.com/cppsstream/index.html
--
"Zwei Dinge sind unendlich: Das Universum und die menschliche Dummheit. Aber beim Universum bin ich mir nicht ganz sicher." - Albert Einstein
www.blue-xenon.de.vu
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (WinAPI, Konsole) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: