【问题标题】:Compiling class issue编译类问题
【发布时间】: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


【解决方案1】:

你好像省略了

#include "stock.h"

来自您的testStock.cpp

【讨论】:

    【解决方案2】:

    编译器告诉你“‘Stock’没有在这个范围内声明”。所以你应该问自己“'Stock'在哪里声明?”你应该能够回答:“它在stock.h中声明".

    “为什么编译器不知道 stock.h 中声明了 'Stock'?” 因为你没有包含它。所以正如这里已经提到的,#include "stock.h" 是解决方案。

    希望您能花更多时间阅读编译器错误/警告,并花更多时间尝试理解它们;)

    【讨论】:

    • testStock.cpp:在函数'int main()'中:testStock.cpp:17:错误:'std::operator中的'operator
    • @Sean :所以它告诉你cout &lt;&lt; first.getName; 是错误的。但是为什么cout &lt;&lt; first.getName(); 是正确的?...缺少括号。宾果游戏!
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:
      #include "stock.h"
      

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

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签