为什么我的对象存储在同一个位置?

时间:2015-11-30 00:47:12

标签: c++

我是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...

我试图找出为什么我的对象似乎都存储在同一个位置。此外,根本没有显示字符串。我意识到这可能是一件非常简单的事情,我只是在忽视,但它是什么?

2 个答案:

答案 0 :(得分:4)

我立即注意到你的构造函数不正确。您没有使用当前构造函数正确设置对象的成员变量。

请按照下面的说明进行操作。

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)
{
    AtomicNumber = a_num;
    Symbol = sym;
   // fix the rest
}

答案 1 :(得分:1)

您的问题已经回答了。我的回答显示了一些方法可以使用C ++ 11来避免编写一堆样板代码(特别是你的大重复构造函数),从而减少出错的可能性。

这里的“技巧”是将变量分离成没有其他构造函数的类。这样的类称为聚合,它允许您执行一些名为聚合初始化的事情:您不需要编写构造函数,您只需传入值列表然后他们按顺序申请成员。 (这是初始化在C中工作的唯一方式。)

#include <string>
#include <vector>
#include <iostream>
using namespace std;

struct MineralRocksBase
{
    int AtomicNumber;
    string Symbol;
    string NameOfMetal;
    double MeltingPoint;
    double BoilingPoint;
    double Density;
    double AtomicRadius;
    string DiscoveryName;
    double SpeedOfSound;
    string ProducerCountry;
    double ProducerCountryPercent;
};

struct MineralRocks : MineralRocksBase
{ 
// Construction with no arguments: Zero out all the numbers, instead of leaving garbage values
    MineralRocks(): MineralRocksBase{} {}

// All other construction attempts, except copy-construction, are forwarded on to MineralRocksBase
    template<typename... Args>
    MineralRocks(int a1, Args&&... args): MineralRocksBase{a1, args...} {}

// The rest of your functionality.
// Functions which do not modify the object should be marked "const".
    double mConversion() const { return MeltingPoint - 217.5; }
    double bConversion() const { return BoilingPoint - 217.5; }

    void display() const
    {
        cout << NameOfMetal << ", " << mConversion() << ".\n";
    }
};

int main()
{
// Make the vector - braced initialization
    vector<MineralRocks> minerals =
    { { 3, "Li", "Lithium", 453.65, 1603.0, 0.534, 152.0, "Johan August Arfwedson", 6000.0, "Australia", 12.0 }
    , { 92, "U", "Uranium", 1405.3, 4404.0, 19.1, 156.0, "Martin Heinrich Klaproth", 3155.0, "Canada", 33.3 }
    };

// Append to vector
    minerals.emplace_back(79, "Au", "Gold", 1337.33, 3243.0, 19.30, 144.0, "Jeande Marignac", 2030.0, "China", 15.734);

// Sample display
    for (auto&& m : minerals)
        m.display();

// Test copy-construction
    MineralRocks f{minerals[0]};

}