【问题标题】:error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::stringstream错误 C2678:二进制“>>”:未找到采用“std::stringstream”类型的左侧操作数的运算符
【发布时间】:2013-12-28 04:08:53
【问题描述】:

每当我尝试使用地图编译我的游戏时,我都会收到错误...

"1>Map.cpp(57): 错误 C2678: 二进制 '>>' : 没有找到采用 'std::stringstream' 类型的左侧操作数的运算符(或者没有可接受的转换)"

...但是当我像下面一样初始化可碰撞时它会编译...

bool collidable[100000];

任何人都可以帮助我,如果您发现我的代码有任何其他问题,请您指出,无论如何提前谢谢

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();
}

【问题讨论】:

  • 向量不提供输入支持(尽管它们可以被初始化或assign()ed 为std::istream_iterators)。无论如何,要小心,因为std::vector&lt;bool&gt; 不像其他人。
  • 哦,对不起,我误判了错误。忽略第一句话。我没有意识到您并没有尝试输入整个向量,但第二个仍然适用(并且应该引导您了解它为什么不编译)。
  • 嗯,你能告诉我们错误在哪一行吗?此外,您需要通过删除不相关的代码来简化此演示,同时保留一个完整的程序来演示该问题。
  • while (!f.eof()) 是错误的——有很多不是文件结尾的错误条件。你想要while (f)。 (而且您确实需要在每次使用&gt;&gt; 后检查错误)

标签: c++ boolean stdvector stringstream


【解决方案1】:

问题在于 vector 是特殊的,并且(很可能)不是单个 bool 元素的简单数组。因此,您希望通过使用索引运算符获得的引用不是“对 bool 的引用”,而是不同的类 (see this) 对象。

您可以选择一些替代向量 或使用临时 bool 对象从流中读取,然后将其分配给向量元素。

bool temp_bool;
stream >> temp_bool;
collidable[numberOfTiles] = temp_bool;

【讨论】:

    猜你喜欢
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多