Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. istream& operator>>(istream& is, Card& d)
  2. {
  3. std::map<std::string,Card::Rank> mr;
  4. std::map<std::string,Card::Suit> ms;
  5. mr[std::regex("/two/i")] = Card::TWO;
  6. mr[std::regex("/three/i")] = Card::THREE;
  7. mr[std::regex("/two/i")] = Card::FOUR;
  8. mr[std::regex("/two/i")] = Card::FIVE;
  9. mr[std::regex("/two/i")] = Card::SIX;
  10. mr[std::regex("/two/i")] = Card::SEVEN;
  11. mr[std::regex("/two/i")] = Card::EIGHT;
  12. mr[std::regex("/two/i")] = Card::NINE;
  13. mr[std::regex("/two/i")] = Card::TEN;
  14. mr[std::regex("/two/i")] = Card::JACK;
  15. mr[std::regex("/two/i")] = Card::QUEEN;
  16. mr[std::regex("/two/i")] = Card::KING;
  17. ms[std::regex("/clubs/i")] = Card::CLUBS;
  18. ms[std::regex("/diamonds/i")] = Card::DIAMONDS;
  19. ms[std::regex("/hearts/i")] = Card::HEARTS;
  20. ms[std::regex("/spades/i")] = Card::SPADES;
  21.  
  22. string srank, ssuit;
  23. char c1;
  24.  
  25. if (is >> srank >> c1 >> ssuit)
  26. {
  27. if (c1 == 'of')
  28. {
  29. Card::Rank rank = mr[srank];
  30. Card::Suit suit = ms[ssuit];
  31. d = Card(rank, suit);
  32. }
  33. else
  34. {
  35. is.clear(ios_base::failbit);
  36. }
  37. }
  38.  
  39. return is;
  40. }
  41.  
  42. error C2679: binary '[' : no operator found which takes a right-hand operand of type 'std::basic_regex<_Elem>' (or there is no acceptable conversion)
  43. with
  44. [
  45. _Elem=char
  46. ]
  47. c:program files (x86)microsoft visual studio 11.0vcincludemap(173): could be 'Card::Rank &std::map<_Kty,_Ty>::operator [](std::basic_string<_Elem,_Traits,_Alloc> &&)'
  48. with
  49. [
  50. _Kty=std::string,
  51. _Ty=Card::Rank,
  52. _Elem=char,
  53. _Traits=std::char_traits<char>,
  54. _Alloc=std::allocator<char>
  55. ]
  56. c:program files (x86)microsoft visual studio 11.0vcincludemap(190): or 'Card::Rank &std::map<_Kty,_Ty>::operator [](const std::basic_string<_Elem,_Traits,_Alloc> &)'
  57. with
  58. [
  59. _Kty=std::string,
  60. _Ty=Card::Rank,
  61. _Elem=char,
  62. _Traits=std::char_traits<char>,
  63. _Alloc=std::allocator<char>
  64. ]
  65. while trying to match the argument list '(std::map<_Kty,_Ty>, std::basic_regex<_Elem>)'
  66. with
  67. [
  68. _Kty=std::string,
  69. _Ty=Card::Rank
  70. ]
  71. and
  72. [
  73. _Elem=char
  74. ]
  75.  
  76. etc.
  77.  
  78. #include <cctype>
  79.  
  80. static inline void stringToLower(std::string &s) {
  81. for (uint i = 0; i < s.size(); ++i) {
  82. s[i] = tolower(s[i]);
  83. }
  84. }
  85.  
  86. istream& operator>>(istream& is, Card& d)
  87. {
  88. std::map<std::string,Card::Rank> mr;
  89. std::map<std::string,Card::Suit> ms;
  90. mr["two"] = Card::TWO;
  91. mr["three"] = Card::THREE;
  92. ...
  93. mr["king"] = Card::KING;
  94.  
  95. ms["clubs"] = Card::CLUBS;
  96. ...
  97.  
  98. string srank, ssuit, c1;
  99.  
  100. if (is >> srank >> c1 >> ssuit)
  101. {
  102. stringToLower(c1);
  103. if (c1 == "of")
  104. {
  105. stringToLower(srank);
  106. stringToLower(ssuit);
  107. Card::Rank rank = mr[srank];
  108. Card::Suit suit = ms[ssuit];
  109. d = Card(rank, suit);
  110. }
  111. else
  112. {
  113. is.clear(ios_base::failbit);
  114. }
  115. }
  116.  
  117. return is;
  118. }
  119.  
  120. static std::map<std::string,Card::Rank> createmr() {
  121. std::map<std::string,Card::Rank> mr;
  122. mr["two"] = Card::TWO;
  123. mr["three"] = Card::THREE;
  124. ...
  125. mr["king"] = Card::KING;
  126. return mr;
  127. }
  128.  
  129. static std::map<std::string,Card::Rank> createms() {
  130. std::map<std::string,Card::Rank> ms;
  131. ms["clubs"] = Card::CLUBS;
  132. ...
  133. return ms;
  134. }
  135.  
  136. istream& operator>>(istream& is, Card& d)
  137. {
  138. static std::map<std::string,Card::Rank> mr = createmr(); // note `static` keyword
  139. static std::map<std::string,Card::Suit> ms = createms();
  140. ...
  141. }
  142.  
  143. static std::map<std::string,Card::Rank> mr = createmr(); // note `static` keyword
  144. static std::map<std::string,Card::Suit> ms = createms();
  145.  
  146. istream& operator>>(istream& is, Card& d)
  147. {
  148. ...
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement