简体   繁体   English

打印出向量中的输入,并将 function 包含在 main 中。 c++

[英]Printing out the input in a vector, and include the function into main. c++

Anyone got any tips for me?有人对我有任何提示吗? I need to print out the vector, but i cant get the function included into main.我需要打印出矢量,但我无法将 function 包含在 main.js 中。 The relevant part is the "void print" and "print(klasse)" in main.相关部分是 main 中的“void print”和“print(klasse)”。 The criteria for the assignment is:分配的标准是:

  1. Asks for the name of a student询问学生姓名

  2. asks for the name of a course and the grade the student got询问课程名称和学生的成绩

  3. Does this until course name is "stop"这样做直到课程名称为“停止”

  4. Then asks for a new student until the name is "stop"然后要求一个新学生,直到名字是“停止”

  5. Then prints out the grades gathered for each student然后打印出为每个学生收集的成绩

     #include <iostream> #include <string> #include <vector> #include <list> #include <algorithm> #include <iterator> using namespace std; class student_i { public: string name; string course; string grade; }; void print(std::vector<student_i> const &klasse) { std::copy(klasse.begin(), klasse.end(), std::ostream_iterator<int>(std::cout, " ")); } int main() { student_i klasse; std::vector<student_i> studenter; for (int i = 0; i < 64; i++) { std::cout << "Enter student name: " << std::endl; getline(std::cin, klasse.name); studenter.push_back(klasse); if ( klasse.name == "stop") { //Why doesnt this function work in main? print(klasse); return 0; } for (int i = 0; i < 64; i++) { std::cout << "Enter student course: " << std::endl; getline(std::cin, klasse.course); if (klasse.course == "stop") { break; } std::cout << "Enter student grade: " << std::endl; getline(std::cin, klasse.grade); } } return 0;

    } }

Your issue here is that you ask to "copy" your std::vector<student_i>::iterator into a std::ostream_iterator<int> instead of an std::ostream_iterator<student_i> (which are very much not the same).您的问题是您要求将std::vector<student_i>::iterator “复制”到std::ostream_iterator<int>而不是std::ostream_iterator<student_i> (它们非常不一样) . You should change that.你应该改变它。

However the compiler will complain that you then need to define the operator<< on the type student_i .但是,编译器会抱怨您需要在类型student_i上定义operator<< Here is how I would do it (you should get familiar with this definition of operator<<, it is encountered quite frequently):下面是我的做法(你应该熟悉这个 operator<< 的定义,它经常遇到):

std::ostream& operator<<(std::ostream& stream, const student_i& stud) {
  stream << stud.name << " " << stud.course << " " << std.grad;
  return stream;
}

Quick note: you made all your attributes public in the class student_i so this will work fine, but this is often not the case (attributes are generally private).快速说明:您在 class student_i中公开了所有属性,所以这可以正常工作,但通常情况并非如此(属性通常是私有的)。 In such case, it is common practice to declare the operator<< friend to the class, so you can still access the attributes in its body:在这种情况下,通常的做法是将operator<<声明为 class 的朋友,因此您仍然可以访问其主体中的属性:

class student_i {
  friend std::ostream& operator<<(std::ostream&, const student_i&);
  private:
    std::string name; 
    ...
      

You can now write something like你现在可以写类似

student_i student;
student.name = "danlon"; 
student.course = "CS"; 
student.grad = "A";
std::cout << student << std::endl;

Once this function is defined, I think your print function should work.一旦定义了这个 function,我认为您的打印 function 应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM