2015-11-30 66 views
-2

我對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

'vector 礦物質(3);''''''''''裏面已經有3個物體了。 – user2357112

+2

請發佈[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve) –

+0

爲什麼你認爲它們存儲在同一個內存位置? – emlai

回答

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

哈哈,這是一個很好的! – ypnos

+1

非常好,非常感謝。這解決了它。我覺得這很簡單,我忽略了。 – ninjarelic

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]}; 

} 
+0

感謝您爲未來的實施提供詳細的指導。相比於我的不討厭的方式,這是非常有意義的。將來會做。 – ninjarelic

相關問題