我在教自己编写C++类,但似乎无法使编译通过。如果你能帮助我找出不仅如何,但为什么,这将不胜感激。先谢谢你!这里是我的三个文件:将C++类拆分为文件现在不会编译
make_pmt.C
#include <iostream>
#include "pmt.h"
using namespace std;
int main() {
CPMT *pmt = new CPMT;
pmt->SetVoltage(900);
pmt->SetGain(2e6);
double voltage = pmt->GetVoltage();
double gain= pmt->GetGain();
cout << "The voltage is " << voltage
<< " and the gain is " << gain << "." <<endl;
return 0;
}
pmt.C
#include "pmt.h"
using namespace std;
class CPMT {
double gain, voltage;
public:
double GetGain() {return gain;}
double GetVoltage() {return voltage;}
void SetGain(double g) {gain=g;}
void SetVoltage(double v) {voltage=v;}
};
pmt.h
#ifndef PMT_H
#define PMT_H 1
using namespace std;
class CPMT {
double gain, voltage;
public:
double GetGain();
double GetVoltage();
void SetGain(double g);
void SetVoltage(double v);
};
#endif
而对于重扰,我得到一个链接错误(右):
Undefined symbols:
"CPMT::GetVoltage()", referenced from:
_main in ccoYuMbH.o
"CPMT::GetGain()", referenced from:
_main in ccoYuMbH.o
"CPMT::SetVoltage(double)", referenced from:
_main in ccoYuMbH.o
"CPMT::SetGain(double)", referenced from:
_main in ccoYuMbH.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
在标题中放置'using'指令通常是一个坏主意,因为该指令被应用于包含标题的任何文件。在你的情况下,任何包含pmt.h的文件(甚至是间接的)都会将所有名称空间std拉入全局名称空间。 – 2010-04-27 06:30:37
我同意gareth。看到这里:http://stackoverflow.com/questions/2712076/为什么它是一个坏主意,'使用名称空间标准;(或任何其他,对于这个问题,根本。 – sbi 2010-04-27 06:38:25