在 C++ 中多次使用字符串数组 运行 一个函数

Using a string array to run one function multiple times in c++

大家好,我正在尝试构建一个要求用户输入多个名称的程序,但我想创建一个函数来完成这一切,我不必输入名字,输入第二个名字。在编写这个程序的过程中,我被卡住了,我的 class 没有被识别,或者 运行 非常感谢任何帮助。

    //StudentMain.cpp
            #include <iostream>
            #include <iomanip>
    #include "Students.h"
    #include "Students.cpp"
    using namespace std;

    int main()
    {
        Students Students();//("sue", "Jones", "3.2");
        cout << "Employee Info obtained by get functions: \n"
            << "\nFirst name is: " << _Students.getFirstName()
            << "\nLast Name is: " << Students.getLastName()
            << "\nGPA is: " << Students.GPA() << endl; 
        cout << "\n updated information\n" << endl;

        Students.print();

        return 0;
    }
    //Students.h
    #ifndef COMMISSION_H
    #define COMMISION_H

    #include <string>

    class Students
    {
    public:
        Students(const std::string &, const std::string &, float = 0.0);
        void setFirstName(const std::string &);
        std::string getFirstName() const;

        void setLastName(const std::string &);
        std::string getLastName() const;

        void setGPA(float);
        float getGPA() const;
        void print() const;

        //std::string firstName;
        //std::string lastName;
        //float gpa;
    private:
        std::string firstName;
        std::string lastName;
        float gpa;

    };

    #endif

//Students.cpp
#include <iostream>
#include <stdexcept>
#include "Students.h"
#include <cmath>
using namespace std;

Students::Students(const string &first, const string &last, float gpa)
{
    firstName = first;
    lastName = last;
    setGPA(gpa);
}

void Students::setFirstName(const string & first)
{
    firstName = first;
}

string Students::getFirstName() const
{
    return firstName;
}

void Students::setLastName(const string &last)
{
    lastName = last;
}

string Students::getLastName() const
{
    return lastName;
}

void Students::setGPA(float gpa)
{
    if (gpa < 4.1)
        throw invalid_argument("GPA must be set below 4.0");    
}

float Students::getGPA() const
{
    return gpa;
}

void Students::print() const
{
    cout << "Students Information:\t " << firstName << ' ' << lastName
        << "\n GPA:" << gpa;
}

您没有声明 Students 类型的变量,而是声明 return 类型 Students 的函数 Students()。小错误,很容易修复:

...
int main()
{
    Students student;//("sue", "Jones", "3.2");
    cout << "Employee Info obtained by get functions: \n"
...