Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;-错误-无操作员“;[……”是;匹配这些操作数_C++_C++11 - Fatal编程技术网

C++ C++;-错误-无操作员“;[……”是;匹配这些操作数

C++ C++;-错误-无操作员“;[……”是;匹配这些操作数,c++,c++11,C++,C++11,我正在为一个容器编写代码,该容器存储字符串并按字母顺序排序(我认为这是一个有趣的想法)。我一直在尝试放置一个“[]”操作符并将其分配给私有成员词,这样我就可以访问该成员中的任何数据或字符串。然而,我一直在努力解决这个不断出现的错误,我很难修复它。它说: No operator "[]" matches these operands. Operand types are std::shared_ptr<std::vector<std::string, std::allocator<

我正在为一个容器编写代码,该容器存储字符串并按字母顺序排序(我认为这是一个有趣的想法)。我一直在尝试放置一个“[]”操作符并将其分配给私有成员词,这样我就可以访问该成员中的任何数据或字符串。然而,我一直在努力解决这个不断出现的错误,我很难修复它。它说:

No operator "[]" matches these operands. Operand types are std::shared_ptr<std::vector<std::string, std::allocator<std::string>>>[size_t]
没有与这些操作数匹配的运算符“[]”。操作数类型为std::shared\u ptr[size\u t]
下面是一些关于错误的代码(错误出现在class.cpp中):

class.h

#pragma一次
#包括
#包括
#包括
#包括
类排序
{
公众:
//...
排序(inti):单词(std::make_shared(i)){
std::共享ptr和操作员[](大小st);
//...
私人:
std::共享单词;
std::string alpha=“abcdefghijklmnopqrstuvxyz”;
};
class.cpp

#包括“sort.h”
#包括
#包括
#包括
//...
std::shared\u ptr&sort::operator[](大小)
{
返回单词[st];//在括号中定义了错误
}
//...

另一件需要注意的事情是,如果我用
st
删除括号,错误就消失了(显然不是我想要实现的)。对此代码的任何帮助或修复都将不胜感激。

问题可能在于
单词
std::shared\u ptr
,而不是
std::vector
<代码>std::shared_ptr::operator[]()(这意味着它不会在C++11中编译),即使这样,它也不会做您认为它会做的事情:

返回值

对数组第idx个元素的引用,即get()[idx]

然后,从:

std::shared\u ptr::get

T*get()常量noexcept;(直到C++17)

元素类型*get()常量noexcept;(从C++17开始)

这意味着
get()
返回一个指针。总之,这有效地使您的代码与以下代码相同:

std::vector<int>* ptr = nullptr; // Note that this data is probably  allocated some how...
// Then, later...
ptr[index];
这归结为:你(可能)想要的是:

它应该编译并执行您想要的操作

编辑:我注意到,由于您的函数原型也需要更改,因为
(*words)[st]
不是
std::shared\u ptr
,它只是作为
std::string
。因此,将原型改为:

在cpp中:


您的
words
成员不是数组或容器。它是一个
std::shared_ptr
,在C++17之前没有定义
操作符[]
(即使这样,您的代码仍然会错误地使用它)。这就是为什么
操作符[]
无法编译的原因

您有一个
std::shared_ptr
指向存储在内存1中其他位置的
std::vector
对象。如果您想让
运算符[]
访问
std::vector
中的
std::string
值,您需要首先遵从指针以访问
std::vector
,然后您可以调用其
运算符[]
。您需要将
运算符[]
的返回值固定为单个
std::string
,而不是
std::shared\u ptr

1:你为什么要用指针?为什么不直接在类中将
words
声明为实际的
std::vector
对象<代码>标准::向量词

请尝试以下方法:

h班

#pragma一次
#包括
#包括
#包括
#包括
类排序
{
公众:
//...
标准:字符串和运算符[](大小);
//...
私人:
std::共享单词;
std::string alpha=“abcdefghijklmnopqrstuvxyz”;
};
class.cpp

#include "sort.h"

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

//...

std::string& sort::operator[](size_t st) 
{
    return (*words)[st];
}

//...
#包括“sort.h”
#包括
#包括
#包括
//...
标准::字符串和排序::运算符[](大小)
{
返回(*字)[st];
}
//...

“显然不是我想要实现的”你想要实现什么?这可能是有用的信息,我们可以帮助您。
words
std::shared\u ptr
。为什么您希望一个
操作符[]
也能生成一个
std::shared_ptr
??
操作符[]
更传统的用法是生成容器的元素,例如
std::string::operator[]()
生成(引用)一个
char
。同样,拥有一个
std::shared_ptr
的类成员是毫无意义的——一个简单的
std::vector
的成员是正确的——并且不太容易出错。还有一个名为
sort
的类。。。。哎呀!我认为首先可能是您的编译器,或者构建或“运算符”需要一个值来至少保存,然后尝试其他方法。如果它不起作用,很抱歉祝你好运。吹毛求疵:“弱智”要么是一个过时的医学术语,要么是一个技术化学工程术语。你不是弱智,只是无知。嘿,现在你学会了!另外,如果你想发布你自己的答案,你不需要编辑你的问题来包含答案。叹气+1用于捕捉功能原型也需要更改的信息。我完全错过了。真不敢相信我这么做了。
std::vector<int>* ptr = nullptr; // Note that this data is probably  allocated some how...
// Then, later...
ptr[index];
(*ptr)[index]; // Parenthesis for clarity. I don't think that they are technically necessary here.
 return (*words)[st]; // again, parenthesis for clarity here.
                     // I don't think they are technically necessary here, either.
std::string& operator [](size_t st);
std::string& sort::operator[](size_t st) 
{
   return (*words)[st];
}
#include "sort.h"

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

//...

std::string& sort::operator[](size_t st) 
{
    return (*words)[st];
}

//...