C ++-我如何在istringstream中误用忽略?

问题描述 投票:0回答:2

我计划在主代码中要求用户以(800)555-1212的形式输入电话号码,该电话号码将发送到我的PhoneNumber构造函数,然后发送到setPhoneNumber进行分解,设置我的私有变量,并分析错误。另外,我想编写我的setPhoneNumber代码,以解决原始输入中的用户错误。我的PhoneNumber.h代码为:

// PhoneNumber.h
#ifndef PHONENUMBER_H
#define PHONENUMBER_H

#include <string>

class PhoneNumber {
   private:
      short areaCode;
      short exchange;
      short line;
   public:
      PhoneNumber(std::string number);
      void setPhoneNumber(std::string number);
      void printPhoneNumber() const;
};
#endif

这是我的PhoneNumber.cpp代码

// PhoneNumber.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <cctype>
#include "PhoneNumber.h"

PhoneNumber::PhoneNumber(std::string number) {
   setPhoneNumber(number);
}

void PhoneNumber::setPhoneNumber(std::string number) {
   bool areaCodeDone = false;
   bool exchangeDone = false;
   int length = number.length();

   std::istringstream iss(number);
   for (int i = 0; i < length; i++) {
      if (! areaCodeDone) {
         if (! std::isdigit(number[i])) {
            std::string str;
            iss >> std::ignore();
         }
         else {
            iss >> std::setw(3) >> areaCode;
            areaCodeDone = true;
         }
      }
      else if (! exchangeDone) {
         if (! std::isdigit(number[i])) {
            iss >> std::ignore();
         }
         else {
            iss >> std::setw(3) >> exchange;
            exchangeDone = true;
         }
      }
      else {
         if (! std::isdigit(number[i])) {
            iss >> std::ignore();
         }
         else {
            if (length - i < 4) {
               throw std::invalid_argument("Something wrong with phone number entry.");
            }
            else {
               iss >> std::setw(4) >> line;
            }
         }
      }
   }
}

我得到的错误是我在std :: ignore上看到的,但是我不知道我是如何错误地使用它。来自g ++编译器的错误是:

PhoneNumber.cpp:23:32:错误:与“(const std :: _ Swallow_assign)()”的调用不匹配]

iss >> std :: ignore();

有人可以分析和协助吗?

c++ ignore istringstream
2个回答
1
投票

没有std::ignore()。代替


0
投票

std::ignore并没有您认为的那样:它用于元组。

© www.soinside.com 2019 - 2024. All rights reserved.