簡體   English   中英

如何根據布爾型成員函數的返回值調用結構成員函數?

[英]How to invoke struct member functions based on return value of a bool type member function?

我想在結構的布爾成員函數中進行一些條件檢查。 我的結構對象struct1如何知道bool成員函數已返回true,以便可以在calc成員函數中使用整數a和b?

int main() {
    vector<Point> pt;
    pt.push_back(Point{ 1.5, 4.2 });
    pt.push_back(Point{ 2.4, 3.1 });

    doSth struct1;
    bool tempbool = struct1.memfuncbool(pt); //error starts here!
    if (tempbool) {int answer = struct1.calc(1);} //??
    std::cout << answer;

    return 0;
}

struct Point {
   double _x;
   double _y;
};

struct doSth {

    int a, b; //data members

    int calc(const int k) {
        return (a + b)*k;
    }

    bool memfuncbool(const vector<Point> &pts) {        

        //does stuff...

        a = var1;  //var1 = 1
        b = var2;  //var2 = 2

        return true;
    }
}

有兩種方法:調用方安全性封裝和純代碼規范。 稍后,您自己確保所編寫的代碼始終了解memfuncbool的最新結果以及設置ab時間和位置。

在第一種方法中,您可以在調用memfuncbool時在您設置的結構中添加一個標志,並檢入calc(並適當地處理它。)在這種情況下,您還應確保在初始化結構時清除該標志-通過構造函數或再次執行代碼規范(例如始終將結構歸零)。

第一種意義上的信息隱藏方法(C ++)如下所示:

class DoSth {
    int a, b;
    bool valid;

public:
    DoSth() : valid(false) { }

    bool isValid() const { return valid; }

    /// returns calc if valid, otherwise 0
    int calc(int k) const {
        return isValid() ? (a + b) * k : 0;
    }

    void setSth(...) {
        a = ...
        b = ...
        valid = true;
        // instead of returning here the caller can check isValid() anytime
    }
};

您的代碼中有許多問題,如果您不嘗試做超出預期的事情,就可以輕松解決。

1您要在主要功能之后定義Point和dosth結構。 因此,主要功能無法知道您的結構做什么。 通常,您將使用頭文件包含聲明,並使用cpp文件包含實現,但是由於您正在執行的是小程序,因此只需將結構的定義移至main函數上方即可。 另外,您可以在main上面聲明您的結構,並在下面實現它們,如下所示。

// Definition of struct Point
struct Point {
   double _x;
   double _y;
};

// Definition of struct doSth
struct doSth {
    int a, b; //data members

    // **Declaration** of doSth methods
    int calc(const int k);
    bool memfuncbool(const std::vector<Point> &pts);
};


int main() {
    ...
}

// Definition of calc method
int doSth::calc(const int k) {
    ...
}

// Definition of memfuncbool method
bool doSth::memfuncbool(const std::vector<Point> &pts) {        
    ...
}

2在主函數中,您正在一個不知道該變量的范圍內使用一個名為answer的變量。

if (tempbool) {
    int answer = struct1.calc(1);
}
std::cout << answer; // ERROR: answer is not a known variable

看,您在if條件中聲明了一個變量,但在外部使用了它。 如果要使用它,還必須在外部聲明該變量。

int answer = 0;
if ( tempbool ) {
    answer = struct1.calc(1);
}
std::cout << answer << std::endl; // OK

要么

if ( tempbool ) {
    int answer = strut1.calc(1);
    std::cout << answer << std::endl;
}
else {
    std::cout << "Invalid result!" << std::endl;
}

這是一個編譯您到目前為止所做的工作的修復程序。 但這不是您的代碼設計問題的解決方案。

3 代碼設計盡管我建議對代碼進行快速修復,但實際的問題與如何設計類和構造代碼有關。 我已經向您展示了您在代碼中做錯了什么,但是可以使用更好的結構化方法來解決您的問題。

在寫我的答案時,貝耶勒(Beyeler)已經為您回答了這一部分,因此請檢查他的答案。

編輯

在您的代碼中,您可能正在做

using namespace std;

但是您已經寫了vector< Point >std::cout 首先,您不應該使用using行,以避免名稱沖突並幫助您知道此向量的來源。

但是,如果您堅持使用此行,那么您不必寫std:: :(如果您知道自己在做什么就可以了),不要在一件事上鍵入std:: ,然后在其中省略它另一個。 保持一致,無論使用還是不使用。

我發現您的代碼出現了三個錯誤:

  1. “ do”是c ++關鍵字。 您不能使用它來命名結構。

  2. “ memfuncbool”的參數缺少其類型。 const&不是類型。

  3. 結構定義后缺少分號。

我也認為var1,var2和arg定義良好。 如果不是,那是一個錯誤。

糾正這些錯誤后,您可以執行以下操作:

if(tempbool) { /*statements*/ };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM