我正在尝试通过 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/