我想了解C++中的類並開發了一些我在Python中看到的類。下面是代碼:C++:用戶定義類型的構造函數
#include <iostream>
#include <cmath>
using namespace std;
/*============================================================================*/
/* Define types
/*============================================================================*/
class none_type;
class bool_type;
class int_type;
struct identifier;
/*============================================================================*/
/* Define none type
/*============================================================================*/
class none_type {
public:
none_type() { /* constructor */ };
~none_type() { /* destructor */ };
}; /* none_type */
/*============================================================================*/
/* Define bool type
/*============================================================================*/
class bool_type {
private:
bool base;
public:
bool_type() { base = false; };
~bool_type() { /* destructor */ };
bool_type(bool init) { base = bool(init); };
bool_type(int init) { base = bool(init); };
bool_type(long init) { base = bool(init); };
bool_type(float init) { base = bool(init); };
bool_type(double init) { base = bool(init); };
bool_type(bool_type init) { base = bool(init.base); };
bool_type(int_type init) { base = bool(init.base); };
int get() { cout << base << endl; };
}; /* bool_type */
/*============================================================================*/
/* Define int type
/*============================================================================*/
class int_type {
private:
long base;
public:
int_type() { base = 0; };
~int_type() { /* destructor */ };
int_type(bool init) { base = long(init); };
int_type(int init) { base = long(init); };
int_type(long init) { base = long(init); };
int_type(float init) { base = long(init); };
int_type(double init) { base = long(init); };
int_type(bool_type init) { base = long(init.base); };
int_type(int_type init) { base = long(init.base); };
int get() { cout << base << endl; };
}; /* int_type */
當我嘗試編譯它,g++
告訴我的所有構造函數,其使用自己的類型是無效的。你能解釋一下有什麼不對嗎?我已經定義了類的原型,我還應該做什麼?提前致謝!
你不必把','在函數定義結束。並瞭解構造函數初始化列表... – 2012-10-28 01:03:14
我已經刪除了分號;沒有任何解決辦法。在__必須在C/C++的最後一行中刪除分號時,似乎沒有很多情況。 – ghostmansd
你混淆了「定義」和「聲明」。 –