【问题标题】:Trying to return a Bool Value from a class in C++尝试从 C++ 中的类返回 Bool 值
【发布时间】:2021-02-07 17:49:27
【问题描述】:

我对编码和 C++ 世界很陌生。我的任务是从具有不同值行的 .txt 文档中获取数字。我需要对每一行进行错误检查,以确保文件格式正确。我希望创建一个可以重用的类来进行这些检查。我,即:

  1. 从第一行获取数据
  2. 检查数据是否符合条件(在这种情况下,它必须是 1 或 0,稍后我将不得不根据此值应用不同的操作)
  3. 根据结果在主文件中触发 true / false if 语句。

Main.cpp

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <functional>
    #include "ErrorSwitch.h"
using namespace std;

//Set pointer of 'file' to beginning line of 'num'
fstream& GotoLine(fstream& file, unsigned int num) {
    file.seekg(ios::beg);
    //Loop through file
    for (int i = 0; i < num - 1; ++i) {
        file.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    return file;
}
int main() {
    fstream file("file1.txt");
    //Check Line 1
    GotoLine(file, 1);
    //Get Input from First Line & Convert to int
    string line1;
    file >> line1;
    int inpu = stoi(line1);
    //Error Check
    ErrorSwitch checkOne;
    checkOne.input = inpu;
    checkOne.errorCheck();
    if (checkOne == true) { //I can't seem to get this to work as a boolean check
        //Do code here
    }
    else if (checkOne == false) {
        //Do code here
    }
    
}

ErrorSwitch.h

#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <functional>
using namespace std;

class ErrorSwitch
{
public:
    int input;

    bool errorCheck() {
        switch (input) {
        case 0:
            return true;
            break;
        case 1:
            return true;
            break;
        default:
            return false;
        }
    }
};

有人可以看看这个并帮助我了解如何从这个类函数中获取返回值。 谢谢!

【问题讨论】:

    标签: c++ class boolean


    【解决方案1】:

    布尔值是从函数errorCheck返回的,而不是类本身。

    您可以像这样将返回值存储到一个变量中:

        bool checkRes = checkOne.errorCheck();
        if (checkRes == true) {
            //Do code here
        }
        else if (checkRes == false) {
            //Do code here
        }
    

    或者干脆做:

        if (checkOne.errorCheck()) {
            //Do code here
        }
        else { // if bool is not true, it is false
            //Do code here
        }
    

    或者,您可以像这样添加一个运算符将类转换为bool

        operator bool() {
            return errorCheck();
        }
    

    ErrorSwitch 班级让这个代码工作:

        if (checkOne == true) { //I can't seem to get this to work as a boolean check
            //Do code here
        }
        else if (checkOne == false) {
            //Do code here
        }
    

    【讨论】:

      【解决方案2】:

      改变这个:

      checkOne.errorCheck();
      if (checkOne == true) {
          //Do code here
      }
      else if (checkOne == false) {
          //Do code here
      }
      

      到这里:

      if (checkOne.errorCheck()) {
          //Do code here
      }
      else {
          //Do code here
      }
      

      【讨论】:

        猜你喜欢
        • 2020-04-14
        • 2013-07-11
        • 2017-02-22
        • 2023-04-09
        • 2016-11-04
        • 2019-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多