在 C++ 中从另一个 class 更改数组值
Change array value from another class in C++
我遇到了分段错误。我是 C++ 的新手,所以不太熟悉指针和其他类似的东西。这似乎是一个基本方面,但我似乎无法弄清楚,我已经花了无数个小时。
我有 5 个文件 element.h、element.cpp、heap.h、heap.cpp、main.cpp
行
出现错误
h.setElements(e,i);
这是Heap.cpp
中的函数
我知道跟数组有关
MAIN.CPP
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "heap.h"
#include "element.h"
using namespace std;
int main(){
Heap h;
h = h.initialize(3);
for(int i=0;i<h.getCapacity(); ++i){
Element e;
e.setKey(i);
h.setElements(e,i); // Error occurs here
}
h.printHeap(h);
return 0;
}
HEAP.H
#ifndef heap_h
#define heap_h
#include "element.h"
#include <iostream>
using namespace std;
class Heap {
public:
Element* getElements();
void setElements(Element e,int index);
Heap initialize(int n);
void printHeap(Heap heap);
private:
int capacity;
int size;
Element* H;
};
#endif
HEAP.CPP
#include "heap.h"
Heap Heap::initialize(int n){
H = new Element[n];
Heap h;
h.capacity = n;
h.size = 0;
return h;
}
void Heap::printHeap(Heap heap){
for(int i=0;i<heap.capacity;++i){
cout << "Element " << i << " = " << H[i].getKey() << endl;
}
}
void Heap::setCapacity(int nCapacity ) {
capacity = nCapacity;
}
int Heap::getCapacity(void) {
return capacity;
}
void Heap::setSize(int nSize ) {
size = nSize;
}
int Heap::getSize(void) {
return size;
}
Element* Heap::getElements(void){
return H;
}
void Heap::setElements(Element e,int index){
H[index] = e;
}
您收到错误消息是因为 H 为空。
错误在Heap::initialize(Element, int) 方法中。
您分配的是调用方法的 Heap 对象的局部变量 H,而不是您正在 returning 的对象。
Heap Heap::initialize(int n){
H = new Element[n]; // You are assigning H for the current heap object
Heap h; // Here you are creating a new Heap object
h.capacity = n;
h.size = 0;
return h; // You haven't assigned h.H
}
为什么要创建一个新的堆对象并 returning 它?
您可以使初始化方法无效,如下所示:
void Heap::initialize(int n) {
H = new Element[n];
capacity = n;
size = 0;
}
或者如果您需要 return 一个新的堆对象,您可以这样做:
Heap Heap::initialize(int n) {
Heap h;
h.H = new Element[n];
h.capacity = n;
h.size = 0;
return h; // You have assigned h.H
}
希望这对您有所帮助。
我遇到了分段错误。我是 C++ 的新手,所以不太熟悉指针和其他类似的东西。这似乎是一个基本方面,但我似乎无法弄清楚,我已经花了无数个小时。
我有 5 个文件 element.h、element.cpp、heap.h、heap.cpp、main.cpp
行
出现错误h.setElements(e,i);
这是Heap.cpp
中的函数我知道跟数组有关
MAIN.CPP
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "heap.h"
#include "element.h"
using namespace std;
int main(){
Heap h;
h = h.initialize(3);
for(int i=0;i<h.getCapacity(); ++i){
Element e;
e.setKey(i);
h.setElements(e,i); // Error occurs here
}
h.printHeap(h);
return 0;
}
HEAP.H
#ifndef heap_h
#define heap_h
#include "element.h"
#include <iostream>
using namespace std;
class Heap {
public:
Element* getElements();
void setElements(Element e,int index);
Heap initialize(int n);
void printHeap(Heap heap);
private:
int capacity;
int size;
Element* H;
};
#endif
HEAP.CPP
#include "heap.h"
Heap Heap::initialize(int n){
H = new Element[n];
Heap h;
h.capacity = n;
h.size = 0;
return h;
}
void Heap::printHeap(Heap heap){
for(int i=0;i<heap.capacity;++i){
cout << "Element " << i << " = " << H[i].getKey() << endl;
}
}
void Heap::setCapacity(int nCapacity ) {
capacity = nCapacity;
}
int Heap::getCapacity(void) {
return capacity;
}
void Heap::setSize(int nSize ) {
size = nSize;
}
int Heap::getSize(void) {
return size;
}
Element* Heap::getElements(void){
return H;
}
void Heap::setElements(Element e,int index){
H[index] = e;
}
您收到错误消息是因为 H 为空。
错误在Heap::initialize(Element, int) 方法中。 您分配的是调用方法的 Heap 对象的局部变量 H,而不是您正在 returning 的对象。
Heap Heap::initialize(int n){
H = new Element[n]; // You are assigning H for the current heap object
Heap h; // Here you are creating a new Heap object
h.capacity = n;
h.size = 0;
return h; // You haven't assigned h.H
}
为什么要创建一个新的堆对象并 returning 它? 您可以使初始化方法无效,如下所示:
void Heap::initialize(int n) {
H = new Element[n];
capacity = n;
size = 0;
}
或者如果您需要 return 一个新的堆对象,您可以这样做:
Heap Heap::initialize(int n) {
Heap h;
h.H = new Element[n];
h.capacity = n;
h.size = 0;
return h; // You have assigned h.H
}
希望这对您有所帮助。