C++ - Error - No operator "[]" matches these operands

grooog

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]

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:

//...


    sort(int i): words(std::make_shared<std::vector<std::string>>(i)) { } 

    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.

Remy Lebeau

Your words member is not an array or container. It is a std::shared_ptr, which does not have an operator[] defined prior to C++17 (and even then, your code would still be using it wrong). That is why your operator[] fails to compile.

You have a std::shared_ptr pointing to a std::vector<std::string> object stored somewhere else in memory 1. If you want your operator[] to access the std::string values in that std::vector, you need to deference the pointer first in order to access the std::vector, and then you can call its operator[]. You need to fix the return value of your operator[] to be a single std::string, not a std::shared_ptr.

1: why are you using a pointer at all? Why not declare words to be an actual std::vector object directly in your class? std::vector<std::string> words;

Try this instead:

class.h

#pragma once

#include <memory>
#include <vector>
#include <string>
#include <iostream>

class sort
{
public:

    //...

    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::string& sort::operator[](size_t st) 
{
    return (*words)[st];
}

//...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

error: no operator "<" matches these operands

Error: no operator "==" matches these operands

no operator ""<<"" matches these operands error

Error : no operator " != " matches these operands

no operator "=" matches these operands error

C++ No operator [] matches these operands

No operator "<<" matches these operands - C++

No operator "[ ]" matches these operands C++

No operator ">=" matches these operands error in c++ when using while loop

no operator "<<" matches these operands recursive tower of hanoi error

Error during implementation of operator>> :C++ no operator matches these operands operand types are: std::istream >> const double error

IntelliSense: no operator "<<" matches these operands

no operator "/" matches these operands

"no operator >> matches these operands"

Error C++: no operator matches these operands. operand types are: std::ostream << void

No operator "<<" matches these operands error between an object and a string literal

C++ - no operator "<<" matches these operands directory_iterator()

C++) E0349 no operator matches these operands occured

E0349 no operator ">>" matches these operands

No Operator = Matches Operands - DX11

more than one operator "[]" matches these operands

E0349 no operator "<<" matches these operands

C# ERROR Operator * cannot be applied to operands of type 'string' and 'string'

No operator "<" matches these operands operand types are: double < my_class

Accessing an entry in std::map causes 'no operator "[]" matches these operands'

No operator "+" matches these operands, aka cannot add Array<double> to double

No Operator ">>" matches these operands operand types are: std::istream>>int

no operator ">>" matches these operands -- operand types are: std::istream >> const double

no operator ">>" matches these operands operand types are: std::istream >> double*