c++ - C++ vector 中没有可行的重载 '='

标签 c++ xcode c++11 vector

我正在尝试通过 DeerPark.cpp 在 game.cpp/.hpp 中将我的 bool vector items[0] 更改为 true。但是,我不明白为什么 Xcode 会抛出此错误消息。感谢大家的时间和帮助。

这是我的错误信息,

No viable overloaded '='

当我这样做时,它发生在 DeerPark.cpp 中

input[1]= true; //and
input[0]= true;

游戏.hpp

#include <vector>
#include <iostream>

class Game
{
    private:
        std::vector<bool> items = std::vector<bool>(3);
    public:
        int intRange(int min, int max, int input);
        void printMenu();
};

游戏.cpp

#include "Game.hpp"
#include <vector>
#include <iostream>

using namespace std;
void Game::printMenu()
{
    items[0] = false;
    items[1] = false;
    items[2] = false;
}

鹿园.hpp

#include <vector>
#include "Game.hpp"
class DeerPark : public Space
{
    protected:
        int feedCounter;
    public:
        DeerPark();

    void feed(Character *person, std::vector<bool>*input);
    void get(Character *person, std::vector<bool>*input);
    void kick(Character *person);
};

鹿园.cpp

#include "DeerPark.hpp"
#include "Space.hpp"
#include <vector>
#include "Game.hpp"

using namespace std;

DeerPark::DeerPark() : Space()
{
    feedCounter = 0;
}
void DeerPark::feed(Character *person, vector<bool>*input)
{

    feedCounter = feedCounter + 1;

    if(feedCounter == 3)
    {
       input[1]= true;
    }
}
void DeerPark::get(Character *person, vector<bool>*input)
{
        Input[0] = true;
}
void DeerPark::kick(Character *person)
{
    person->setStrength(-5);
}

最佳答案

DeerPark::feed , input参数是 vector<bool>*指针,因此 input[1]将是对 vector<bool> 的引用, 和 vector<bool>::operator=不接受 bool值(value)。这就是编译器提示“没有可行的重载 '='”的原因。

解决这个问题的正确方法是取消引用指针:

(*input)[1]=true;

DeerPark::get 相同的问题(在修正 Input 应该是 input 的拼写错误后)。

关于c++ - C++ vector 中没有可行的重载 '=',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49333326/

相关文章:

c++ - 我应该如何打包和解包这个 64 位整数?

ios 在状态 "Preparing for Submission"时将应用程序从一个帐户转移到另一个帐户

xcode - 每个用户在 Xcode 中定义

c++ - 内联函数的局部静态/线程局部变量?

c++ - 如果指针已经被删除,指针容器如何拥有指针的所有权?

c++ - 在mbed微 Controller (c++)上加密AES128-CBC并在nodejs中解密AES128-CBC

c++ - 将 float 四舍五入到一定精度

c++ - 错误消息 : 'value_type' : is not a member of

ios - iPhone 应用程序在 iPhone 上出现像素化

c++ - high_resolution_clock 最高分辨率为1000ns