【问题标题】:How to invoke struct member functions based on return value of a bool type member function?如何根据 bool 类型成员函数的返回值调用 struct 成员函数?
【发布时间】:2015-09-07 01:56:12
【问题描述】:

我想在结构的 bool 成员函数中进行一些条件检查。我的 struct 对象 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;
    }
}

【问题讨论】:

  • do 是 C++ 中的关键字,顺便说一下。你不能用它来命名事物。
  • 错误 LNK2001: 无法解析的外部符号“public: bool __thiscall doSth::memfuncbool(class std::vector> const &)” (?memfuncbool @doSth@@QAE_NABV?$vector@UPoint@@V?$allocator@UPoint@@@std@@@std@@@Z)
  • 这是你的真实代码吗?您是否有定义,而不仅仅是在您的实际代码中声明 doSth::memfuncbool

标签: c++ struct boolean


【解决方案1】:

有两种方法:调用者安全的封装和纯代码规范。稍后您自己确保您编写的代码始终知道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
    }
};

【讨论】:

    【解决方案2】:

    如果您不尝试做比您知道的更多的事情,您的代码有很多问题很容易解决。

    1 您在 main 函数之后定义 Point 和 doSth 结构。所以 main 函数无法知道你的结构是做什么的。通常你会使用一个头文件来包含声明和 cpp 文件来包含实现,但是由于你正在做一个小程序,你可以将你的结构的定义移动到主函数之上。或者,您可以在 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&lt; Point &gt;std::cout。首先,您不应使用 using 行,以避免名称冲突并帮助您了解该向量的来源。

    但是,如果你坚持使用这条线,这样你就不必写 std::(如果你知道你在做什么就可以了),不要在某件事上输入 std::,然后在另一个中省略它。保持一致,要么使用它,要么不使用它。

    【讨论】:

      【解决方案3】:

      我发现您的代码存在三个错误:

      1. “do”是一个 c++ 关键字。你不能用它来命名结构。

      2. “memfuncbool”的参数缺少其类型。 const& 不是类型。

      3. 结构定义后缺少分号。

      我还认为 var1、var2 和 arg 定义明确。如果他们甚至都不是,那是一个错误。

      纠正这些错误后,也许你可以做一些类似的事情-

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

      【讨论】:

      • 谢谢阿尼什。我对代码做了一些更改。仍然收到上面未解决的外部符号错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      相关资源
      最近更新 更多