I created a class with a private vector that uses std::string as its data type.
#pragma once
#include <string>
#include <vector>
#include <iostream>
class Pokemon {
public:
//Constructor - leaving it here for reference
Pokemon(std::string name, int LVL, int HP, int ATK, int DEF, int SPATK, int SPDEF, int SPD,
std::vector<std::string>moves, std::vector<int>PP);
//Member Functions
std::vector<std::string> getMoves();
private:
std::vector<std::string>moves;
};
In order to retrieve information from this vector, I created a public class function titled getMoves(), which is supposed to return all of the information from that vector. Here's the function definition that I wrote in the .cpp file.
std::vector<std::string> Pokemon::getMoves() {
return moves;
}
After attempting to print the vector which has these moves with std::cout and receiving a "no match for operator" error, I realized that I had to overload the << operator.
I have several questions on how to go about overloading the << operator so my vector will print.
- I'm uncertain where to declare the overload operator.Do I declare it before my class? Inside my class as a public friend function? In a different header file? Inside my main function?
- What type am I supposed to be overloading to print? I believe it'd be the same type as my class because the getMoves() function belongs to the Pokemon class but I'm unsure if it's that or
std::vector<std::string>
- How do I utilize this overloaded operator within my main function? Just like a normal std::cout?
I'd appreciate any help with these questions, thank you!