在c++中使用boost拆分字符串两次



我正试图使用分隔符从文件中分割数据,但之后我想再次使用空白分割某些字符串,但我一直遇到编译器错误。这段代码如下…

PS:我没有分享所有的代码,所以如果它看起来不完整,这就是为什么,我只是在寻找一种方法来重新分割所选的字符串。

bool loadProperties(char* residential, char* commercial,
                    PropertyContainer* thePropertyContainer) {
   ifstream propertyFile;
   int datapos = 6;
   Property* propertyType = NULL;
   propertyFile.open(residential, ios::in);
   if (!propertyFile.is_open()) {
      cout << "Cant open file: " << residential << endl;
      return false;
   }
   while (propertyFile.good()) {
      getline(propertyFile, buffer);
      typedef tokenizer<char_separator<char> > tokenizer;
      char_separator<char> sep("nt,*");
      tokenizer tokens(buffer, sep);
      for (tokenizer::iterator pos = tokens.begin();
            pos != tokens.end(); ++pos) {
         if (!validate(datapos, *pos)) {
            return false;
         }
         switch(datapos) {
         case 6:
            vector < string > splitstring; <------------------------ (LINE 127)
            boost::split(splitstring, *pos, boost::is_any_of(" ")); <------this is where I am trying to split via white space for a second time.
            if (splitstring[0] == "RS") {
               propertyType = new ResSales;
            } else if (splitstring[0] == "RS") {
               propertyType = new ResRentals;
            } else {
               return false;
            }
            break;
         case 7:
            propertyType->setOwner(*pos);
            break;
         case 8:
            propertyType->setAddress(*pos);
            break;
         case 9:
            propertyType->setSuburb(*pos);
            break;
         case 10:
            propertyType->setPostcode(changeString<int>(*pos));
            break;
         case 11:
            dynamic_cast<Residential*>(propertyType)->setBedrooms
                        (changeString<int>(*pos));
            break;
         case 12:
            if (propertyType->getType() == "ResSales") {
               dynamic_cast<Sales*>(propertyType)->setAuctionDate(*pos);
            } else if (propertyType->getType() == "ResRentals") {
               dynamic_cast<Rentals*>(propertyType)->setBond
               (changeString<double>(*pos));
            }
            break;
         case 13:
            if (propertyType->getType() == "ResSales") {
               dynamic_cast<Sales*>(propertyType)->setPrice
               (changeString<double>(*pos));
            } else if (propertyType->getType() == "ResRentals") {
               dynamic_cast<Rentals*>(propertyType)->setMonthlyRent
               (changeString<double>(*pos));
            }
            break;
         }
         if (datapos >= 14) {
            thePropertyContainer->addProperty(propertyType);
            datapos = 6;
         } else {
            ++datapos;
         }
      }


   }
   propertyFile.close();
   return true;
}

基本上,如果情况是6,我想再次空白分割令牌,我不知所措,如何实现这一点,我正试图使用boost,你可以告诉。但我得到了错误....

任何帮助都非常感激,谢谢。

更新:在评论中与一些人讨论了后续问题后,似乎问题是与switch语句作用域有关,现在只需要阻塞代码部分并像对待一样工作,谢谢大家。

您似乎正在遵循一个类似于Boost文档中的示例,但缺少一行。

typedef vector < string > splitstring;
splitstring stringthathasbeensplit;
boost::split(stringthathasbeensplit, *pos, boost::is_any_of(" "));

问题是,您声明splitstring是类型vector<string>的别名。split将接受vector<string>类型的对象,但不是定义本身-这是通过传递splitstring传递给它的。

实际上,您应该把typedef放到一起,直接声明一个类型为vector<string>的对象,因为typedef在这个上下文中没有任何作用。如果你删除typedef关键字,它会像你写的那样工作。

vector < string > splitstring;
boost::split(splitstring, *pos, boost::is_any_of(" "));

在函数内部有一个类型定义是非常奇怪的。我怀疑你只是想定义一个类型为std::vector<std::string>的变量。为此,您可以简单地写入:

std::vector<std::string> splitstring;

您可以将typedef行替换为该行。

编辑:另一个问题是,你试图在一个开关语句中定义这个对象,这类似于一堆"goto"语句和标签。应该将对象放在它自己的作用域中,这样就不会有代码路径跳到它的作用域中,否则就会得到编译器错误。要做到这一点,只需将对象定义和使用它的任何代码用一组额外的括号括起来:

   case 6:
        {
            std::vector<std::string> splitstring;
            boost::split(splitstring, *pos, boost::is_any_of(" "));
            if (splitstring[0] == "RS") {
                propertyType = new ResSales;
            } else if (splitstring[0] == "RS") {
                propertyType = new ResRentals;
            } else {
                return false;
            }
        }
        break;

免责声明:我没有测试过这段代码,可能有我没有注意到的其他错误或拼写错误

最新更新