【问题标题】:How do I added an object to a vector<object> that's inside another class?如何将对象添加到另一个类中的 vector<object> ?
【发布时间】:2010-11-15 10:04:11
【问题描述】:
#include "player.h"

class team
{
public:
 team();
 void addPlayer(player);
 void deletePlayer(int);
 double getTotalCost();
 int getPlayerCount();
 double inBank();
 string toString();

private:
 player a;
 int playerId;
 double bank;
 int i;
};


#include "../../std_lib_facilities.h"
#include "team.h"


team::team()
{
 vector <player> a;
 player a;
}

team::addPlayer(player)
{
 a.push_back(a);
}

如果需要更多信息,请询问。提前感谢您的帮助。

【问题讨论】:

  • 既然你不喜欢浪费时间正确格式化你的代码,我不明白为什么其他人应该浪费他们的时间来解码它。
  • 问题的标题是标题,而不是问题。它不应包含您的课程名称等详细信息。
  • 是的……不。在你对这个问题付出一些努力之前,我不会回答这个问题。
  • 投票结束,因为过于本地化。我们不是您的个人开发者……是吗??

标签: c++ class vector


【解决方案1】:

我想这就是你的意思:

#include "player.h"
#include  <vector>

class team
{
public:
 team();
 void addPlayer(player);
 void deletePlayer(int);
 double getTotalCost();
 int getPlayerCount();
 double inBank();
 string toString();

private:
 vector<player> a;
 int playerId;
 double bank;
 int i;
};

#include "../../std_lib_facilities.h"
#include "team.h"


team::team()
{
}

team::addPlayer(player p)
{
 a.push_back(p);
}

【讨论】:

  • 最好写成 team::addPlayer(const player &p) {...}
  • 如果addPlayer 将参数作为对 const (void team::addPlayer(const player &amp; p)) 的引用会更好。
  • 这很好,但由于某种原因,现在它给了我这些错误
  • 错误 4 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int x:\c++\assignment 2\assignment 2\team.cpp 10 team
  • 错误 5 错误 C2556: 'int team::addPlayer(player)' : 重载函数仅因返回类型与 'void team::addPlayer(const player)' x:\c++\assignment 2 不同\assignment 2\team.cpp 10 团队
【解决方案2】:

你应该让你的向量变量成为你的类的成员,或者在堆上创建它并保留指向它的指针。现在您正在 teem 构造函数中的堆栈上创建向量。构造函数完成后,它将被删除。 此外,您不能将名称 a 用于播放器和矢量。我建议你先阅读一些 C++ 书籍。

【讨论】:

    【解决方案3】:

    这有很多错误我不知道从哪里开始,你在这里声明一个单一的玩家:

    private:
        player a; // why not just call every variable in your program "a"?
    

    然后在你的团队构造器中:

    team::team()
    {
        vector<player> a;  // a vector that will be destroyed on exit from constructor
        player a; // another single player, but you've just defined 'a' so you should get a compiler error along the lines of redefinition.
    }
    

    我怀疑你想要类似的东西:

    #include <vector>
    #include <string>
    
    #include "player.h"
    
    class team
    {
    private:
        std::vector<player> m_Players;   // players
    
    public:
        void addPlayer(const player& _player) { m_Players.push_back(_player); }
    }; // eo class team
    

    【讨论】:

      【解决方案4】:

      你需要什么?在您的班级团队中存储球员?

      #include <iostream>
      #include <vector>
      using namespace std;
      
      class Player
      {
      public:
          Player(int id)  {   m_id = id; }
          int GetId() {return m_id;   }
      private:
          int m_id;
      };
      
      class team
      {
      public:
          team(){};
          void AddPlayer(Player p) {m_arr.push_back(p);}
          size_t Size(){  return m_arr.size();}
          Player GetPlayer(size_t index) {return m_arr[index];}
      private:
          vector<Player> m_arr;
      };
      
      void main()
      {
          team t;
          for (int i =0; i < 10; ++i)
          {
              t.AddPlayer(Player(i));
          }
      
          for (int i =0; i < t.Size();++i)
          {
              cout << "Player [" << i + 1 <<  "] with id: " << t.GetPlayer(i).GetId() << endl;
          }
      }
      

      【讨论】:

      • 详细说明通过 const 引用传递 Player 的更好方法...如果 Player 对象很大,则为指针
      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 2017-12-20
      • 1970-01-01
      • 2017-05-07
      相关资源
      最近更新 更多