简体   繁体   English

Stl struct作为类成员的列表

[英]Stl list of struct as a class member

For my homework I have to do a class Game in C++ that has name, size, and a list of updates that contain date of update and some information about that update. 对于我的作业,我必须使用C ++编写一个类游戏,该类具有名称,大小和包含更新日期以及有关该更新的一些信息的更新列表。 (for example : 22.05.2018 Bug fixed at quest 3). (例如:22.05.2018已在任务3中修复错误)。 Here is what i tried, but it doesn't work. 这是我尝试过的方法,但是不起作用。 Game.h: Game.h:

class Game{
 public:
  struct update{
     string date;
     string info;
 };
  string name;
  double size;
  list<update>l;
  Game(string name, double size, list<update>l);
  virtual ~Game();
 };

and in Game.cpp: 并在Game.cpp中:

Game::Game(string name, double size, list<update>l){
    this->name=name;
    this->size=size;
    this->l=l;
 }

In int main I created a list: 在int main中,我创建了一个列表:

int main()
{
  list<update>mylist;
  update u1,u2,u3;
  u1.date="20.05.2018";
  u1.info="Mission 3 bug fixed";
  u2.date="25.05.2018";
  u2.info="New quest";
  mylist.push_back(u1);
  mylist.push_back(u2);
  Game g("Gta5",60.0,mylist);
  return 0;
 }

i get this error: 我收到此错误:

no matching function for call to 'Game::Game(const char [4], double, std::__cxx11::list<update>&)'|

Or if you want to keep the nested class update : 或者,如果您想保持嵌套类的update

#include <string>
#include <list>


using std::string;
using std::list;

class Game{
public:
    struct update{
        string date;
        string info;
    };
    string name;
    double size;
    list<update>l;
    Game(string name, double size, list<update>l);
    virtual ~Game() {}
};


Game::Game(string name, double size, list<update>l){
    this->name=name;
    this->size=size;
    this->l=l;
}


int main()
{
    list<Game::update> mylist; // use Game::update to access nested class
    Game::update u1,u2,u3;
    u1.date="20.05.2018";
    u1.info="Mission 3 bug fixed";
    u2.date="25.05.2018";
    u2.info="New quest";
    mylist.push_back(u1);
    mylist.push_back(u2);
    Game g("Gta5",60.0,mylist);
    return 0;
}

Try this, 尝试这个,

#include<bits/stdc++.h>
using namespace std;

struct update
{
     string date;
     string info;

     update(string date,string info){
         this->date=date;
         this->info=info;
     }
};

class Game{
 public:

  string name;
  double size;
  list<update>l;
  Game(string name, double size, list<update>l);
 // virtual ~Game();
 };

 Game::Game(string name, double size, list<update>l){
    this->name=name;
    this->size=size;
    this->l=l;
 }

int main(){

 list<update> l,m;
 l.push_front(update("10/10/2017","some bug fixed"));
 double size=100;
 string name="Game1";
 Game obj(name,size,l);

 cout<<obj.name<<" "<<obj.size<<" "<<endl;
 m=obj.l;
 list<update>::iterator i;

 for(i=m.begin();i!=m.end();i++){
    update structObj=*i;
    cout<<structObj.date<<" "<<structObj.info<<endl;   
 }     

 return 0;   
} 

Output 产量

Game1 100 游戏1100
10/10/2017 some bug fixed 10/10/2017修复了一些错误

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

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