error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::stringstream

StackOverflow https://stackoverflow.com/questions/20810896

Pregunta

Whenever I try and compile my game with the map I get the error...

"1>Map.cpp(57): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::stringstream' (or there is no acceptable conversion)"

...but it compiles when I initialize collidable like it is below...

bool collidable[100000];

Can anyone please help me and also if you spot anything else that's wrong with my code can you please point it out as well anyway thanks in advance

MAP.h

#pragma once

class Map{
public:
    std::vector<GLfloat> tileX, tileY, boxX, boxY, srcX, srcY;
    std::vector<bool> collidable;
    std::vector<int> collidableObjects;

    GLfloat imageW, imageH, tileW, tileH;
    int numberOfCollidableObjects, numberOfTiles;

    void Load(const char* filename);
    bool Collision(Object obj1);
    void Display();
private:
    GLuint image;
    string filenametemp;
    const char* mapImageFilename;
};

Map.cpp

#include "GEN.h"

void Map::Load(const char* filename){
    stringstream temp;
    string temp1;

    ifstream file;
    file.open(filename);

    getline(file, filenametemp);
    mapImageFilename = filenametemp.c_str();

    getline(file, temp1);
    temp.str(temp1);
    temp >> imageW;
    temp.clear();

    getline(file, temp1);
    temp.str(temp1);
    temp >> imageH;
    temp.clear();

    getline(file, temp1);
    temp.str(temp1);
    temp >> tileW;
    temp.clear();

    getline(file, temp1);
    temp.str(temp1);
    temp >> tileH;
    temp.clear();

    numberOfTiles = 0;
    while(!file.eof()){
        getline(file, temp1);
        temp.str(temp1);
        temp >> tileX[numberOfTiles];
        temp.clear();

        getline(file, temp1);
        temp.str(temp1);
        temp >> tileY[numberOfTiles];
        temp.clear();

        getline(file, temp1);
        temp.str(temp1);
        temp >> srcX[numberOfTiles];
        temp.clear();

        getline(file, temp1);
        temp.str(temp1);
        temp >> srcY[numberOfTiles];
        temp.clear();

        getline(file, temp1);
        temp.str(temp1);
        temp >> collidable[numberOfTiles];
        temp.clear();

        if(collidable[numberOfTiles] == 1){
            getline(file, temp1);
            temp.str(temp1);
            temp >> boxX[numberOfTiles];
            temp.clear();

            getline(file, temp1);
            temp.str(temp1);
            temp >> boxY[numberOfTiles];
            temp.clear();

            collidableObjects[numberOfCollidableObjects] = numberOfTiles;
            numberOfCollidableObjects ++;
        }

        numberOfTiles ++;
    }

    file.close();

    image = SOIL_load_OGL_texture(mapImageFilename, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT);
}

void Map::Display(){
    glBindTexture(GL_TEXTURE_2D, image);
    glBegin(GL_QUADS);
    for(int n = 0;n < numberOfTiles; n++){
        glTexCoord2f((srcX[n]/imageW), (srcY[n]/imageH));glVertex2f(tileX[n]-tileW/2, tileY[n]+tileH/2);
        glTexCoord2f((tileW/imageW)+(srcX[n]/imageW), (srcY[n]/imageH));glVertex2f(tileX[n]+tileW/2, tileY[n]+tileH/2);
        glTexCoord2f((tileW/imageW)+(srcX[n]/imageW), (srcY[n]/imageH)+(tileH/imageH));glVertex2f(tileX[n]+tileW/2, tileY[n]-tileH/2);
        glTexCoord2f((srcX[n]/imageW), (srcY[n]/imageH)+(tileH/imageH));glVertex2f(tileX[n]-tileW/2, tileY[n]-tileH/2);
    }
    glEnd();
}
¿Fue útil?

Solución

The problem is that vector<bool> is special and is (most probably) not a simple array of individual bool elements. So the reference that you expect to get by using the index operator is not a 'reference to bool' but a different class (see this) object.

You can choose some alternative to vector<bool> or use a temporary bool object to read from the stream and then assign it to the vector element.

bool temp_bool;
stream >> temp_bool;
collidable[numberOfTiles] = temp_bool;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top