2012-02-15 11 views
0

C++クラスのプログラムをコンパイルしようとしても、これらのエラーが発生し続ける。クラスのコンパイルの問題

testStock.cpp: In function ‘int main()’: testStock.cpp:8: error: ‘Stock’ was not declared in this scope testStock.cpp:8: error: expected ;' before ‘first’ testStock.cpp:9: error: ‘first’ was not declared in this scope testStock.cpp:12: error: expected ;' before ‘second’ testStock.cpp:13: error: ‘second’ was not declared in this scope

stock.h

#ifndef STOCK_H 
#define STOCK_H 
using namespace std; 

class Stock 
{ 
private: 
    string symbol; 
    string name; 
    double previousClosingPrice; 
    double currentPrice; 
public: 
    Stock(string symbol, string name); 
    string getSymbol() const; 
    string getName() const; 
    double getPreviousClosingPrice() const; 
    double getCurrentPrice() const; 
    double changePercent(); 
    void setPreviousClosingPrice(double); 
    void setCurrentPrice(double); 
}; 

#endif 

stock.cpp

#include <string> 
#include "stock.h" 

Stock::Stock(string symbol, string name) 
{ 
    this->symbol = symbol; 
    this->name = name; 
} 

string Stock::getSymbol() const 
{ 
    return symbol; 
} 

string Stock::getName() const 
{ 
    return name; 
} 

void Stock::setPreviousClosingPrice(double closing) 
{ 
    previousClosingPrice = closing; 
} 

void Stock::setCurrentPrice(double current) 
{ 
    currentPrice = current; 
} 

double Stock::getPreviousClosingPrice() const 
{ 
    return previousClosingPrice; 
} 

double Stock::getCurrentPrice() const 
{ 
    return currentPrice; 
} 

double Stock::changePercent() 
{ 
    return ((currentPrice - previousClosingPrice)/previousClosingPrice) * 100; 
} 

testStock.cpp

#include <string> 
#include <iostream> 
#include "string.h" 
using namespace std; 

int main() 
{ 
    Stock first("aapl", "apple"); 
    cout << "The stock symbol is " << first.getSymbol() << " and the name is " << first.getName() << endl; 
    first.setPreviousClosingPrice(130.0); 
    first.setCurrentPrice(145.0); 
    Stock second("msft", "microsoft"); 
    second.setPreviousClosingPrice(30.0); 
    second.setCurrentPrice(33.0); 
    first.changPercent(); 
    second.changePercent(); 
    cout << "The change in percent for " << first.getName << " is " << first.changePercent() << endl; 
    cout << "The change in percent for " << second.getName << " " << second.getSymbol() << " is " << second.changePercent() << endl; 

    return 0; 
} 

明らかにその何かが、その唯一の私の2番目のクラスのプログラムを確認してくださいイム。

+0

は、デバッグを試してみました);

を使用すると、コンパイラエラー/警告を読んで多くの時間を過ごすことになりますし、また、より多くの時間がそれらを理解しようと願っていますか? – simchona

+0

@imchona:与えられたエラーは* compile * errorsです。 –

+0

はい.---------------- – Sean

答えて

2

「在庫がこの範囲で宣言されていません」。だからあなたは自分自身に尋ねるべきです"在庫はどこにありますか?"と答えてください。stock.hで宣言しました」です。

および「なぜ株価がstock.hに宣言されているのかコンパイラがわからないのはなぜですか?」あなたはそれを含んでいないので。既にここで言及したように、#include "stock.h"が解決策です。

+0

testStock.cppは: testStock.cpp:17:ERROR: 'int型のmain()' 関数では用がない試合 '演算子<<' のstd ::演算子<< – Sean

+0

@Sean」内:だから、 'coutのことを示しています<< first.getName; 'が間違っています。しかし、なぜ 'cout << first.getName();'が正しいのですか?...カッコがありません。ビンゴ! – LihO

1

メインファイルには"stock.h"が含まれていないだけなので、コンパイラはStock firstの意味を知りません。

3

あなたtestStock.cppから

#include "stock.h" 

を省略しているように見えます。

0
#include "stock.h" 

、あなたはそれがあなたのTestStockクラスに表示されますようStockオブジェクトを作成することができます。