60 questions
0
votes
1
answer
143
views
Run-time error using a compound subscripting expression compiled by the MS VS22 C++ compiler
I have some obfuscated code specially written to test the subscript operator.
From my point of view there is used a correct subscripting expression according to the C++ Standard (the so-called sub-...
-1
votes
1
answer
64
views
Compilation issue when conversion operator is added
The following piece of code compiles fine
#include <iostream>
#include <map>
#include <memory>
using namespace std;
class child {
public:
child(std::string name,uint32_t ...
1
vote
2
answers
253
views
C++ change char in string
if i want to change a single char in a string i can do:
#include <iostream>
#include <string>
using namespace std;
int main() {
string mystring = "Hello";
mystring[0] = 'T';
...
0
votes
2
answers
63
views
What does mean ARR in the half place in C?
I have code :get_transaltion(&(arr[1/2]) )
The array is of structures that each contain an array of 3 places, which places does the function accept?
I edited the array in the first place in the ...
0
votes
2
answers
1k
views
warning: top-level comma expression in array subscript changed meaning in C++23 [-Wcomma-subscript]
I have overloaded the 2D subscript operator in one of my classes. And for that I use the -std=c++23 option to compile the program.
Now when calling this operator, GCC complains:
warning: top-level ...
2
votes
1
answer
625
views
How to overload the operator[] with multiple subscripts [closed]
C++23 added support for overloading operator[] with multiple subscripts. It's now available on GCC 12. How should one make use of it?
An example struct:
struct Foo
{
int& operator[]( const std:...
0
votes
1
answer
587
views
operator[] caller's site source location current workaround
sadly, the current source location can't be used directly in the parameter list of operator[], as this operator has to have only one argument. However, is there a workaround so I can get the callers ...
2
votes
1
answer
197
views
I want to implement python list in cpp, but stuck at overloading subscript operator [ ] and comma , [closed]
I want to implement puthon list in cpp, bug do not fin a way to implement pyton's slice operator, e.g.:
list[3:7] // sublist including item 3 included to item 7 excluded
As colon is not an operator ...
-1
votes
1
answer
138
views
C: Why can't I malloc inside the function
I have int *b and a function
void to_Binary(int num, int range, int **bi_res)
{
int k = num, c = 0, r;
*bi_res = (int *) calloc(range,sizeof(int));
while (range >= c) {
r = k%2;
...
2
votes
1
answer
253
views
Overloading subscript operator not working as expected
I have a String struct that I overloaded the subscript operator on. But it doesn't seem to work.
//my_string.h
struct String {
char* Text;
uint64 Length;
char& operator[](int32 index)...
6
votes
2
answers
2k
views
Comma Operator in subscript operator?
I am quite confused with the comma operator. I have never seen such code with such syntax? but I am curious if it useful at any place? why is it deprecated in c++20?
#include <iostream>
int main(...
3
votes
2
answers
1k
views
Indexing operator of an object owned by unique_ptr
I am working on a C++ project. I need to put different classes into std::vector. I found that this indeed is possible by creating classes with a common type and then putting pointers in the vector. In ...
1
vote
3
answers
63
views
Getting pointer issue in C program
As we know that arrays are passed by pointers only in C then swap1(arr[i],arr[j]) below means that two
pointers will be passed to the function swap1() then why swap1(arr[i],arr[j]) is giving me error? ...
0
votes
3
answers
264
views
unable of get output from a 2d vector because of runtime error
i am writing a program that will intake value of a 2 dimensional vector and provide them a output.But,i am getting a runtime error.
#include <bits/stdc++.h>
using namespace std;
int main() {
// ...
2
votes
2
answers
1k
views
why my code is unable to handle large array input(>10000)?
int n;//input size of array
cin >> n;
vector <int> a(n);
vector <int> in;
for (int i = 0; i < n; i++)
cin >> a[i];//input array elements
if (n == 1) {
cout << ...
3
votes
2
answers
437
views
Writing a subscript non-member function
I'm guessing this just isn't legal in C++, but I thought I'd ask, given a struct that I don't own:
struct foo {
int x;
int y;
int z;
};
I want to write a non-member subscript operator for ...
4
votes
1
answer
677
views
How can I overload the subscript operator to return an optional which can be an lvalue?
I'm learning some C++ features by implementing an octree class. I want the subscript operator on this class to return the octant corresponding to an index. How should I define the subscript operator ...
7
votes
1
answer
1k
views
Do pointers support "array style indexing"?
(Self-answered Q&A - this matter keeps popping up)
I assume that the reader is aware of how pointer arithmetic works.
int arr[3] = {1,2,3};
int* ptr = arr;
...
*(ptr + i) = value;
Teachers/C ...
2
votes
4
answers
118
views
Why is the printf statement in the code below printing a value rather than a garbage value?
int main(){
int array[] = [10,20,30,40,50] ;
printf("%d\n",-2[array -2]);
return 0 ;
}
Can anyone explain how -2[array-2] is working and Why are [ ] used here?
This was a question in my ...
1
vote
1
answer
618
views
Overloading subscript operator in an user defined class C++
Consider the following class:
class SocialPrefNode{
public:
// Constructors & Destructor
SocialPrefNode( );
SocialPrefNode( char self, int ind, int link, bool stack, std::vector<...
0
votes
2
answers
379
views
How to subscript a text on a spinner in Android?
I am using a spinner with some text (String) and I would like to write the chemical expression MgCl2 with the number 2 subscripted. How can I do it?
I have seen some posts telling about to use Html....
10
votes
3
answers
22k
views
Do std::strings end in '\0' when initialized with a string literal?
I know string objects aren't null terminated but why should this work?
std::string S("Hey");
for(int i = 0; S[i] != '\0'; ++i)
std::cout << S[i];
So the constructor copies the null ...
0
votes
1
answer
384
views
Good behavior for subscript
I'm creating an extension for String and I'm trying to decide what proper/expected/good behavior would be for a subscript operator. Currently, I have this:
// Will crash on 0 length strings
subscript(...
-4
votes
2
answers
276
views
Overload the subscript operator to call a function based on the type assigned
I have an object that has functions like addString and addInteger. These functions add data to a JSON string. At the end, the JSON string can be obtained and sent out. How can this be made easier by ...
1
vote
2
answers
59
views
what does the subscript operator mean in sort compare function?
struct pair{
int first;
int second;
}
vector<pair> v;
sort(v.begin(), v.end(), [](pair p1, pair p2){return p1.first < p2.first});
what does the [](pair p1, pair p2){return p1.first &...
0
votes
1
answer
69
views
const and overloading operator
I have generic map object.
I want to overload the operator[] so map[key] return the key's value.
I made two versions of the subscript operator.
non-const:
ValueType& operator[](KeyType key){
...
2
votes
2
answers
2k
views
How to implement swift subscript setter for enum associated value
Trying to get the following to work:
enum Foobar {
case values([Int])
case singleThing(Double)
subscript(index:Int) -> Int? {
get {
switch self {
case ...
0
votes
2
answers
2k
views
ostream& does not name a type error. What am I doing wrong here?
I am a learner. I am working on operator overloading. I am trying to write a code for overloading [] and print the elements in the member array. But when I am overloading << to print the member ...
2
votes
4
answers
2k
views
Overloading the class subscript operator to access elements of a member std::vector object
I am parsing a text based file to read variables from it. Existence of variables in the file is important, so I decided to write a template class which will hold both value of the variable (Value) and ...
25
votes
6
answers
11k
views
Swift operator `subscript` []
I am beginner with the Swift having no advance knowledge with operators.
I have the following class
class Container {
var list: [Any] = [];
}
I want to implement the operator subscript [] in ...
7
votes
1
answer
47k
views
Struct type "does not provide a subscript operator"
I am trying to read values from a file into an array of structs. However, I keep getting compiler errors that tell me that my struct, Books, does not provide a subscript operator and I am lost.
The ...
6
votes
3
answers
1k
views
Stack overflow when defining subscript on CKRecord in Swift
This question asks whether one can use subscripting with CKRecord in Swift. While I already knew how to do what the questioner wanted, every permutation of it gives me a stack overflow:
subscript(key:...
5
votes
1
answer
405
views
Why does operator[] only take one argument? [duplicate]
There are plenty of questions related to operator[] only taking one argument, but I can't find one that actually says why.
For example, it seems a very natural extension of the language to have ...
1
vote
1
answer
266
views
Bug in Swift array subscript indexing?
I've isolated some Swift code from my project that can be pasted into a Playground. It produces an error "Could not find an overload for '+' that accepts the supplied arguments" both in normal Xcode ...
3
votes
2
answers
115
views
Is there a way to get (*pointer)[ index ] functionality from something more terse?
I have a class to represent a one dimensional spectrum. The underlying datatype is a simple array of floats. To manipulate elements of the spectrum I overloaded the subscript operators as follows:
...
9
votes
3
answers
821
views
is int[pointer-to-array] in the C++ - standard? [duplicate]
As I have learned, one can write the following code:
char *a = new char[50];
for (int i = 0; i < 50; ++i) {
i[a] = '5';
}
It compiles. It works. It does exactly the same as
char *a = new char[...
3
votes
3
answers
2k
views
Array Subscripting: returning Reference vs proxy class method
While searching for methods for overloading Subscript('[]') operator for template class, I came across two different techniques.
First Technique:
Overloading the operator [] returning pointer to the ...
11
votes
4
answers
2k
views
Overloading subscript operator "["
I am trying to overload the subscript operator ("[") for a custom class I've created. I am trying to figure out how to deal with the following issues.
How can you figure out if the operator is on lhs ...
0
votes
4
answers
220
views
C++ overloaded operator []
I have made a application where you type the amount of books you want to enter and using overloaded operator ([]) but every time I give a pointer to store an array, it gives me an errors such as
2 ...
0
votes
2
answers
446
views
C++ Subscript operator
I have written a code but it doesn't seems to work. Every time I execute the program, I get this error
Run-Time Check Failure #2 - Stack around the variable 'ary' was
corrupted
anyway here is my ...
2
votes
1
answer
817
views
variadic templated overload of operator [] in templated class
I am trying to overload the operator[]. The following code does not compile and I suspect I'm just making a syntax mistake, but I need help understanding what I'm doing wrong and why.
Here is an ...
8
votes
2
answers
3k
views
What type does C++ expect for array subscripts?
In C, array subscription: a[b] is merely the syntactic sugar equivalent of dereferencing after pointer arithmetic: *(a+b) (as explained, say, here).
How is array subscription interpreted in C++, for ...
0
votes
4
answers
920
views
C++ Double subscript overloading: cannot convert from 'type' to 'type &'
I'm trying to make a 2D matrix class which is actually a vector of vectors and both classes are templates. I've overloaded the subscript operator in the vector class. The problem occurs when I try to ...
1
vote
1
answer
128
views
Overloading [] but its result isn't resolving before interacting with other operators (c++)
I've got a data object that I'm trying to get all of the operators working with. It's one chunk of data with variable ptrs into it, and has any number of different types and sizes and whatnot. Types ...
0
votes
1
answer
2k
views
Subscript operator overloading: returning reference problems
I'm overloading the subscript operator for the first time and I'm having troubles in returning a reference value.
I followed the rules of thumb from a post in c++faq tag, but there's something I'm ...
2
votes
1
answer
1k
views
invalid use of non-static data member (array)
I'm very new to C++, so this may be a pretty noobish issue, but I'm having trouble finding a solution. I've searched around for an answer, and the TA seems to have logically equivalent yet ...
0
votes
3
answers
945
views
Design : const and non-const accessors interdependance? [duplicate]
Possible Duplicate:
How do I remove code duplication between similar const and non-const member functions?
In the following example :
template<typename Type, unsigned int Size>
class ...
2
votes
4
answers
3k
views
subscript operators for class with std::map member variable
I am trying to make a class that wraps std::map and does checking to make sure the keys are one the of approved valid strings, and also initializes the map to have default values for all the approved ...
1
vote
1
answer
3k
views
subscripting with 2 arguments in python
Assume I have a class X which has 2 attributes : i and j.
I want to have :
x = X((1,2,3),(2,3,4)) #this would set i to (1,2,3) and j to (2,3,4)
I now want subscripting to work in the following way :
...
0
votes
1
answer
100
views
Avoiding Temporary Variable in Subscript Operator
I have a class v which dynamically allocates an array of doubles. Entries is a pointer to the beginning of this array. Spacing allows me to skip over entries, for example, if I wanted to only consider ...