编译类问题
在尝试编译 C++ 类程序时不断出现这些错误。
testStock.cpp:在函数“int main()”中:testStock.cpp:8:错误: 在此范围 testStock.cpp:8 中未声明“Stock”:错误: 预期<代码>;'在“第一个”testStock.cpp:9之前:错误:“第一个”不是 在此范围内声明 testStock.cpp:12: 错误:预期;'前 “第二”testStock.cpp:13:错误:“第二”未在此声明 范围
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;
}
我确信它是显而易见的,但它只是我的第二类程序。
Keep getting these errors while trying to compile an c++ class program.
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
;' before
declared in this scope testStock.cpp:12: error: expected
‘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;
}
Im sure its something obvious but its only my second class program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您已经
从
testStock.cpp
中省略了。It looks like you have omitted
from your
testStock.cpp
.编译器告诉您“'Stock'未在此范围内声明”。因此,您应该问自己“‘Stock’在哪里声明?”,您应该能够回答:“它在
中声明” stock.h
"。和“为什么编译器不知道'Stock'在
stock.h
中声明?”因为你还没有包含它。正如这里已经提到的,#include "stock.h"
是解决方案。希望您能花更多时间阅读编译器错误/警告,并花更多时间尝试理解它们;)
Compiler tells you "‘Stock’ was not declared in this scope ". So you should ask yourself "Where is 'Stock' declared?" and you should be able to answer it: "It's declared in
stock.h
".And "Why compiler doesn't know that 'Stock' is declared in
stock.h
?" Because you haven't included it. So as it was mentioned here already,#include "stock.h"
is the solution.Hope you will spend more time reading compilers errors / warnings and also more time trying to understand them ;)
您只是没有在主文件中包含
"stock.h"
,因此编译器不知道Stock first
的含义。You are just not including
"stock.h"
in your main file, so the compiler doesn't know whatStock first
means.您将能够创建
Stock
对象,因为它对您的TestStock
类可见。and you will be able to create
Stock
object as it will be visible to yourTestStock
class.