C++ 阵列结构
我想做一个小小的自定义向量C++ 阵列结构,c++,pointers,C++,Pointers,我想做一个小小的自定义向量 v-------- should use "const element_foo &" element_foo(int dat, element_foo next){ data.set_attr(dat); ptr_next = &next; } // a2->ptr_next is dangling element_foo a2(15,a1); 元素类,应指向下
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);
元素类,应指向下一个类的地址,但使用此代码
class foo {
private:
int attr;
public:
foo(){attr = 10;}
int get_attr(){return attr;}
void set_attr(int a){attr =a;}
};
class element_foo {
private:
foo data;
element_foo *ptr_next;
public:
element_foo(){ptr_next = NULL;}
element_foo(int dat){data.set_attr(dat); ptr_next = NULL;}
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
foo get_data(){return data;}
element_foo get_next(){return *ptr_next;}
void print_array(){
if (ptr_next == NULL) {
std::cout<< data.get_attr()<<std::endl;
}
else {
std::cout<< data.get_attr()<<std::endl;
this->get_next().print_array();
}
}
};
int main (int argc, char * const argv[]) {
// insert code here...
element_foo a1(10);
element_foo a2(15,a1);
element_foo a3(20,a2);
a3.print_array();
std::cout << "Hello, World!\n";
return 0;
}
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);
class-foo{
私人:
int attr;
公众:
foo(){attr=10;}
int get_attr(){return attr;}
空集_attr(int a){attr=a;}
};
类元素_foo{
私人:
foo数据;
元素_foo*ptr_next;
公众:
元素_foo(){ptr_next=NULL;}
元素_foo(int dat){data.set_attr(dat);ptr_next=NULL;}
元素_foo(int dat,元素_foo next){
数据集属性(dat);
ptr_next=&next;
}
foo get_data(){return data;}
元素_foo get_next(){return*ptr_next;}
无效打印_数组(){
if(ptr_next==NULL){
std::cout此构造函数中存在错误:
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);
您正在获取本地地址。ptr\u next=&next;
函数结束后,地址无效
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);
您需要做的是将next
作为指针传入:
element_foo(int dat, element_foo *next){
data.set_attr(dat);
ptr_next = next;
}
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);
并将主菜单更改为:
element_foo a1(10);
element_foo a2(15,&a1);
element_foo a3(20,&a2);
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);
编辑:
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);
或者,您可以通过引用传递它:
element_foo(int dat, element_foo &next){
data.set_attr(dat);
ptr_next = &next;
}
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);
问题是您正在存储指向临时对象的指针
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);
“next
”只是由于值复制而传入的a1
/a2
的副本。应使用引用复制,以便&next
准确地引用a1
/a2
的地址,而不是它们的副本
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);
执行上述行时,a2->ptr\u next
不指向a1
,
而是一个已经被破坏的临时本地对象。
因此,a2->ptr\u next
是悬空的。以后通过该指针进行的任何访问都有未定义的行为。指针?为什么没有引用?@Tomalak Geret'kal:我回答时没有想到。但是,是的,那也会起作用。我已经放置了一个引用,它工作得很好,感谢大家!语义说明:您正在实现的更像是一个list而不是向量。通常,向量被理解为表示连续存储在内存中的元素的动态集合。当然,您可以让它表示您想要的任何内容,但它可能会混淆其他内容。
v-------- should use "const element_foo &"
element_foo(int dat, element_foo next){
data.set_attr(dat);
ptr_next = &next;
}
// a2->ptr_next is dangling
element_foo a2(15,a1);