编译类问题

发布于 2025-01-06 03:19:07 字数 2397 浏览 1 评论 0原文

在尝试编译 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
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;
}

Im sure its something obvious but its only my second class program.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

汐鸠 2025-01-13 03:19:07

看起来您已经

#include "stock.h"

testStock.cpp 中省略了。

It looks like you have omitted

#include "stock.h"

from your testStock.cpp.

烈酒灼喉 2025-01-13 03:19:07

编译器告诉您“'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 ;)

篱下浅笙歌 2025-01-13 03:19:07

您只是没有在主文件中包含 "stock.h" ,因此编译器不知道 Stock first 的含义。

You are just not including "stock.h" in your main file, so the compiler doesn't know what Stock first means.

女皇必胜 2025-01-13 03:19:07
#include "stock.h"

您将能够创建 Stock 对象,因为它对您的 TestStock 类可见。

#include "stock.h"

and you will be able to create Stock object as it will be visible to your TestStock class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文