你所写的程序的同一个类的头文件与实现文件的命名怎么不一样啊?
应该按以下的方式来命名:
Stock.h
Stock.cpp
应该牢记规范书写及命名不易出错。
我对你的程序代码作了一下修正,修正后的代码如下:
/*程序清单1:Stock.h文件*/
// Stock.h: interface for the Stock class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_STOCK_H__607A1CF5_D1F5_4E38_B2D2_7A255D0A48EB__INCLUDED_)
#define AFX_STOCK_H__607A1CF5_D1F5_4E38_B2D2_7A255D0A48EB__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class Stock
{
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot(){total_val=shares * share_val;}
public:
Stock();
Stock(const char * co,int n=0,double pr=0.0);
~Stock();
void buy(int num,double price);
void sell(int num,double price);
void update(double price);
void show();
const Stock &topval(const Stock & s) const ;
};
#endif // !defined(AFX_STOCK_H__607A1CF5_D1F5_4E38_B2D2_7A255D0A48EB__INCLUDED_)
/*程序清单2:Stock.cpp文件*/
// Stock.cpp: implementation of the Stock class.
//
//////////////////////////////////////////////////////////////////////
#include "Stock.h"
#include
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Stock::Stock()
{
std::cout<<"Default constructor called\n";
strcpy(company,"no name");
shares=0;
share_val=0.0;
total_val=0.0;
}
Stock::Stock(const char * co,int n,double pr)
{
std::cout<<"Constructor using"< strncpy(company,co,29); company[29]='\0'; if(n<0) { std::cerr<<"Number of shares can't be negative." < shares=0; } else shares=n; share_val=pr; set_tot(); } Stock::~Stock() { std::cout<<"Bye, "< } void Stock::buy(int num,double price) { if(num<0) { std::cerr<<"Number of shares purchased can't negative." <<"Transaction is aborted.\n"; } else { shares+=num; share_val=price; set_tot(); } } void Stock::sell(int num,double price) { using std::cerr; if(num<0) { cerr<<"Number of shares sold can't be negative." <<"Transaction is aborted.\n"; } else if (num>shares) { cerr<<"You can't sell more than you have!" <<"Transaction is aborted.\n"; } else { shares-=num; share_val=price; set_tot(); } } void Stock::update(double price) { share_val=price; set_tot(); } void Stock::show() { using std::cout; using std::endl; cout<<"Company:"< <<"Shares:"< <<"share Price: $"< <<"Total Worth: $"< } const Stock & Stock::topval(const Stock & s) const { if(s.total_val>total_val) return s; else return *this; } /*程序清单3:UseStock.cpp文件*/ #include using namespace std; #include "Stock.h" const int STKS=4; int main() { /* std::const; //在std中根本就没有const这一成员,这句应该去掉*/ std::ios_base; Stock stock[STKS]= { Stock("NanoSmart",12,20.0), Stock("Boffo Objects",200,2.0), Stock("Monolithic Obeliks",130,3.25), Stock("Fleep Enterprises",60,6.5) }; cout.precision(2); cout.setf(ios_base::fixed,ios_base::floatfield); cout.setf(ios_base::showpoint); cout<<"Stock holdings:\n"; int st; for (st=0;st stock[st].show(); Stock top=stock[0]; for(st=1;st top=top.topval(stock[st]); cout<<"\nMonst valuable holding:\n"; top.show(); return 0; } 修正后的程序可以顺利地编译通过,其运行结果如下所示:
你有没有把他们放到一个工程里面编译?
。。。
程序清单 10.4 stock1.h
#ifndef STOCK1_H_
#define STOCK1_H_
程序清单10.6 usestock1.cpp
#include
#include
Cannot open include file: 'stock2.h': No such file or directory
。。。
#include
无法打开'stock2.h' 这个头文件,你看你有没有把这个头文件包含在这个工程里面
很明显找不到头文件stock2.h,你自己检查下。