首页 > 解决方案 > 将对象推入矢量以手动操作面向对象的程序

问题描述

我有一门课程有一个项目,但我被困住了,迫切需要帮助。我正在为学生招生创建一个面向对象的程序。

我必须基本上操纵变量,然后在没有库的情况下以特定方式手动排列。我一直在尝试将成员变量添加到向量中。

我一直在尝试在类中定义向量并使用 get 和 set 函数将对象推送到它似乎不起作用,我也尝试过朋友。如果有人很好地指出我正确的方向,我将不胜感激。我正在使用旧版本的 C++

setpopulate(student& s1, vector<student> temp) //friend
{
  temp.push_back(s1.FirstName)
}
  string line;
  student s1;
  ifstream domesticFile("domestic-stu.txt");
  if(!domesticFile.is_open()) {
    cout << "Unable to open file domestic-stu.txt" << endl;
    return -1;
  }
  getline(domesticFile, line);
  cout << "File format: " << line << endl;

  int stu_count = 1;
  while( getline(domesticFile, line) ) {

    istringstream ss(line);

    string firstName, lastName, province, s_cgpa, s_researchScore;
    float cgpa;
    int researchScore;

    getline(ss, firstName, ',');
    getline(ss, lastName, ',');
    getline(ss, province, ',');
    getline(ss, s_cgpa, ',');

    cgpa = atof(s_cgpa.c_str());
    getline(ss, s_researchScore, ',');
    researchScore = atoi(s_researchScore.c_str());

    s1.setFirstName(firstName);
    s1.setLastName(lastName);
    s2.setProvince(province);
    s1.setcgpa(cgpa);
    s1.setresearchscore(researchScore);
    //print the student info to the screen
    cout << "Domestic student " << stu_count << " " << firstName << " " 
     << lastName << " from " << province << " province has cgpa of "
     << cgpa << ", and research score of " << researchScore << endl;
    stu_count++;
  }
  domesticFile.close();

#ifndef STUDENT_HPP_
#define STUDENT_HPP_
using namespace std;
#include<string>
#include<cstdlib>
#include<vector>
#include<iomanip>


class student
{ 
 public:

 student();
student(string newFirstName,string newLastName,float newcgpa,int newresearchscore,vector<student> x);


/*student(const student& copy)
{
  string newFirstName,string newLastName,float newcgpa,int newresearchscore)
};*/

 string getFirstName() const;
 void setFirstName(string newFirstName) ;

 string getLastName()const ;
 void setLastName(string newLastName);

 
 float getcgpa() const;
 void setcgpa(float newcgpa);

 int getresearchscore() const;
 void setresearchscore(int newresearchscore);

 friend void setpopulate(student& s1, string vector<student> temp);
 
 void setvector(string vector<student> x);
 string getvector() const;
 

friend std::ostream& operator<<(std::ostream& output, const student& obj);

friend string compareFirstName(const student& s1, const student& s2);
friend string compareLastName(const student& s1, const student& s2);
friend float comparecgpa(const student& s1, const student& s2);
friend int compareresearchscore(const student& s1, const student& s2);

friend bool operator ==(const student& s1, const student& s2);
friend bool operator >(const student& s1, const student& s2);
friend bool operator <(const student& s1, const student& s2);


 private:
 string FirstName;
 string LastName;
 float cgpa;
 int researchscore;
 vector<student> temp;

 

};
#endif

标签: c++classoopvectormethods

解决方案


推荐阅读