C++默认构造函数不在运行时使用,但在编译时需要

Lin*_* Ma 9 c++

这是我的代码.如果我删除默认构造函数,下面会出现错误.但是如果我添加一个默认构造函数,它将没有编译和运行的问题.想知道为什么?我特别困惑,因为在运行时根本没有使用默认构造函数,为什么在编译时需要它?

#include <iostream>
#include <vector>
#include <string>

class Foo {
 public:
  //Foo();
  Foo(const std::string& name, double score);
  Foo(const Foo& other);
  Foo(Foo&& other);
  const std::string& name() const { return name_; }
  double score() const { return score_; }

 private:
  std::string name_;
  double score_;
};

/*
Foo::Foo() {
  std::cout << "In default constructor " << std::endl;
  name_ = "foo";
  score_ = 1.0;
}*/

Foo::Foo(const std::string& name, double score) : name_(name), score_(score) {
  std::cout << "In parametered constructor " << std::endl;
}

Foo::Foo(const Foo& other) {
  std::cout << "In copy constructor " << std::endl;
  name_ = other.name();
  score_ = other.score();
}

Foo::Foo(Foo&& other)
    : name_(std::move(other.name())), score_(std::move(other.score())) {
  std::cout << "In move constructor " << std::endl;
}

int main(int argc, char** argv) {
  std::vector<Foo> students;
  students.emplace_back("name1", 4.0);
  students.emplace_back("name2", 5.0);
  std::cout << "resizing begin " << std::endl;
  students.resize(1);
  for (Foo student : students) {
    std::cout << "student name: " << student.name()
              << " student score: " << student.score() << std::endl;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

没有默认构造函数时的错误消息,

Error:
  required from 'static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int; bool _TrivialValueType = false]'
Run Code Online (Sandbox Code Playgroud)

有默认构造函数时成功运行输出,

In parametered constructor 
In parametered constructor 
In copy constructor 
resizing begin 
In copy constructor 
student name: name1 student score: 4
Run Code Online (Sandbox Code Playgroud)

Ste*_*ell 5

问题是你的呼吁resize(具体而言students.resize(1)); 当删除该行时,代码将编译.问题是resize必须初始化新元素,如果它不够大,那么它需要默认的构造函数.如果你想缩小你的尺寸students而不确保它足够大,你可以使用erase(Max Vollmer有一个具体的例子).

有关信息,resize请访问cppreference.你陷入了第一个(单个参数)形式.