求助:gbean编译多文件时报错

Fade_kang 2009-12-20 11:14:59
源码是c++primier plus上的
#ifndef STOCK1_H_
#define STOCK1_H_

class Stock
{
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot() { total_val = shares * share_val; }
public:
Stock(); // default constructor
Stock(const char * co, int n = 0, double pr = 0.0);
~Stock(); // noisy destructor
void buy(int num, double price);
void sell(int num, double price);
void update(double price);
void show();
};

#endif

// stock1.cpp – Stock class implementation with constructors, destructor added
#include <iostream>
#include "stock1.h"

// constructors (verbose versions)
Stock::Stock() // default constructor
{
std::cout << "Default constructor called\n";
std::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 " << co << " called\n";
std::strncpy(company, co, 29);
company[29] = '\0';

if (n < 0)
{
std::cerr << "Number of shares can't be negative; "
<< company << " shares set to 0.\n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
// class destructor
Stock::~Stock() // verbose class destructor
{
std::cout << "Bye, " << company << "!\n";
}

// other methods
void Stock::buy(int num, double price)
{
if (num < 0)
{
std::cerr << "Number of shares purchased can't be 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: " << company
<< " Shares: " << shares << endl
<< " Share Price: $" << share_val
<< " Total Worth: $" << total_val << endl;
}

// usestok1.cpp -- use the Stock class
#include <iostream>
#include "stock1.h"

int main()
{
using std::cout;
using std::ios_base;
cout.precision(2); // #.## format
cout.setf(ios_base::fixed, ios_base::floatfield);// #.## format
cout.setf(ios_base::showpoint); // #.## format

cout << "Using constructors to create new objects\n";
Stock stock1("NanoSmart", 12, 20.0); // syntax 1
stock1.show();
Stock stock2 = Stock ("Boffo Objects", 2, 2.0); // syntax 2
stock2.show();

cout << "Assigning stock1 to stock2:\n";
stock2 = stock1;
cout << "Listing stock1 and stock2:\n";
stock1.show();
stock2.show();

cout << "Using a constructor to reset an object\n";
stock1 = Stock("Nifty Foods", 10, 50.0); // temp object
cout << "Revised stock1:\n";
stock1.show();
cout << "Done\n";
return 0;
}
编译时候报错:
/tmp/cciR6GJK.o: In function `main':
usestok1.cpp:(.text+0xf6): undefined reference to `Stock::Stock(char const*, int, double)'
usestok1.cpp:(.text+0x101): undefined reference to `Stock::show()'
usestok1.cpp:(.text+0x126): undefined reference to `Stock::Stock(char const*, int, double)'
.......
...全文
36 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
昵称很不好取 2009-12-20
  • 打赏
  • 举报
回复
在万人鄙视的VC6.0下给你试了下,好像没错误,运行结果如下:
Using constructors to create new objects
Constructor using NanoSmart called
Company: NanoSmart Shares: 12
Share Price: $20.00 Total Worth: $240.00
Constructor using Boffo Objects called
Company: Boffo Objects Shares: 2
Share Price: $2.00 Total Worth: $4.00
Assigning stock1 to stock2:
Listing stock1 and stock2:
Company: NanoSmart Shares: 12
Share Price: $20.00 Total Worth: $240.00
Company: NanoSmart Shares: 12
Share Price: $20.00 Total Worth: $240.00
Using a constructor to reset an object
Constructor using Nifty Foods called
Bye, Nifty Foods!
Revised stock1:
Company: Nifty Foods Shares: 10
Share Price: $50.00 Total Worth: $500.00
Done
Bye, NanoSmart!
Bye, Nifty Foods!

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧