如何通过不同的指针使用类的对象访问结构?(链表)(C++)
How do I access a structure using an object of a class through a different pointer?(LINKED LISTS)(C++)
当我试图使用指向同一内存块的指针调用struct的成员时,我的程序崩溃。请原谅我最近才开始学习c++的糟糕代码(大约两个月了(。
#include "iostream"
using namespace std;
struct node
{
int data;
node *next;
};
class trial
{
node *hello;
public:
trial()
{
hello=new node;
hello->data=0;
hello->next=NULL;
}
friend void access(trial);
void get();
};
void access(trial t1)
{
node *temp;
temp=t1.hello;
//My program stops working after I write the following line of code:
cout<<temp->data;
}
int main()
{
trial t1;
access(t1);
}
在access()
中,通过值传入t1
参数,这意味着编译器将对输入对象进行复制。但是trial
类不支持正确的复制语义(请参阅3/5/0规则(,因此t1
参数没有正确初始化。
您应该通过引用传递t1
参数,以避免复制:
void access(trial &t1)
最好通过const
参考,因为access()
不修改t1
:
void access(const trial &t1)
相关文章:
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 访问存储在向量C++中的结构的多态成员
- 访问类lintalizer列表中的结构元素
- 仅使用结构名称访问结构成员
- 如何从其他结构访问受保护的结构变量
- 如何从类中定义的结构访问私有成员?c++
- 枚举范围无法通过 C++ 中的类中的结构访问
- 通过更高级别的结构访问子变量
- 从匿名结构访问枚举条目
- 如何定义别名以通过结构访问枚举类成员
- C 用于向量中的结构访问索引或使用参考值
- 奇怪的结构访问错误
- 将数组作为结构访问与未定义的行为
- 使用c++以及x86和x64体系结构访问处理器中断
- 使用 ostream 从另一个类的结构访问数据
- 从结构访问数组会使程序崩溃
- 联合中的C++结构访问联合成员
- SWIG如何使用Lua表从Lua中的c结构访问字节数组
- c++结构访问冲突