vs2013编译数据隐藏文件时第一次运行没错误,第二次就提示无法解析的外部符号
源程序
#include <iostream>
#include "stock.h"
int main()
{
Stock flu;
flu.acquire("Nanosmart", 20, 12.50);
flu.show();
flu.buy(15, 18.125);
flu.show();
flu.sell(400, 20.00);
flu.show();
flu.buy(300000, 40.125);
flu.show();
flu.sell(300000, 0.125);
flu.show();
return 0;
}
头文件
#ifndef STOCK_H_
#define STOCK_H_
#include <string>
class Stock
{
private:
std::string company;
long shares;
double share_val;
double total_val;
void set_tot(){ total_val = shares*share_val; }
public:
void acquire(const std::string &co, long n, double pr);
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show();
};
#endif
定义头文件
#include <iostream>
#include "stock.h"
void Stock::acquire(const std::string &co, long n, double pr)
{
company = co;
if (n < 0)
{
std::cout << "数值不能为零 "
<< company << "低于零\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
void Stock::buy(long num, double price)
{
if (num < 0)
{
std::cout << "数值不能为零 \n";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(long num, double price)
{
using namespace std;
if (num < 0)
{
cout << "数值不能为零 \n";
}
else if (num>shares)
{
cout << "数值超额\n";
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show()
{
std::cout << "company:" << company
<< "shares:" << shares
<< "share price:$:" << share_val
<< "total worth:$:" << total_val << std::endl;
}