c++ - gcc 编译器错误 : STL_construct. h,STL_uninitialized.h

标签 c++

我创建了两个类(class)(学生和类(class)):

#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cmath>
#include <list>
using namespace std;




class Student{
    private:
    string name;
    int matr_num;
    int test_1;
    int test_2;

    public:
    //Constructor:
    Student(string name,int matr_num,int test_1,int test_2);
    //Destructor:
    ~Student();
    //Copy constructor:
    Student(const Student& other);
    //Copy assingment:
    Student operator=(const Student& other);
    //Move constructor:
    Student(Student&& other);
    //Move assingment:
    Student operator=(Student&&other);  
    //Average of two tests:
    double test_average();
    //get name,matrikel_number, scores of test:
    string get_name() const ;
    double get_matr_num() const;
    double get_test_1() const ;
    double get_test_2() const ;

};

//Constructor
Student::Student(string name,int matr_num,int test_1,int test_2)
    :name{name},matr_num{matr_num},test_1{test_1},test_2{test_2}
        {
        }

//Destructor
Student::~Student(){
}

//Copy constructor:
Student::Student(const Student& other)
    :name{other.name},matr_num{other.matr_num},test_1{other.test_1},test_2{other.test_2}
        {

        }


//Copy assingment:
Student Student::operator=(const Student& other)
{
    name = other.name;
    matr_num = other.matr_num;
    test_1 = other.test_1;
    test_2 = other.test_2;

}   


//Move Constructor:
Student::Student(Student&& other)
    :name{other.name},matr_num{other.matr_num},test_1{other.test_1},test_2{other.test_2}
        {
            other.name = "";
            other.matr_num = 0;
            other.test_1 = 0;
            other.test_2 = 0;
        }       

//Move assingment:
Student Student::operator=(Student&& other)
{
    name = other.name;
    matr_num = other.matr_num;
    test_1 = other.test_1;
    test_2 = other.test_2;
    other.name = "";
    other.matr_num = 0;
    other.test_1 = 0;
    other.test_2 = 0;
}   

double Student::test_average(){
    return (test_1 + test_2)/2;
}


string Student::get_name()const{return name;}
double Student::get_matr_num()const{return matr_num;}
double Student::get_test_1()const{return test_1;}
double Student::get_test_2()const{return test_2;}


//Course class:
class Course{
private:
    string titel;
    vector<Student> stud_list;

public:
    //Default Constructor:
    Course(string titel);
    //Constructor:
    Course(string titel,vector<Student> students);
    //Destructor:
    ~Course(){};
    //Copy constructor:
    Course(const Course& other);
    //Copy assingment:
    Course operator=(const Course& other);
    //Move constructor:
    Course(Course&& other);
    //Move assingment:
    Course operator=(Course&&other);    
    //average if course for test_1:
    double ave_test_1(){
        double result = 0;
        for(int i = 0; i<stud_list.size();++i){
            result += stud_list[i].get_test_1();
        }
        return result/stud_list.size();
    }

    double ave_test_2(){
        double result = 0;
        for(int i = 0; i<stud_list.size();++i){
            result += stud_list[i].get_test_2();
        }
        return result/stud_list.size();
    }

    double average(){
        double result = 0;
        for(int i = 0; i<stud_list.size();++i){
            result += stud_list[i].test_average();
        }
        return result/stud_list.size();
    }

};


//Default constructor:
Course::Course(string titel)
    : titel{titel}, stud_list{0}{}


//Constructor:
Course::Course(string titel,vector<Student> students){
    titel = titel;
    for(int i = 0;i<students.size();++i){
        stud_list[i] = students[i];
    }
}

//Copy constructor:
Course::Course(const Course& other)
    : titel{other.titel},stud_list{other.stud_list} {}

//Copy assingment:
Course Course::operator=(const Course& other){
    titel = other.titel;
    stud_list = other.stud_list;
}






int main(){
    Student first("Ro Fi", 0123,90,95);
    Student second("Mat Dam",0111,80,87);
    Student third("Mike Ross",0222, 75, 85);

    cout<<first.get_test_1();
}

但是在编译过程中我遇到了以下错误:

n file included from /usr/include/c++/7/vector:62:0,
                 from main.cpp:3:
/usr/include/c++/7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = Student; _Args = {}]’:
/usr/include/c++/7/bits/stl_uninitialized.h:527:18:   required from ‘static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Student*; _Size = long unsigned int; bool _TrivialValueType = false]’
/usr/include/c++/7/bits/stl_uninitialized.h:583:20:   required from ‘_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Student*; _Size = long unsigned int]’
/usr/include/c++/7/bits/stl_uninitialized.h:645:44:   required from ‘_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = Student*; _Size = long unsigned int; _Tp = Student]’
/usr/include/c++/7/bits/stl_vector.h:1347:36:   required from ‘void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Student; _Alloc = std::allocator<Student>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/7/bits/stl_vector.h:285:30:   required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = Student; _Alloc = std::allocator<Student>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Student>]’
main.cpp:158:29:   required from here
/usr/include/c++/7/bits/stl_construct.h:75:7: error: no matching function for call to ‘Student::Student()’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:74:1: note: candidate: Student::Student(Student&&)
 Student::Student(Student&& other)
 ^~~~~~~
main.cpp:74:1: note:   candidate expects 1 argument, 0 provided
main.cpp:55:1: note: candidate: Student::Student(const Student&)
 Student::Student(const Student& other)
 ^~~~~~~
main.cpp:55:1: note:   candidate expects 1 argument, 0 provided
main.cpp:45:1: note: candidate: Student::Student(std::__cxx11::string, int, int, int)
 Student::Student(string name,int matr_num,int test_1,int test_2)
 ^~~~~~~
main.cpp:45:1: note:   candidate expects 4 arguments, 0 provided

我不知道这个错误是什么意思。您能解释一下这里发生了什么以及如何修复它吗?

学生类工作正常,没有任何错误。但是当我添加 Course 类时,我收到了上述错误。那么,这是什么原因呢?

最佳答案

问题出在构造函数中:

Course::Course(string titel)
: titel{titel}
, stud_list{0}{} <--- in here

只需删除 , stud_list{0}{}std::vector<>有一个默认构造函数,可以为您初始化 0 个元素。

您可能还想通过添加 -Wall -Wextra 来编译代码并启用警告。到编译器命令行。

关于c++ - gcc 编译器错误 : STL_construct. h,STL_uninitialized.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48228599/

相关文章:

c++ - 使用直接函数 VS std::invoke

c++ - 如何在不使用当前段落样式的情况下追加到QTextEdit

c++ - 在 C++ 流中使用 UTF-8 的正确跨平台方式

c++ - 我不知道如何处理我的非默认构造函数中传递的指针

c++ - 高效的,极不可能的条件?

c++ - valgrind 在使用 libxml2 时向我显示泄漏

c++ - 为什么这个递归函数计算二叉搜索树中的节点总是返回比预期更大的结果?

c++ - 互斥条件信令循环如何工作?

c++ - 匿名对象的属性

c++ - 正确编写引用的比较运算符