I'm working on code for a container that stores strings and sorts them in alphabetical order (thought that it'd be a fun idea). I've been attempting to put a "[]" operator and assign it to the private member words so I can access any data or strings inside of said member. However, I've been struggling with this continuous error that I'm having trouble fixing. It says:
No operator "[]" matches these operands. Operand types are std::shared_ptr(std::vector(std::string, std::allocator(std::string)))[size_t] (Pretend the parentheses are angular brackets)
Here's some of the code regarding the error (Error is present at class.cpp):
class.h
#pragma once
#include <memory>
#include <vector>
#include <string>
#include <iostream>
class sort
{
public:
//...
std::shared_ptr<std::vector<std::string>> & operator [](size_t st);
//...
private:
std::shared_ptr<std::vector<std::string>> words;
std::string alpha = "abcdefghijklmnopqrstuvwxyz";
};
class.cpp
#include "sort.h"
#include <memory>
#include <vector>
#include <iostream>
//...
std::shared_ptr<std::vector<std::string>> & sort::operator[](size_t st)
{
return words[st]; //Error is defined at the brackets
}
//...
Another thing to note is that if I remove the brackets with st, the error is gone (Obviously not what I'm trying to achieve). Any help or a fix to this code would be greatly appreciated. Thank you.
Aucun commentaire:
Enregistrer un commentaire