How find structure index in a structure-TArray? - c++

There is a function on BP that looks for an empty slot in the array and fills it. When trying to rewrite it in c++, it throws the error C2678 "binary"==": an operator accepting a left operand of the type "const FSlotStructure" was not found (or there is no acceptable conversion)"
The variables in the picture correspond to the variables in the code. The Find() function itself does not search for the FSlotStructure variable in the Array Inventory array. What is the mistake?
The function below is custom, because "FIND" and "SetArrayElem" functions from UKismetArrayLibrary and could not be used directly.
void UInventoryComponent::CreateStack(FSlotStructure& ContentToAdd)
{
//TArray<FSlotStructure> Inventory;
int32 Index;
for (auto& SlotStructure : Inventory)
{
int32 NewIndex = Inventory.Find(SlotStructure, Index);
Inventory.Insert(ContentToAdd, NewIndex);
}
}

Everything has been resolved, at least the code compiles without errors. All I had to do was add it to FSlotStructure
operator bool() const
{
return true;
}

Related

How do I return an error when the error type of a function is different?

I am writing a vector class that uses templates so that it can support multiple data types being stored within it. I have a function called get() which returns a specific index within the vector.
MyVector.h
template<class type>
class MyVector {
/* Stuff to make the vector work with features such as pushing back...*/
type get(int index) {
if (index < 0 || index > this->size) {
/* Make the compiler recognize this as a compile error and return
an error message of my choice
*/
return type{};
}
return this->data[index];
}
};
My first question here is how do I make the compiler stop and recognize this as an error and fail to compile. In addition to failing to compile, the compiler should print some error message like Index out of range or something like that. Is this possible within C++?
My second question here is why return type{} actually works (I had to return something, and I just randomly tried return type{}).
In addition, I am trying to use operator overloading so that I can access elements of the vector class with ease. The operator overloading function should return a reference to the element being returned, so that I can change it.
Like vector[index] = value.
The following is my function
type& operator[](int index) {
if (index < 0 || index > this->size) {
/* Make the compiler recognize this as a compile error and return
an error message of my choice
*/
return type{};
}
return this->data[index];
}
I have the same question as above, namely, how do I make the compiler fail to compile and print an error message. Also, what am I supposed to return in this situation? Is there any way I can avoid returning altogether?

Accessing Elements in an Array c++

I am using a templated class and creating an array to store values. I need to overload the [] operator in this array and this is where I am having issues.
template <class TYPE>
class ArrayList : public ListInterface<TYPE>
{
public:
TYPE * aL = new TYPE[25];
}
For my overloaded operater for [] I have the following:
TYPE & operator[] (int num) const //will addd throw std out of range
{
return aL[num];
}
However, when I use this operator later in the program it doesn't return the value at that specific index.
void insertAt(int index, const TYPE & newEntry)
//throw (std::out_of_range)...
{
aL[index] = newEntry;
TYPE a = aL[index]; //use this to see what it is returning
}
I used the variable "a" as a debugging tool to see what is returned for aL[index] and I get the following int held inside of "a" : -858993460. I am sure this must be a simple error, I have searched for a very long time to figure out why this behavior is occuring but have not been able to get to the bottom of it. Thanks for the help!

remove_if: Predicate error when passing a function returning bool

I have this pre-defined function.
void attack(std::vector<GameObject*> objects, unsigned damage) {
for (GameObject* object : objects) {
object->takeDamage(damage);
auto isDead = object->isDead();
objects.erase(std::remove_if(objects.begin(),objects.end(),isDead), objects.end());
}
}
This is my isDead function
bool isDead() const {
if (destructed) {
std::cout << "memory error" << std::endl;
}
return life <= 0;
}
This is the error I keep getting. Have tried a lot of things, but not at all able to figure this one out. Any help appreciated!
error: expression cannot be used as a function
{ return bool(_M_pred(*__it)); }
isDead is a variable in the function. You can't use it as an argument to remove_if.
You can't use a regular member function as argument to std::remove_if either. Use a lambda function instead.
Don't erase objects from a container while you are iterating over it using a range for loop.
Change the argument to attack to be a reference. Otherwise, you will be removing objects from a copy, not the original container.
Here's an updated version of attack:
void attack(std::vector<GameObject*>& objects, unsigned damage)
{
for (GameObject* object : objects)
{
object->takeDamage(damage);
}
objects.erase(std::remove_if(objects.begin(),objects.end(), [](GameObject* object){return object->isDead();}), objects.end());
}
isDead() is a member function of one of your classes, which is exactly why it doesn't work: you did not supply this pointer (object instance) for it to be called on. Oh, and the predicate for remove_if must have exactly one argument of the type objects::value_type.
Do this instead:
objects.erase(std::remove_if(objects.begin(),objects.end(),[](GameObject* object){return object->isDead()), objects.end());

Calling a function on the this keyword

So in my header file I have these two variables declared as private
private:
char* data;
int len;
and give this to access it
int length() const { return len; }
Then in my cpp file I am trying to override the operators in string implementation like this:
bool MyString::operator>(const MyString& string)
{
//Compare the lengths of each string
if((this.length()) > (string.length())){
return 0;
}
//The given string is shorter
return -1;
}
when I compile this I get this error:
mystring.cpp:263:14: error: request for member ‘length’ in ‘this’, which is of non-class type ‘MyString* const’
From what I can tell by trying to call the .length() on the this is trying to access a variable on the this pointer which is causing a problem, like in this question.
That's fine because I can do this instead:
bool MyString::operator>(const MyString& string)
{
//Compare the lengths of each string
if((this->len) > (string.length())){
return 0;
}
//The given string is shorter
return -1;
}
which compiles fine but now I'm wondering how do you call a function on a this pointer. I thought that because it was a pointer I would have to dereference it first so I tried this:
bool MyString::operator>=(const MyString& string)
{
//Compare the lengths of each string
if((*(this).length()) >= (string.length())){
return 0;
}
//The given string is shorter but not equal
return -1;
}
But again I got this error:
mystring.cpp:273:17: error: request for member ‘length’ in ‘this’, which is of non-class type ‘MyString* const’
It seems like this should have worked fine as I would have dereferenced the pointer into the object it pointed to which does indeed have that method but I seem to be missing something. How would I go about calling a function defined in my class on the this pointer? And is there some functional reason why the way I described above does not work?
if((this.length()) > (string.length())){
This should be
if((this->length()) > (string.length())){
as this is a pointer.Basically this is just a pointer referring to the object on which member function is called. So, you have to use -> for all reference to members of that class.
One more advice stop using variable names which are standard keywords. like string in your case. Had you included std namespace you would have got the reason for not doing so.

How to convert array pointer into an array argument from template function?

I guess the title is quite confusing, I'll explain my case with some code.
template<uint16_t Len>
void add(const int8_t (&i_array)[Len])
{
// Do something
}
class Test
{
public:
int8_t* GetName()
{
return name;
}
private:
int8_t name[10] = "myname";
}
int main()
{
Test mytest;
add(mytest.GetName()); // Compilation error
}
This code does not compile. The following error is generated :
"Error#304 : no instance of function template add matches the argument list"
It seems that the compilator is not able to determine that GetName() return an array of size 10. Is that right ?
How could I call "add" with a pointer on an array ?
Thanks,
Nicolas
Test::GetName returns a pointer, not an array. You cannot bind its result to a function that expects an array reference. However, you could change the signature of GetName to make it return the array (by reference, of course):
int8_t (&GetName())[10] { return name; }
Alternatively you could use a cast, but that would defeat the purpose of the type system.