Indexer access returns temporary value. Cannot modify struct member when accessed struct is not classified as a variable
Assistant
The error message "Indexer access returns temporary value. Cannot modify struct member when accessed struct is not classified as a variable" typically occurs in C++ when trying to modify a member of a struct (or class) through an indexer or function call that returns a temporary object rather than a modifiable lvalue. In simpler terms, you're trying to change a part of an object that doesn't stick around long enough to be changed because it's not stored in a variable.
An example can illustrate this problem more clearly:
cpp
#include<iostream>
struct MyStruct {
int value;
};
class Container {
private:
MyStruct arr[10];
public:
MyStruct get(int index) const { // This function returns a copy, not a reference.
return arr[index];
}
MyStruct& getRef(int index) { // This function returns a reference, allowing modifications.
return arr[index];
}
};
int main() {
Container c;
// Attempting to modify a struct member through a method that returns a temporary copy.
// This will fail or not compile in strict languages/settings because get(i) returns a temporary object.
//c.get(0).value = 5; // BAD: attempting to modify a temporary object
// Correct way: Use a method that returns a reference
c.getRef(0).value = 5; // GOOD: modifies the actual object inside the container
return 0;
}
Why does this happen?
When you try to modify a property or call a non-const method on a temporary object (like the one returned by get(int index)), the compiler issues an error because once that line of code executes, the temporary object no longer exists, making the modification pointless and potentially hazardous. To mitigate this, the operation is disallowed.
How to fix?
Return a reference: Ensure that your method or operator that's intended to allow modifications returns a reference (Type&) to the object rather than a copy. This is what getRef(int index) does in the example.
Use a variable: If you necessarily have to work with a method that returns by value (a copy), you can assign its return value to a local variable first, modify the variable, and then use it. Note that this won't modify the original object but the copy.
cpp
auto temp = c.get(0); // Copy
temp.value = 5; // Modify the copy
// Note: the original in `c` is NOT modified.
Caution:
While returning a reference solves this specific problem, it also exposes the internals of your class or struct, potentially leading to unintended modifications from outside code. Use this approach judiciously, ensuring it fits within your design principles and security considerations.
User
Indexer access returns temporary value. Cannot modify struct member when accessed struct is not classified as a variable C#代码报错,请问怎么解决
public struct MyStruct {
public int Value;
}
public class Container {
private MyStruct[] items = new MyStruct[10];
public MyStruct this[int index] {
get { return items[index]; }
set { items[index] = value; }
}
}
// 尝试使用
var container = new Container();
container[0].Value = 123; // 这里会出现问题
了解您所提的情况,基于您使用的DDict<BattleFieldPosition, BattleSlot>和想要修改BattleSlot中的成员,但遇到“Indexer access returns temporary value. Cannot modify struct member when accessed struct is not classified as a variable”这样的错误信息,我们可以展开讨论解决方案。
var tempSlot = m_battleSlots[somePosition];
tempSlot.SomeMember = newValue;
m_battleSlots[somePosition] = tempSlot; 这种还不行,就是需要改成tempSlot.value.SomeMember = newValue; 但是这样又会报错 Property 'Value' access returns temporary value. Cannot modify struct member when accessed struct is not classified as a variable
Assistant
基于您提供的详细信息,看起来您遇到的问题与C#中对值类型(比如结构体)的处理有关。在您的示例中,尝试通过对tempSlot的成员直接赋值来更新DDict集合中的BattleSlot项似乎不起作用。接着,您尝试了tempSlot.Value.SomeMember = newValue;,假设tempSlot是一个可能含有.Value属性的类型(可能是一个Nullable<T>或类似的结构),这也不起作用,并引发了“Property 'Value' access returns temporary value. Cannot modify struct member when accessed struct is not classified as a variable”的错误。
根据您的描述,“Property 'Value' access returns temporary value. Cannot modify struct member when accessed struct is not classified as a variable”的错误提示清楚地指向了尝试修改一个临时值,而这在处理值类型或包含值类型的属性时是常见的问题。希望上述解释和建议能帮助您解决问题。
User
如果把BattleSlot 改成类又会出问题The type 'CGGame.BattleSlot' must be a non-nullable value type in order to use it as parameter 'V'
Assistant
如果您在尝试将 BattleSlot 从结构体 (struct) 改为类 (class) 时遇到了错误信息:“The type 'CGGame.BattleSlot' must be a non-nullable value type in order to use it as parameter 'V'”,这表明在某个地方,BattleSlot 被用作一个泛型参数 V 的地方有对它是值类型的预期。这通常发生在一些特定的泛型类或方法中,这些类或方法专为处理值类型(如结构体)设计。