2012-02-15 29 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; 
} 

我敢肯定它的東西很明顯,但它只是我的第二類節目。

+0

您是否嘗試過調試? – simchona 2012-02-15 19:42:37

+0

@simchona:給出的錯誤是*編譯*錯誤。 – 2012-02-15 19:43:47

+0

是的。------------------ – Sean 2012-02-15 19:44:01

回答

2

編譯器告訴你「'庫存'未在此範圍內聲明」。所以你應該問問自己「宣佈'股票'在哪裏?」,你應該可以回答它:「它在stock.h中聲明」

and 「爲什麼編譯器不知道'Stock'是在stock.h?中聲明的?」因爲你沒有包括它。因此,這裏已經提到過,#include "stock.h"就是解決方案。

希望你會花更多的時間閱讀編譯器錯誤/警告,也有更多的時間試圖瞭解他們;)

+0

testStock.cpp:在函數'int main()'中: testStock.cpp:17:錯誤:在'std :: operator << – Sean 2012-02-15 19:57:17

+0

@Sean'中沒有匹配'operator <<'所以它告訴你'cout << first.getName;'是錯誤的。但爲什麼'cout << first.getName();'是正確的?......缺少括號。答對了! – LihO 2012-02-15 20:00:00

1

您的主文件中只包含"stock.h",因此編譯器不知道Stock first的含義。

3

看起來你已經從你的testStock.cpp省略

#include "stock.h" 

0
#include "stock.h" 

,你將能夠創建Stock對象,因爲它會是你的TestStock類可見。