使用返回的向量

时间:2014-12-11 20:22:16

标签: c++ if-statement vector compiler-errors std

我的程序中有一个类用于从文件中读取文本,将其存储为字符串向量,然后将该向量返回到调用它的函数。

然后在该函数中,我试图访问向量副本的元素以比较值。

看起来有点像这样:

filehandler.h

class fileHandler
{
public:
    std::vector<std::string> static ReadFromFile(std::string filename);

private:
    std::vector<std::string> blockOfText;
};

filehandler.cpp

std::vector<std::string> fileHandler::ReadFromFile(std::string filename)
{
//code to read file line by line and put into blockOfText

return blockOfText;
}

assetloader.cpp

void assetLoader::LoadFromExternalFile(std::string filename)
{
//Load the assets into the asset array
unsortedAssetText = ReadFromFile(filename);

//Iterate over the array
lineNumber = 0;
while (lineNumber < unsortedAssetText.size())
{
    if (unsortedAssetText[lineNumber] = "player")
    {

    }
}

然后,当我尝试构建我的程序时,我收到以下错误,我不知道它意味着什么,更别说知道如何解决问题了。有人能帮忙吗?

error: could not convert '(&((assetLoader*)this)->assetLoader::unsortedAssetText.std::vector<_Tp, _Alloc>::operator[]<std::basic_string<char>, std::allocator<std::basic_string<char> > >(((assetLoader*)this)->assetLoader::lineNumber))->std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >(((const char*)"player"))' from 'std::basic_string<char>' to 'bool'|

3 个答案:

答案 0 :(得分:2)

=是赋值运算符,比较运算符是==

if (unsortedAssetText[lineNumber] == "player")
//                                ^^

答案 1 :(得分:0)

变化:

if (unsortedAssetText[lineNumber] = "player")

为:

if (unsortedAssetText[lineNumber] == "player")

答案 2 :(得分:0)

也许您需要更改行

    if (unsortedAssetText[lineNumber] = "player")

    if (unsortedAssetText[lineNumber] == "player")