Reputation: 1
private:
struct info{
int size = 0;
int key = 0;
int capacity = 1;
std::vector<int*> *value = new std::vector<int*>(capacity);
};
int keyCapacity_;
int size_;
std::vector<info*> *keys_;
within a function...
//some code
keys_ = new std::vector<info*>(keyCapacity_);
//some code
(*keys_)[size_] = new info;
(*keys_)[size_]->(*value)[size] = inputValue; //getting error on this line
(*keys_)[size_]->size += 1;
(*keys_)[size_]->key = key;
I have a pointer to a vector of struct info. Then within info there is a pointer to a vector that will hold values. Very large amounts of data may be input and the program resizes the vectors if needed. The current input value is added to the first open spot on the vector which is size_. On the line I've identified above I am getting an error:
a3.hpp:67:20: error: expected unqualified-id before ‘(’ token
(*keys_)[size_]->(*value)[size] = value;
^
a3.hpp:67:22: error: invalid type argument of unary ‘*’ (have ‘int’)
(*keys_)[size_]->(*value)[size] = value;
How can I access this vector to change the value?
Upvotes: 0
Views: 81
Reputation: 8598
The quick fix for your line is this: (*((*keys_)[size_]->value))[size] = inputValue;
But seriously, don't use C style pointers. Change your code to this instead:
private:
struct info {
int size = 0;
int key = 0;
int capacity = 1;
std::vector<int> value;
}
int keyCapacity_;
int size_;
std::vector<std::unique_ptr<info>> keys_;
And in that function:
//some code
keys_.resize(keyCapacity_);
//some code
keys_[size_] = std::make_unique<info>();
keys_[size_]->value[size] = inputValue;
keys_[size_]->size += 1;
keys_[size_]->key = key;
Since you haven't given a complete example, I can't be entirely sure, but the code still looks wrong to me, because it seems you try to write to a vector that has size 0. From what I can see this should work, but take it was a grain of salt:
//some code
keys_.resize(keyCapacity_);
//some code
keys_[size_] = std::make_unique<info>();
keys_[size_]->value.push_back(inputValue);
keys_[size_]->size += 1;
keys_[size_]->key = key;
Upvotes: 0
Reputation: 1189
(*keys_)[size_]
this one is right
(*keys_)[size_]->(*value)[size]
but this one is not. it will look like you are calling (*value)[size] which doesnt make any sense. its a syntax error.
so you have to call the value first and then dereference it as a whole. like this (*keys_)
int size = yourSize;
(*((*keys_)[size_]->value))
after that you will can now access its index and object inside it.
(*((*keys_)[size_]->value))[size] = &value;
Upvotes: 1
Reputation: 3073
Well, *value
is not in scope in this context. It's in parentheses, so it will be evaluated separately. To get to value, you need:
(*keys_)[size_]->value
And then you want to dereference that:
*((*keys_)[size_]->value)
The extra set of parens probably isn't necessary, but it makes things clear. Then you want to index that:
(*((*keys_)[size_]->value))[size]
And I am assuming that size_
and size
are correct, different indices. Be careful with your naming. That similarity could trip you up very easily.
As a side note, do realize that using new
is almost never what you want to do in modern C++. You probably want to be using smart pointers instead.
Upvotes: 2