【发布时间】:2012-02-15 19:41:55
【问题描述】:
在尝试编译 c++ 类程序时不断收到这些错误。
testStock.cpp:在函数“int main()”中:testStock.cpp:8:错误: 'Stock' 未在此范围内声明 testStock.cpp:8: 错误: 预期
;' before ‘first’ testStock.cpp:9: error: ‘first’ was not declared in this scope testStock.cpp:12: error: expected;'前 ‘second’ testStock.cpp:13: error: ‘second’ 未在此声明 范围
股票.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
股票.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;
}
我确定这很明显,但它只是我的二等课程。
【问题讨论】:
-
@simchona: 给出的错误是 compile 错误。
-
@GregHewgill 通过调试,我的意思是更笼统的意思是“你是否尝试过根据错误告诉你的内容做任何事情”
-
您没有在 testStock 模块中包含 Stock 类头 (#include "stock.h"),因此编译器对任何名为 Stock 的类一无所知,并吐出该错误。
标签: c++ compilation