【问题标题】:using while loop to create objects [closed]使用while循环创建对象[关闭]
【发布时间】:2017-01-18 05:22:50
【问题描述】:

我正在处理的最大问题是在我的 main.cpp 文件中,但是我发布了我的 video.h 文件和 video.cpp 文件以防万一。

我很难理解读取输入然后创建对象的概念。

我有一个 Video 类,其中包含创建对象所需的成员函数。我的课很好。在我的主要内容中,我希望能够在创建对象之前读取输入,而不是创建对象然后初始化变量。

视频.h

#ifndef VIDEO_H
#define VIDEO_H
#include<iostream>
#include<string>
using namespace std;

class Video{

  public:

// constructor works fine
    Video(string name, string link, string comment,
        double rating, int stars);
    void print();




// member variables
  private:

    string m_name;
    string m_link;
    string m_comment;
    double m_rating;
    int m_stars;

};

#endif

视频.cpp

#include<iostream>
#include "video.h"
using namespace std;


Video :: Video (string name, string link, string comment,
                double rating, int stars){


  m_name = name;
  m_link = link;
  m_comment = comment;
  m_rating = rating;
  m_stars = stars;
}

void Video :: print (){

  cout << m_name << ", " << m_link << ", " << m_comment << ", " <<
    m_rating << ", ";

for(int count = 0; count < m_stars; ++count){

  cout << "*";
//cout << endl;
}
cout << endl;
  //<< " , " << m_stars << endl;


}

main.cpp

#include <iostream>
#include <string>
using namespace std;

#include "video.h"

int main()

/*  My program can create objects and initialize the string variables
   if the data is hard-coded when the object is created however I want
   read data using cin and a while loop then create an object.
    Video video1("Title One", "www.youtube.com/one", 
                           "Comment ONE", 1.1, 1);
    Video video2("Title Two", "www.youtube.com/two", 
                           "Comment TWO", 2.2, 2);

    video1.print();
    video2.print();
*/
{

  while (Video >> cin) { //I want to use a while loop here
    /* The program needs to read these getlines 
       and other variables in order to store names or comments 
       that have spaces */


    getline(cin, name); // user enters the name then presses "enter"
    getline(cin, link); // user enters the link then presses "enter"
    getline(cin, comment); // user enters the comment then presses "enter"
    double rating; // user enters the rating then presses "enter"
    int stars; // user enters the amount of stars then presses "enter"
    Video video_one; /* Once the user enters data for all five members a new object is created */
  }



}

【问题讨论】:

  • 呃,Video 是一种运行时类型,所以不可能编译。您需要在重载 &gt;&gt; 运算符的类型上创建一个朋友。

标签: c++ class object while-loop cin


【解决方案1】:

注意: 你不能这样做:

while(video>>cin){...}

没有友元函数重载提取>>运算符。做你想做的事,在你的 Video 类中重载提取运算符(>>):

friend istream &operator>>(istream &input, Video &V)
 {
  input>> V.m_name>>V.m_link>>V.m_comment...;
  return input;
 }

并像这样使用它:

 while(cin>>video){...}   

同样要先读取输入然后实例化对象,您需要一个容器(向量或数组)来保存所有创建的新视频对象。您知道将预先输入的视频数量吗?原因是数组被分配了固定的内存量,如果输入的新视频对象的数据总量超过数组的内存量,就会导致分段错误。有一些方法可以使您的数组动态化,但使用 Vector 类更容易,因为随着输入更多视频标题,它会自动增加容器大小(获得更多内存)。我使用了一个名为 listofVideos 的向量。在 while 循环结束时,您使用迭代器遍历向量。

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

typedef struct Video_t{ //I used a struct in this case instead of the class 

    string m_name;
    string m_link;
    string m_comment;
    int m_stars;
}Video_t;
//#include "video.h"

int main()
{
  string buffer;
  string name;
  string link;
  string comment;
  double rating;
  double stars;
    vector<Video_t> listofVideos; //I used a vector in this case to store all the videos. 

  while (getline(cin,buffer)&&buffer != "end") { //I used a control word to signal end of inputs.
    if(buffer.empty());
    istringstream is(buffer);
    if(is>>name>>link>>comment>>stars)
    {
      Video_t vid;
      vid.m_name = name;
      vid.m_link=link;
      vid.m_comment = comment;
      vid.m_stars = stars;
      listofVideos.push_back(vid);
    }
  }  

  vector<Video_t>::iterator it;
 for(it=listofVideos.begin();it!=listofVideos.end();++it)
  cout<<(*it).m_name<<" "<<(*it).m_link<<" "<<(*it).m_stars<<endl;

  return 0;
  }

【讨论】:

  • 我不打算存储我想连续创建对象并拥有指向对象的指针数组的对象。这样我就可以随意排序了。
  • 您不断地创建新对象,但除非您将新创建的结构存储到容器中,否则结构中捕获的信息将在 while 循环内的下一次迭代中被覆盖。您还可以更改矢量参数以存储视频类型指针 (Video*) 而不是视频。然后,您还需要使用 在动态存储中实例化您的 Video 对象。这是为了防止您的对象超出范围。
【解决方案2】:

我会做的

    while(true)
    {
      getline(cin, name); // user enters the name then presses "enter"
      getline(cin, link); // user enters the link then presses "enter"
      getline(cin, comment); // user enters the comment then presses "enter"
      double rating; // user enters the rating then presses "enter"
      cin >> rating;
      int stars; // user enters the amount of stars then presses "enter"
      cin >> stars;
      Video video_one(name,link,comment,rating,stars); /* Once the user enters data for all five members a new object is created */
    }

【讨论】:

    猜你喜欢
    • 2015-04-15
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 2014-05-29
    相关资源
    最近更新 更多