大家好,我正在尝试构建一个要求用户输入多个名称的程序,但我想创建一个函数来完成这一切,我不必输入名字,输入第二个名字。在编写这个程序的过程中,我被卡住了,我的类(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
的变量,而是声明返回类型为 Students
的函数 Students()
。小错误,很容易修复:
...
int main()
{
Students student;//("sue", "Jones", "3.2");
cout << "Employee Info obtained by get functions: \n"
...
关于c++ - 在 C++ 中使用字符串数组多次运行一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30403026/