Justin S
Justin S

Reputation: 19

No viable overloaded '=' in C++ vector

I am trying to change my bool vector, items[0] to true in game.cpp/.hpp via DeerPark.cpp. However, I cannot figure out why Xcode is throwing this error message. Thank you all for your time and assistance.

This is my error message,

No viable overloaded '='

and it take place in DeerPark.cpp when I do

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

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

Game.cpp

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

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

DeerPark.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);
};

DeerPark.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);
}

Upvotes: 1

Views: 10771

Answers (3)

Kaisheng Liu
Kaisheng Liu

Reputation: 54

in DeerPark::feed, the input argument is a vector<bool>* pointer, and therefore input[1] would be a reference to a vector<bool>, and vector<bool>::operator= doesn't accept a bool value. That's why the compiler complains about "No viable overloaded '='".

The correct way to fix this is to dereference the pointer:

(*input)[1]=true;

Same issue with DeerPark::get (after fixing the typo where Input should be input).

Upvotes: 3

Carl
Carl

Reputation: 2067

It appears you are writing Input[0] with a capital I while the parameter is in fact called input. You are attempting to assign to something that does not exist.

Specifically here:

void DeerPark::get(Character *person, vector<bool>*input)
{
        Input[0] = true;
}

Change that to (*input)[0] = true;

Also, like others are pointing out, since it's passed as a pointer you must dereference the vector before you can subscript it. Also shown in the above snippet. Otherwise you are trying to assign to the pointer. So in short, a typo and an indirection error.

Upvotes: 3

user7860670
user7860670

Reputation: 37579

vector<bool>*input function parameter is a pointer to vector so to access first element you will need to write (*input)[0]. Or (even better) pass by reference:

void DeerPark::feed(Character *person, vector<bool> & input)

Upvotes: 0

Related Questions