简体   繁体   English

我应该如何将char *转换为Class类型

[英]How am I supposed to cast a char* into a Class type

I'm trying to cast a char* into a Class of my own making and can't seem to get it to work without an error telling me that I simply can't cast a char* to type Player (my class name). 我正在尝试将char *强制转换为我自己创建的Class,并且似乎没有错误告诉我我根本无法将char*转换为Player (我的类名)类型,因此无法使其正常工作。 The character pointer comes from a pointer array, char *names[i], i = any index, names[i] = one of the names , ie Austin or Kyle. 字符指针来自指针数组char *names[i], i = any index, names[i] = one of the names ,即Austin或Kyle。 With this I think i is a char* , but I could be wrong. 我认为i是一个char* ,但是我可能是错的。

What I am trying to achieve over all is iterating through a vector of type Player and push_back each char* i to the vector while at the same time casting i to type Player. 我试图实现的目标是遍历Player类型的向量,并将每个char* i push_back回该向量,同时将i强制转换为Player类型。 I sure hope this was specific enough I'll clarify if asked. 我当然希望这足够具体,如果需要我会澄清。

/*
Inside of my Blakjack constructor
*/
for (int i = 0; i < numPlayers; i++) {
    //m_players is the vector of type Player
    //Player *p = new Player(names[i]);
    m_players.push_back(names[i]);
  }

/*
The Player.h File
*/
#include <vector>
class Player {
    public:
        Player(const char *name);

    private:
        int m_funds;
        char *m_name;
};

/*
The Player.cpp File
*/
#include "Player.h"
#include <iostream>
Player::Player(const char *name){
    m_name = name; //I think this will work but it's hard to tell with my Blackjack
                     constructor still giving errors
    m_funds = 100;
}

It sounds like you want to create a Player object for each name in an array of character strings. 听起来您想为字符串数组中的每个名称创建一个Player对象。

If that's the case, you don't really want to cast a char * to a Player , you want to construct a Player for each char * in your array. 在这种情况下,您实际上并不想将char *转换为Player ,而是要为数组中的每个char *构造一个Player Something like: 就像是:

for (int i = 0; i < nameCount; i++)
{
    Player *p = new Player(names[i]);

    // Do something with p
    v.push_back(p); // ?? or something
}

This assumes your Player class has a constructor that looks like this: 假设您的Player类具有一个类似如下的构造函数:

Player::Player(const char *name)

and v is a std::vector<Player> vstd::vector<Player>

And if your Player class has this constructor, the language actually allows the char * to be implicitly converted into a Player (which may be what you're thinking of when you're trying to cast the char* into a Player): 如果您的Player类具有此构造函数,则该语言实际上允许将char *隐式转换为Player(这可能是您在尝试将char *转换为Player时所考虑的):

for (int i = 0; i < nameCount; i++)
{
    v.push_back(names[i]); // adds a new Player, using the name stored in names[i]
}



EDIT: Your Player class also needs a default constructor: 编辑:您的Player类还需要一个默认的构造函数:

class Player {

    public:
        Player(const char *name);
        Player() {};  // should probably initialize m_name and m_funds in here (set to 0 or something) just to be pedantic

    private:
        int m_funds;
        char *m_name;
};

This is needed in order to put your objects into a container such as std::vector. 为了将您的对象放入容器,例如std :: vector,这是必需的。

Your Player constructor receives a const char* while your m_name member is char*. 当m_name成员为char *时,Player构造函数将接收const char *。 Assigning const char* to char* will not work. 将const char *分配给char *将不起作用。

Make your constructor Player::Player(char* name). 使您的构造函数成为Player :: Player(char * name)。

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

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