2013-07-09 67 views
-1

我正在編譯這個頭文件。我不明白爲什麼在虛空中它不會顯示getfunctions的值。從下面的例子中,我正確地做到了這一點。這是給我C++頭文件輸出

// return the player's batting average 
double Baseball::getBatAvg() 
{ 

    if (atbats == 0) 
     // average is 0.0 if player has no at bats 
     return 0.0; 
    else 
     // batting average is the number of hits 
     // divided by the number of at bats 
     return double(hits)/double(atbats); 
} 

// format and output batting statistics 
void Baseball::writeBattingStats() 
{ 
    cout << "Player" << setw(3) << uniformNo 
     << " At bats" << setw(4) << atbats 
     << " Hits" << setw(4) << hits 
     << " Average " << setreal(1,3) << getBatAvg() 
     << endl; 
} 

#ifndef STOCK_MARKET_CLASS 
#define STOCK_MARKET_CLASS 

// system defined preprocessor statement for cin/cout operations 
#include <iostream > 

// programmer defined preprocessor statement for setreal operation 
#include "textlib.h" 

// programmer defined preprocessor statement for String 
#include "tstring.h" 

class StockMarket 
{ 
    private: 
    String symbol;  // identifies the company 
    double startingPrice;  // starting price of the stock 
    double closingPrice;  // closing price of the stock 

    public: 
    // Constructor initializes the attributes that are the symbol, the starting price of the stock, and   
    // the closing price of the stock. 
    StockMarket(String sym, double sPrice, double cPrice); 

    // Takes the closing price of the stock and subtracts the starting price of the stock. Returns the 
    // amount of change in the price of the stock. 
    // You might not have any arguments. 
    double change(double sPrice, double cPrice); 

    // Returns the symbol. 
    // You might not have any arguments. 
    String getSymbol(String sym); 

    // Returns the starting price of the stock. 
    // You might not have any arguments. 
    double getStartingPrice(double sPrice); 

    // Returns the closing price of the stock. 
    // You might not have any arguments. 
    double getClosingPrice(double cPrice); 

    // Outputs the following information that is listed below. 
    // Stock Information for IBM:   <=== where IBM is the symbol 
    // Starting Price  $XXX.XX 
    // Closing Price  $XXX.XX 
    //     ------------- 
    // Difference   $XXX.XX 
    // You might not have any arguments. 
    void writeStockInfo(); 
}; 

//********************************************************************** 
//    StockMarket Class Implementation 
//********************************************************************** 

// Constructor is passed arguments sym, sPrice, and cPrice 
// Implementation of the constructor 
StockMarket::StockMarket(String sym, double sPrice, double cPrice) 
{ 
    symbol = sym; 
    startingPrice = sPrice; 
    closingPrice = cPrice; 
} 

// Function which takes the closing price of the stock and subtracts the starting price of the stock. Returns the 
// amount of change in the price of the stock. 
// Implementation of the function 
// You might not have any arguments. 
double StockMarket::change(double cPrice, double sPrice) 
{ 
    return double (cPrice) - double (sPrice); 
} 

// Function to return the symbol. 
// Implementation of the function 
// You might not have any arguments. 
String StockMarket::getSymbol(String sym) 
{ 
    return sym; 
} 


// Function to return the starting price of the stock. 
// Implementation of the function 
// You might not have any arguments. 
double StockMarket::getStartingPrice(double sPrice) 
{ 
    return sPrice; 
} 


// Function to return the closing price of the stock. 
// Implementation of the function 
// You might not have any arguments. 
double StockMarket::getClosingPrice(double cPrice) 
{ 
    return cPrice; 
} 


// Function that outputs the following information that is listed below. 
// Stock Information for IBM:   <=== where IBM is the symbol 
// Starting Price  $XXX.XX 
// Closing Price  $XXX.XX 
//     ------------- 
// Difference   $XXX.XX 
// Implementation of the function 
// You might not have any arguments. 
void StockMarket::writeStockInfo() 
{ 
    cout << "Stock Information for"<< setw(4) << getSymbol() << ":" << endl; 
} 

#endif           // STOCK_MARKET_CLASS 

例子是我的cpp的代碼

#include "stdafx.h" 
// system defined header file that declares the input/output operations (cin/cout) 
#include <iostream> 
using namespace std; 
// system defined header file that declares parametric manipulators 
#include <iomanip> 
#include "Stock.h" 

int main() 
{ 
    StockMarket IBMStock("IBM", 150.00, 300.00); 

    IBMStock.writeStockInfo(); 


    system("PAUSE"); 
    return 0; 

} 
+2

什麼是「無效」? – 2013-07-09 15:19:45

+7

@ H2CO3非常哲學的問題。 – Salgar

+1

你通常不會編譯頭文件。也許你可以展示你如何運行這段代碼? – juanchopanza

回答

0

的GET功能,如getSymbol,需要String類型的一個參數。

void StockMarket::writeStockInfo() 
{ 
    cout << "Stock Information for"<< setw(4) << getSymbol() << ":" << endl; 
} 

您正在調用getSymbol(),它沒有提供參數。 這就是爲什麼IBMStock.writeStockInfo()不顯示任何結果。

但是,這忽略了另一個問題。在你的get函數實現中,你只是簡單地返回你傳入的參數......這沒有多大意義。你應該做的就是利用'this'指針。

String StockMarket::getSymbol() 
{ 
    return this->symbol; 
} 

我認爲你所追求的是名爲Symbol的成員變量字符串,它屬於StockMarket對象。

另外,C++方面的說明。分離頭文件和實現文件是最好的做法。

編輯: 對於澄清,您可以訪問成員變量沒有this指針以及

String StockMarket::getSymbol() 
{ 
    return symbol; 
} 
+0

變量'symbol'可以不使用'this->'來訪問嗎? –

+0

確實如此。我想我把它提出來說明這個符號屬於一個StockMarket對象。 –