无法将指针的值更改为等于另一个指针
Can't change the value of a pointer to equal another pointer
我将团队作为团队对象的数组,而matches是团队对象中Match对象的数组。thisMatch
声明为:
Match* thisMatch = new Match();
这就是我要做的:
&(teams[i].matches[foundMatches]) = thisMatch;
// I also tried another method:
(tams[i].matches + foundMatches) = thisMatch;
其中foundMatches
是内部
然而,无论我做什么,我都会不断地得到这个编译器错误:
error: lvalue required as left operand of assignment
&teams[i].matches[foundMatches] = thisMatch;
^
有人知道可能出了什么问题吗?如果需要,我可以提供更多信息,但我认为大部分信息都不相关。
您要做的是将变量分配给常数值。teams[i].matches[foundMatches]
的地址已经预定义(在声明数组时),不能更改。
您要做的是更改teams[i].matches[foundMatches]
的内容(更进一步,我假设这是一个Match
对象,因为您说过matches是Team对象中的Match对象数组)。改变内容可以通过teams[i].matches[foundMatches] = *thisMatch
来完成,即将thisMath
的内容(即*thisMath
)分配给数组条目。
除非您在之前或之后没有使用thisMath
进行任何无法直接对teams[i].matches[foundMatches]
执行的操作,否则您可以使用teams[i].matches[foundMatches] = Match()
并实例化对象,而不是分配内存并将其复制到teams[i].matches[foundMatches]
,从而强制词尾释放。
注意:请确保在Match
类中实现了一个CTOR/CCTOR,因为副本将涉及其中任何一个。
teams[i].matches[foundMatches] = thisMatch;
如果假设matches
包含指向匹配项的指针数组,则应该声明它:
Match **matches;
然后您应该将其分配为:
team[i].matches = new (Match*)[num];
然后以上作业就可以了。
相关文章:
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 修改函数中的指针(将另一个指针作为参数传递)
- 如何将指针从一个void函数传递到另一个C++
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 将指针分配给另一个指针时会发生什么情况?
- 将指向给定子类的指针从一个向量复制到另一个向量
- C++将一个指针分配给另一个指针时执行的类型检查
- 将一个类的方法指针存储到另一个类中
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- 初始化类中的指针数组,并在另一个类中检索它
- 从另一个指针指向函数的指针
- 使用指向成员函数的指针在另一个类中调用类构造函数
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- 无法将指向类的成员函数的函数指针作为参数传递给同一类的另一个成员函数
- 无法"this"指针传递到另一个类并在 CPP 中调用该类的任何方法
- 如果类"A"具有指向另一个类的指针,则"B" A 可以访问 B 类公共方法
- 如何使用指针从另一个类访问属性
- 如何从另一个嵌套类中调用某个封闭类的嵌套类的函数指针成员的值?
- C++ 无效双指针干扰另一个
- 两个抽象类,派生自同一个基类.如何访问从一个抽象类到另一个抽象类的指针