我對C++編程非常陌生,並且遇到了有關對象位置的問題。爲什麼我的對象存儲在同一個位置?
我的代碼如下:
#include <iostream>
#include <ostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
class MineralRocks
{
public:
int AtomicNumber;
string Symbol;
string NameOfMetal;
double MeltingPoint;
double BoilingPoint;
double Density;
double AtomicRadius;
string DiscoveryName;
double SpeedOfSound;
string ProducerCountry;
double ProducerCountryPercent;
//default constructor
MineralRocks() {};
//initialized consntructor
MineralRocks(int a_num, string sym, string n_metal, double melt, double boil, double dense, double a_radius,
string discoName, double SoS, string p_country, double p_CPercent)
{
a_num = AtomicNumber;
sym = Symbol;
n_metal = NameOfMetal;
melt = MeltingPoint;
boil = BoilingPoint;
dense = Density;
a_radius = AtomicRadius;
discoName = DiscoveryName;
SoS = SpeedOfSound;
p_country = ProducerCountry;
p_CPercent = ProducerCountryPercent;
};
//member function melting point conversion
double mConversion() {
return MeltingPoint - 217.15;
}
//member function boiling point conversion
double bConversion() {
return BoilingPoint - 217.15;
}
//member function display results
void display() {
cout << "**********Results**********" << endl;
cout << "Atmoic Number: " << AtomicNumber << endl;
cout << "Atomic Symbol: " << Symbol << endl;
cout << "Metal Name: " << NameOfMetal << endl;
cout << "Melting Point(K): " << MeltingPoint << endl;
cout << "Melting Point(C): " << mConversion() << endl;
cout << "Boiling Point(K): " << BoilingPoint << endl;
cout << "Boiling Point(C): " << bConversion() << endl;
cout << "Density: " << Density << endl;
cout << "Atmoic Radius: " << AtomicRadius << endl;
cout << "Discovorer: " << DiscoveryName << endl;
cout << "Speed of Sound: " << SpeedOfSound << endl;
cout << "Production Country: " << ProducerCountry << endl;
cout << "Production %: " << ProducerCountryPercent << endl;
}
};
int main()
{
//vector (container) of class to store created objects
vector<MineralRocks> minerals(3);
MineralRocks Gold(79, "Au", "Gold", 1337.33, 3243, 19.30, 144, "Jeande Marignac", 2030, "China", 15.734);
minerals.push_back(Gold);
MineralRocks Lithium(3, "Li", "Lithium", 453.65, 1603, 0.534, 152, "Johan August Arfwedson", 6000, "Australia", 12);
minerals.push_back(Lithium);
MineralRocks Uranium(92, "U", "Uranium", 1405.3, 4404, 19.1, 156, "Martin Heinrich Klaproth", 3155, "Canada", 33.3);
minerals.push_back(Uranium);
//for loop, display object data with system pause
for (int x = 0; x < 3; x++) {
minerals[x].display();
system("PAUSE");
cout << "********End Results********" << endl;
};
cout << ">>>>> Total Metal Density Combined: " << minerals[0].Density + minerals[1].Density + minerals[2].Density << endl;
};
真正的問題涉及到我的輸出,主要內容如下:
Atomic Number: -842150451
Atomic Symbol:
Metal Name:
Melting Point(K): -6.27744e+66
Melting Point(C): -6.27744e+66
Boiling Point(K): -6.27744e+66
Boiling Point(C): -6.27744e+66
Density: -6.27744e+66
Atomic Radius: -6.27744e+66
Discoverer:
Speed of Sound: -6.27744e+66
Production Country:
Production %: -6.27744e+66
Press any key to continue...
Atomic Number: -842150451
Atomic Symbol:
Metal Name:
Melting Point(K): -6.27744e+66
Melting Point(C): -6.27744e+66
Boiling Point(K): -6.27744e+66
Boiling Point(C): -6.27744e+66
Density: -6.27744e+66
Atomic Radius: -6.27744e+66
Discoverer:
Speed of Sound: -6.27744e+66
Production Country:
Production %: -6.27744e+66
Press any key to continue...
Atomic Number: -842150451
Atomic Symbol:
Metal Name:
Melting Point(K): -6.27744e+66
Melting Point(C): -6.27744e+66
Boiling Point(K): -6.27744e+66
Boiling Point(C): -6.27744e+66
Density: -6.27744e+66
Atomic Radius: -6.27744e+66
Discoverer:
Speed of Sound: -6.27744e+66
Production Country:
Production %: -6.27744e+66
Press any key to continue...
我試圖找出爲什麼我的對象似乎都存儲在相同的位置。另外,字符串根本不顯示。我意識到這可能很簡單,我只是俯視,但是它是什麼?
'vector礦物質(3);''''''''''裏面已經有3個物體了。 –
user2357112
請發佈[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve) –
爲什麼你認爲它們存儲在同一個內存位置? – emlai