相关疑难解决方法(0)

最烦恼的解析

我从这里得到了代码.

class Timer {
 public:
  Timer();
};

class TimeKeeper {
 public:
  TimeKeeper(const Timer& t);

  int get_time()
  {
      return 1;
  }
};

int main() {
  TimeKeeper time_keeper(Timer());
  return time_keeper.get_time();
}
Run Code Online (Sandbox Code Playgroud)

从它的外观来看,由于该行应该会出现编译错误:

TimeKeeper time_keeper(Timer());
Run Code Online (Sandbox Code Playgroud)

但只有在return time_keeper.get_time();存在的情况下才会发生 .

为什么这条线甚至很重要,编译器会发现time_keeper(Timer() )构造上的含糊不清.

c++ most-vexing-parse

27
推荐指数
1
解决办法
2436
查看次数

c ++向量构造函数的奇怪行为

我注意到了c ++ vector构造函数的一些奇怪的行为,有人可以为我解释一下吗?谢谢.

代码段1有效:

#include <string>
#include <vector>
#include <iostream>
#include <istream>
#include <ostream>
#include <iterator>
#include <sstream>
#include <algorithm>

using namespace std;

int main()
{
  string str = "The quick brown fox";

  stringstream strstr(str);

  istream_iterator<string> start(strstr);
  vector<string> results(start, istream_iterator<string>());

  ostream_iterator<string> oit(cout, "\n");
  copy(results.begin(), results.end(), oit);
}
Run Code Online (Sandbox Code Playgroud)

但是代码段2没有:

#include <string>
#include <vector>
#include <iostream>
#include <istream>
#include <ostream>
#include <iterator>
#include <sstream>
#include <algorithm>

using namespace std;

int main()
{
  string str = "The quick brown fox";

  stringstream strstr(str);

  vector<string> …
Run Code Online (Sandbox Code Playgroud)

c++ stl vector

0
推荐指数
1
解决办法
182
查看次数

标签 统计

c++ ×2

most-vexing-parse ×1

stl ×1

vector ×1