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

时间:2018-04-29 00:10:58

标签: c++

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

Employee

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

#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;
}

存在默认构造函数

时成功运行输出
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]'

2 个答案:

答案 0 :(得分:5)

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

resize的信息,请访问cppreference。你落入第一个(单一论证)形式。

答案 1 :(得分:3)

而不是需要默认构造函数的students.resize(1);,您可以执行students.pop_back();,或者,如果向量的大小可能不同,则students.erase(students.begin()+1, students.end());