鍍金池/ 問答/C++/ 以下程序什么輸出為空?

以下程序什么輸出為空?

以下是程序源碼:

class base{
    private:
        int x;
    public:
        void setx(int a) {x = a;}
        int getx() {return x;}
};

int main(){
    int * p;
    base a;
    a.setx(55);
    p = new int(a.getx());
    cout << *p;
}
回答
編輯回答
撥弦

你確定你編譯出來了嗎?cout << *p;你都沒加分號。另外#include <iostream>using namepspace std;你加了嗎?還有C++的main一定要return一個整型的,而不是void。

以下代碼我已經(jīng)測試過了,會輸出55。

#include <iostream>

using namespace std;

class base {
    private:
        int x;
    public:
        void setx(int a) {x = a;}
        int getx() {return x;}
};

int main() {
    int * p;
    base a;
    a.setx(55);
    p = new int(a.getx());
    cout << *p;
    return 0;
}
2018年2月23日 04:25