很多不合理的编译器错误c ++ [英] lots of unreasonable compiler errors c++

查看:97
本文介绍了很多不合理的编译器错误c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写大学管理课程作为大学的任务。
到目前为止,Ive创建了学生和课程类似乎是确定,直到在某些时候,当我试图建立他们完全,我收到几十个不合理的错误,如 -

Im writing a sort of college management program as a college task. So far Ive created the student and course classes which seemed to be ok, until at some point when I try to build them altogether I get tens of unreasonable errors, such as -

error C2146: syntax error : missing ';' before identifier 'name'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error : missing ';' before identifier 'getname'
error C2805: binary 'operator +=' has too few parameters

代码很长,但我相信这些错误没有任何意义,希望你能帮助我在这里散发一些光。

The code is pretty long but Im pretty sure none of this errors make sense, hopefully you could help me shed some light here.

EDIT - 我尝试声明class course;在student.h,包括student.h在course.h。
但显然编译器不能识别学生文件中的任何课程。
我错了吗?

EDIT - I tried declaring "class course;" on student.h, and including student.h on course.h. But apparently the compiler doesnt recognize any "course" in student files. Am I getting this wrong?

档案 - student.h

file - student.h

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include "course.h"
#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;

class course;
class student{
private:
    string name;
    int id;
    string gender;
    int age;

public:
    int amountofcourses;
    student();
    ~student();
    student& operator=(const student&);
    bool operator==(const student&) const;
    bool operator>(const int avg);
    friend ostream& operator<<(ostream&, student&);
    friend istream& operator>>(istream&, student&);
    void operator+=(course&);
    void operator-=(course&);
    course operator[](const int);
    const string getname();
    void setname(string);
    const int getid() const;
    void setid(int);
    const string getgender();
    void setgender(string);
    const int getage();
    void setage(int);
    course **courses;
};

档案 - student.cpp

file - student.cpp

#include "student.h"
student::student(){
    courses = NULL;
    course *courses = new course;
}

student::~student(){
    delete[] courses;
}

const string student::getname(){
    return this->name;
}

void student::setname(string newname){
    this->name = newname;
}

const int student::getid() const{
    return this->id;
}

void student::setid(int newid){
    this->id = newid;
}

const string student::getgender(){
    return this->gender;
}

void student::setgender(string newgender){
    this->gender = newgender;
}

const int student::getage(){
    return this->age;
}

void student::setage(int newage){
    this->age = newage;
}

student& student::operator=(const student& other){
    this->setname(other.name);
    this->setid(other.id);
    this->setgender(other.gender);
    this->setage(other.age);
    return *this;
}

bool student::operator==(const student& other) const{
    if (this->getid() == other.getid()){
        return true;
    }
    return false;
}

ostream& operator<<(ostream& out,student& astudent){
    out << "Student's name :" << astudent.getname() << endl;
    out << "Student's ID :" << astudent.getid() << endl;
    out << "Student's gender :" << astudent.getgender() << endl;
    out << "Student's age :" << astudent.getage() << endl;
    out << "The student is registered to the following courses :" << endl;
    for (int i = 0; i < astudent.amountofcourses; i++){
        out << astudent.courses[i]->getname() << endl;
    }
    return out;
}

istream& operator>>(istream& in, student& astudent){
    int numvalidation=0;
    string gender;
    cout << "Enter student's name " << endl;
    in >> astudent.name;
    cout << "Enter student's ID " << endl; 
    cin >> numvalidation;
    while (cin.fail() || numvalidation < 100000000 || numvalidation > 999999999) {
        cin.clear(); 
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Incorrect input, an ID number contains only DIGITS and must be 9 digit long" << endl;
        cin >> numvalidation;
    }
    astudent.id = numvalidation;
    cout << "Enter student's gender - female or male " << endl;
    while (1) {
        cin >> gender;
        if (gender != "female" && gender != "male" && gender != "MALE" && gender != "FEMALE"){
            cout << "Incorrect input - only female or male" << endl;
            continue;
        }
        break;
    }
    astudent.gender = gender;
    cout << "Enter student's age " << endl;
    cin >> numvalidation;
    while (cin.fail() || numvalidation < 6 || numvalidation > 160) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Incorrect input, an age number contains only DIGITS (or you entered a rediculous number)" << endl;
        cin >> numvalidation;
    }
    astudent.age = numvalidation;
    return in;
}

bool student::operator>(const int avg){
    int sum = 0, cnt = 0;
    for (int i = 0; i < amountofcourses; i++){
        sum += courses[i]->grades[i];
        cnt++;
    }
    sum /= cnt;
    if (sum <= avg){
        return false;
    }
    return true;
}

void student::operator+=(course& newcourse){
    for (int i = 0; i < amountofcourses + 1; i++){
        if (**(courses + i) == newcourse){
            cout << "This course is already enlisted" << endl;
            return;
        }
    }
    courses[amountofcourses] = &newcourse;
    amountofcourses++;
    course *courses = new course[amountofcourses];
}

void student::operator-=(course& newcourse){
    int cnt = 0;
    for (int i = 0; i < amountofcourses + 1; i++){
        if (**(courses + i) == newcourse){
            cnt++;
        }
    }
    if (cnt == 0){
        cout << "The student isnt enlisted on this course" << endl;
        return;
    }
    if (cnt > 0){
        for (int i = 0; i < amountofcourses + 1; i++){
            if (**(courses + i) == newcourse){
                courses[i] = courses[i + 1];
            }
        }
    }
    cout << "The course has been removed" << endl;
}

course student::operator[](const int index){
    return **(courses + index);
}

档案 - course.h

file - course.h

#include "student.h"
#pragma once

class course{
private:
    string name;
    int num;
    int amountofstudents;

public:
    course();
    ~course();
    int *grades;
    student **students;
    const string getname() const;
    void setname(const string);
    const int getnum() const;
    void setnum(const int);
    const int getamountofstudents() const;
    void setamountofstudents(const int);
    course& operator=(const course&);
    bool operator==(const course&) const;
    void operator+=(student&);
    void operator-=(student&);
    student operator[](const int);
};

档案 - course.cpp

file - course.cpp

#include "course.h"

course::course(){
    int *grades = new int;
    student *students = new student;
}

course::~course(){
    delete[] grades;
    delete[] students;
}

const string course::getname() const{
    return this->name;
}

void course::setname(const string newname){
    this->name = newname;
}

const int course::getnum() const{
    return this->num;
}

void course::setnum(const int newnum){
    this->num = newnum;
}

const int course::getamountofstudents() const{
    return this->amountofstudents;
}

void course::setamountofstudents(const int newamount){
    this->amountofstudents = newamount;
}

course& course::operator=(const course& other){
    this->name = other.name;
    this->num = other.num;
    this->amountofstudents = other.amountofstudents;
    this->grades = other.grades;
    this->students = other.students;
    return *this;
}

bool course::operator==(const course& other) const{
    if (this->num == other.num){
        return true;
    }
    return false;
}

void course::operator+=(student& newstudent){
    for (int i = 0; i < amountofstudents + 1; i++){
        if (**(students+i)==newstudent){
            cout << "The student is already registered to this course" << endl;
            return;
        }
    }
    students[amountofstudents] = &newstudent;
    grades[amountofstudents] = -1;
    amountofstudents++;
    student *students = new student[amountofstudents];
    int *grades = new int[amountofstudents];
}

void course::operator-=(student& newstudent){
    int cnt = 0;
    for (int i = 0; i < amountofstudents + 1; i++){
        if (**(students + i) == newstudent){
            cnt++;
            break;
        }
    }
    if (cnt == 0){
        cout << "The student isnt registered to this course" << endl;
        return;
    }
    if (cnt > 0){
        for (int i = 0; i < amountofstudents + 1; i++){
            if (**(students + i) == newstudent){
                students[i] = students[i + 1];
            }
        }
    }
    cout << "The student has been removed from this course" << endl;
}

student course::operator[](const int index){
    return **(students+index);
}


推荐答案

c> course.h 和 student.h 循环(这是无意义的),你在包含任何标准标题之前。毫不奇怪,对 string course.h 中的任何引用不编译:没有 course.h 中编译器已知的任何类型的字符串

You included course.h and student.h into each other cyclically (which is always pointless), and you did that before including any standard headers. Not surprisingly, any references to string in course.h don't compile: there's no string of any kind known to the compiler in course.h.

这篇关于很多不合理的编译器错误c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆