【问题标题】:C++ Visual Studio 2017 getting error code C3867 with my Vector of Objects [duplicate]C++ Visual Studio 2017 使用我的对象向量获取错误代码 C3867 [重复]
【发布时间】:2018-01-07 08:28:42
【问题描述】:

我现在陷入了目前的困境。我正在尝试将对象向量传递到我的函数中,该函数将打印或保存到文件中。但是每当我尝试遍历我的向量时,它都会显示“C3867”


我的代码是:

#include <iostream>
#include <string>
#include <vector>
#include "Student.h"

using namespace std;

/* FUNCTIONS */
void programStart();
void saveClassInfo(vector<Student> &newClass);
void printClassInfo(vector<Student> &newClass);

int main()
{
    programStart();
    cout << "Press any key to close...";
    cin.get();
    cin.get();
    return EXIT_SUCCESS;
}

void programStart()
{
    int size_of_class;
    vector<Student> my_class;

    cout << "What is the size of the class: ";
    cin >> size_of_class;

    for (int i = 0; i < size_of_class; i++) {
        string student_name;
        char student_grade;
        cout << "Name of student " << (i + 1) << ": ";
        cin >> student_name;
        cout << "Grade of student " << (i + 1) << ": ";
        cin >> student_grade;
        if (student_grade >= 'A' || student_grade <= 'F') {
            Student newStudent(student_name, student_grade);
            my_class.push_back(newStudent);
        }
        else {
            while (student_grade < 'A' || student_grade > 'F') {
                cout << "Input the correct grade: ";
                cin >> student_grade;
            }
            Student newStudent(student_name, student_grade);
            my_class.push_back(newStudent);
        }
    }
    //saveClassInfo(my_class);
    printClassInfo(my_class);
}

void saveClassInfo(vector<Student> &newClass)
{
    ofstream newFile;
    newFile.open("class_info.txt");
    if (newFile.fail()) {
        perror("The following error occured");
    }
    else {
        for (int i = 0; i < newClass.size(); i++) {
            newFile << newClass[i].getName << "\t\t"
                << newClass[i].getGrade << endl;
        }
    }
    newFile.close();
    cout << "Done saving.\n";
}

void printClassInfo(vector<Student> &newClass)
{
    for (int i = 0; i < newClass.size(); i++) {
        cout << newClass[i].getName << "\t\t" << newClass[i].getGrade << endl;
    }
}

“学生.h”

#ifndef STUDENT_H
#define STUDENT_H
#include <string>
#include <fstream>
#include <stdio.h>
using namespace std;

class Student
{
public:
    Student(string student_name, char student_grade);

    // Accessors
    string getName();
    char getGrade();

    // Modifiers
    void setName(string student_name);
    void setGrade(char student_grade);

private:
    string _name;
    char _grade;
};

#endif /* STUDENT_H */

学生.cpp

#include "Student.h"

Student::Student(string student_name, char student_grade)
{
    setName(student_name);
    setGrade(student_grade);
}

string Student::getName()
{
    return _name;
}

char Student::getGrade()
{
    return _grade;
}

void Student::setName(string student_name)
{
    _name = student_name;
}

void Student::setGrade(char student_grade)
{
    _grade = student_grade;
}

谁能告诉我我的代码有什么问题?

【问题讨论】:

  • getName 是一个函数,你忘了括号
  • 那是我的愚蠢......现在我添加括号后它正在工作!非常感谢!

标签: c++ c++11 object vector c++14


【解决方案1】:

在:

newFile << newClass[i].getName << "\t\t"

必须是getName()

【讨论】:

  • 我犯了一个愚蠢的错误……这表明我在编程方面缺乏经验。非常感谢!现在修好了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
相关资源
最近更新 更多