Reputation: 967
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.
Upvotes: 0
Views: 300
Reputation: 42083
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 ;)
Upvotes: 2
Reputation: 14053
#include "stock.h"
and you will be able to create Stock
object as it will be visible to your TestStock
class.
Upvotes: 0
Reputation: 993095
It looks like you have omitted
#include "stock.h"
from your testStock.cpp
.
Upvotes: 3
Reputation: 92261
You are just not including "stock.h"
in your main file, so the compiler doesn't know what Stock first
means.
Upvotes: 1