- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试重载 [] 运算符,以便当程序员希望检索和设置字符串的特定字符时,该字符串包含在 vector 的一个点中,传递给 [] 的数字将引用来自的第 i 个字符 vector 的开头,到结尾。
例如:这是一个 vector 。从头(0)开始,内容如下:
dog
cat
parrot
其中“dog”是 vector 中的第一个字符串,“parrot”是最后一个。我希望 [] 运算符按如下方式运行:
searchable_vector vec;
vec.push_back("dog");
vec.push_back("cat");
vec.push_back("parrot");
std::cout << vec[3] << " should say 'c' ";
这是我的 searchable_vector.hpp:
#pragma once
#include <vector>
class searchable_vector : public std::vector<std::string>
{
public:
char& operator [] (int n);
private:
std::vector<std::string> vector;
};
这是 searchable_vector.cpp:
char& searchable_vector::operator[] (int i)
{
// 'for each' string in vector member, count
// the length and add it to a counter until
// the counter is at desired 'length' (given from i)
unsigned int counter = 0;
unsigned int spot_in_string = 0;
std::string holding_string = "";
std::vector<std::string>::iterator it;
it = vector.begin();
while (it != vector.end() && counter != i)
{
if (counter == i) // it is where it needs to be
break;
else
{
spot_in_string = it->length() - 1;
if (counter != 0)
counter += spot_in_string + 1;
else
counter += spot_in_string;
if (counter < i) // look in next string
{
it++;
}
else if (counter > i)
{
while (counter != i) // go 'back' one at a time
{
counter--;
spot_in_string--;
}
break; //should break out of master while loop
}
}
}
holding_string = *it;
return holding_string[spot_in_string];
}
我不会太担心这段代码内部的逻辑,但我认为我在重载方面做得不对,因为我在测试中遇到了 Linker 2019 错误(使用 Visual Studio)单元测试文件:
TEST_CASE("Test searchable_vector class [] operator", "[searchable_vector]")
{
searchable_vector vec;
vec.push_back("dog");
vec.push_back("cat");
vec.push_back("parrot");
char result = vec[3];
REQUIRE(result == 'c');
}
错误如下:
LNK2019 unresolved external symbol "public: char & __cdecl
searchable_vector::operator[](int)" (??Asearchable_vector@@QEAAAEADH@Z)
referenced in function "void __cdecl ____C_A_T_C_H____T_E_S_T____2(void)" (?
____C_A_T_C_H____T_E_S_T____2@@YAXXZ) unit_tests
接着是我的 VM_Module.obj 所在的目录
我认为这只是我重载的一些基本问题,而不是逻辑本身。如果那被打破,它应该仍然链接并运行,只是表现不佳或崩溃。
我们将不胜感激任何建设性的反馈。
此外,我想重载它的原因是,我将要编写的代码期望 searchable_vector 对象的行为如前所述,返回 vector 中的第 i 个字符,如果成功实现,应该是干净和容易的来写。谢谢。
最佳答案
链接的问题是因为我没有将标题和源代码添加到我的 CMakelists.txt 中,我想 CMake 无法“了解”。更改后,我重新运行 cmake,一切正常。
至于从 vector 继承的危险,是的,当我发现我的私有(private) vector 中没有任何内容时,我就知道了。这是因为当我调用
vec.push_back("somestring");
在我的测试中,push_back()(继承自 vector)将值存储到 INHERITED“集合”(如果您愿意),而不是我的私有(private)集合。所以我只是删除了我的私有(private) vector ,因为它是不必要的,并且稍微改变了我的 cpp,所以它被设置为
it = this->begin();
而不是
it = vector.begin();
所以它现在可以工作了:)
关于c++ - operator [] 在 vector 类上重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49480335/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minim
家庭作业 必须同时重载 operator>(istream &in, Complex &value); public: // constructor Compl
这个问题在这里已经有了答案: What are the basic rules and idioms for operator overloading? (8 个答案) 关闭 5 年前。 有什么区别
这是文档中一个不清楚的示例,使用此运算符:http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#at 最佳答案 请注
我不明白这种行为: > sort([1,2,3,4]) ~~ sort([1,2,3,4]) False 你能给我解释一下吗? 为什么这两个列表(显然是相等的)根据 Perl 6 不相等。 更新 有趣
我正在尝试将 Ø 设为逻辑否定运算符。 ¬ True; multi sub prefix: ($n) { return not $n; } 当我运行上面的程序时,它返回以下错误: $
class Port { private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bo
早上好。我有一些问题。我有这些字段: name: "Mike", city: "NY", address: "something", pets: ["dog", "cat"] 我创建了索引 db.pe
我有以下代码使用 Javascript Webcrypto-API 解密 AES 加密数据,但它会导致“OperationError”并显示消息“操作因操作特定原因而失败”: function loa
我制作了一个自定义 Airflow 操作符,这个操作符接受一个输入,这个操作符的输出在 XCOM 上。 我想要实现的是使用一些定义的输入调用运算符,将输出解析为可在分支运算符内部调用的 Python,
int a = 1; a += ++a; cout << a << endl; // 4 int a = 1; a += a++; cout << a << endl; // 3 为什么这两个例子有不
我有这个队列的实现: #include using namespace std; template struct elem_q { T inf; elem_q* link; }; template
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我对 C++ 中的运算符重载有疑问。我有代表复数的结构,我正在重载运算符,所以我可以用复数进行计算。Visual Studio 2012 给我这个错误:1 IntelliSense:没有运算符“!=”
我正在尝试创建一个 BoolArray 类,它表示一个 bool 值数组,而不是为每个 bool 变量保存 1 个 bool 值。它使用 1 个字符变量来表示使用位的 8 个 bool 值,并节省内存
我类有 2 个运算符有点问题。 我的类(class)宣布: template class MyMap{ keyType keys[MAX]; valueType values[MAX
我有类的迭代器模板和用于 for 语句的类。 template class Itr2 { public: Itr2() { } ~Itr2() { } typedef t
Section 7.2 Enumeration declarations 没有说明任何关于 operator!=() 和作用域的 operator==()枚举。但是下面的代码可以编译。 #includ
我需要将一个 int 序列化到本地文件并将其读入内存。这是代码 #include "stdafx.h" #include #include using namespace std; int _tm
operator += 这样定义对吗?! void operator +=(const BigNumber& other) { *this=(*this) + other; } 在这样的类中:
我是一名优秀的程序员,十分优秀!