gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:07:54 29 4
gpt4 key购买 nike

我创建了两个类(学生和类(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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com