Visualizzazione dei risultati da 1 a 4 su 4

Discussione: [c++]stl map

  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826

    [c++]stl map

    ciao.
    vogli assegnare con l'operatore [] ad un puntatore map <string,string> un valore , il problema è che se faccio :
    codice:
    map<string,string>* mAllTextures
    //e
    mAllTextures['a']= 'test'
    il compilatore mi da quest errore:
    Error 2 error C2107: illegal index, indirection not allowed c:\programmazione\c++\d3dxcreatemeshfvf\mesh\colla dareadertriangles.cpp 552

    ho provato a fare

    codice:
    map<string,string>* mAllTextures
    //e
    *mAllTextures['a']= 'test'
    ma niente da fare.

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Normale. La key deve essere del tipo dichiarato nel template. Nel tuo caso una stringa, non un carattere.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2003
    Messaggi
    4,826
    grazie shodan, ho corretto cosi:

    codice:
    string strFilename = puriImage->getPath();
    string strName = string(dImage->getName());
    			
    mAllTextures[strName] = strFilename;
    ma adesso mi da quest errore:

    Error 2 error C2677: binary '[' : no global operator found which takes type 'std::string' (or there is no acceptable conversion) c:\programmazione\c++\d3dxcreatemeshfvf\mesh\colla dareadertriangles.cpp 554

    eppure sono ambedue stringhe.

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Mi era sfuggito che fosse un puntatore.
    Devi fare così se vuoi usare []

    (*mAllTextures)[strName] = strFilename;

    Io però farei così:
    codice:
    map<string,string>::iterator it = mAllTextures->find(strName);
    if ( it != mAllTextures->end() ) {
       mAllTextures->insert(std::make_pair(strName,strFilename));
    }
    Altrimenti corri il rischio di perdere una "strFilename" in caso utilizzassi due volte la stessa key.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.