[英]How do I make my adress show the values in my array?
提前谢谢你的帮助!!
我创建了一个 class ,我将数组的名称作为参数传递给它。
Element(string, string, short*); //Constructor for my class Element
接下来,我将一个数组传递给我的 class 的实例 H。
short neutrons_H[] = {0, 1, 2};
Element H("Hydrogen", "H", neutrons_H);
现在,如果我在构造函数中请求它们,我可以从数组中访问正确的值。 但是,如果我从同一个 class 请求方法中的值,我只会得到 -13108 作为响应。 更改索引并不能解决问题。 我想从存储在“中子”下的地址中检索正确的值。
为了清楚起见,我在下面添加了所有三个文件。
主程序:
#include <iostream>
#include "Element.h"
using namespace std;
Element PeriodicTable[118];
int FillPeriodicTable() {
short neutrons_H[] = {0, 1, 2};
Element H("Hydrogen", "H", neutrons_H);
PeriodicTable[Element::getElementNumber()-1] = H;
short neutrons_He[] = {2};
Element He("Helium", "He", neutrons_He);
PeriodicTable[Element::getElementNumber()-1] = He;
cout << endl;
return Element::getElementNumber();
}
int main()
{
cout << FillPeriodicTable() << " elements out of the total 118 have been created and added!" << endl;
PeriodicTable[0].showProperties();
}
Header 文件用于 class 元素:
#pragma once
using namespace std;
class Element
{
private:
static int elementNumber;
string name = "-";
string abbreviation = "";
short protons = 0;
short* neutrons = 0;
short electrons = 0;
public:
Element();
Element(string, string, short*);
void showProperties();
static int getElementNumber() { return elementNumber; }
};
class 元素的源代码:
#include <iostream>
#include "Element.h"
using namespace std;
int Element::elementNumber = 0;
Element::Element() {
}
Element::Element(string name, string abbreviation, short* neutron) {
cout << ++elementNumber << " : " << abbreviation << " - " << name << endl;
this->name = name;
this->abbreviation = abbreviation;
this->protons = elementNumber;
this->neutrons = neutron;
this->electrons = elementNumber;
}
void Element::showProperties() {
cout << abbreviation << " - " << name << ":" << endl << endl;
cout << "Protons:" << endl << " " << protons << endl;
cout << endl << "Neutrons:" << endl;
for (int isotope = 0; isotope < 3; isotope++) {
cout << " " << neutrons[isotope] << endl; // Returns only -13108
}
cout << endl << "Electrons:" << endl << " " << electrons << endl;
}
您正在存储一个悬空指针。
不是数组; 一个指针。
当您尝试使用它时,该数组(它是FillPeriodicTable()
中的局部变量)已失效。 走了。 埋葬。 早已化作尘埃。
如果您希望 class 对象包含 arrays,那么让它们执行此操作,最好是通过存储std::array
。
如果你在valgrind下运行,你将拥有:
bruno@bruno-XPS-8300:/tmp$ valgrind ./a.out
==4288== Memcheck, a memory error detector
==4288== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4288== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4288== Command: ./a.out
==4288==
1 : H - Hydrogen
2 : He - Helium
2 elements out of the total 118 have been created and added!
H - Hydrogen:
Protons:
1
Neutrons:
==4288== Invalid read of size 2
==4288== at 0x401640: Element::showProperties() (in /tmp/a.out)
==4288== by 0x40109A: main (in /tmp/a.out)
==4288== Address 0xffefffc60 is on thread 1's stack
==4288== 240 bytes below stack pointer
==4288==
30240
1401
0
Electrons:
1
==4288==
==4288== HEAP SUMMARY:
==4288== in use at exit: 72,704 bytes in 1 blocks
==4288== total heap usage: 2 allocs, 1 frees, 73,728 bytes allocated
==4288==
==4288== LEAK SUMMARY:
==4288== definitely lost: 0 bytes in 0 blocks
==4288== indirectly lost: 0 bytes in 0 blocks
==4288== possibly lost: 0 bytes in 0 blocks
==4288== still reachable: 72,704 bytes in 1 blocks
==4288== suppressed: 0 bytes in 0 blocks
==4288== Rerun with --leak-check=full to see details of leaked memory
==4288==
==4288== For counts of detected and suppressed errors, rerun with: -v
==4288== ERROR SUMMARY: 3 errors from 1 contexts (suppressed: 0 from 0)
出现问题是因为Element保存了指向short的指针,而该指针是FillPeriodicTable中局部变量的地址
要存储您的数组,您可以在堆中分配它或使用std::vector简化所有
所以 Element 的构造函数有签名
Element::Element(string name, string abbreviation, const vector<short> & neutron)
要在showProperties中写入同位素列表,可以将循环替换为:
for (auto isotope : neutrons)
cout << " " << isotope << endl;
请注意,这与任何数量的同位素兼容,氙和铯有 26 种同位素。 您的代码假设该数组具有(至少)3 个元素,但对于只有 1 个的氦来说这是错误的
在 class 元素中,场中子变为
vector<short> neutrons;
当然主要是:
vector<short> neutrons_H = {0, 1, 2};
...
vector<short> neutrons_He = {2};
现在valgrind下的执行是:
bruno@bruno-XPS-8300:/tmp$ valgrind ./a.out
==4406== Memcheck, a memory error detector
==4406== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4406== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4406== Command: ./a.out
==4406==
1 : H - Hydrogen
2 : He - Helium
2 elements out of the total 118 have been created and added!
H - Hydrogen:
Protons:
1
Neutrons:
0
1
2
Electrons:
1
==4406==
==4406== HEAP SUMMARY:
==4406== in use at exit: 72,704 bytes in 1 blocks
==4406== total heap usage: 8 allocs, 7 frees, 73,752 bytes allocated
==4406==
==4406== LEAK SUMMARY:
==4406== definitely lost: 0 bytes in 0 blocks
==4406== indirectly lost: 0 bytes in 0 blocks
==4406== possibly lost: 0 bytes in 0 blocks
==4406== still reachable: 72,704 bytes in 1 blocks
==4406== suppressed: 0 bytes in 0 blocks
==4406== Rerun with --leak-check=full to see details of leaked memory
==4406==
==4406== For counts of detected and suppressed errors, rerun with: -v
==4406== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
bruno@bruno-XPS-8300:/tmp$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.