Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:44:33+00:00 2026-06-17T22:44:33+00:00

I have a few simple classes and I can’t get them to work. TL;DR

  • 0

I have a few simple classes and I can’t get them to work.

TL;DR I have a “Player” instance, after I set some data to the instance, I can get it back. If I push the instance to std::vector Players; if I have Players.at(0).getName() it returns “”. The data is not there! Is gone. (Debugging the application I see “_name” set in “vPlayer” and in “Players” I see an element, with “_name” = “”)

Here is the code:

//Player.h
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>

class Player
{
public:
    Player();
    Player(const Player &Player);
    Player& operator=(const Player &Player);
    std::string getName();
    bool        setName(const std::string &name);
    bool        nameValid(const std::string &name);

private:
    std::string _name;
};



#endif



//Player.cpp

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

Player::Player()
{

}
Player::Player(const Player &Player)
{

}
Player& Player::operator=(const Player &Player) {
    return *this;
}

std::string Player::getName()
{
    return this->_name;
}

bool Player::setName(const std::string &name)
{
    if ( ! this->nameValid(name) )
    {
        return false;
    }

    this->_name = name;
    return true;
}

bool Player::nameValid(const std::string &name)
{
    return name.empty() == false;
}




//Map.h
#ifndef MAP_H
#define MAP_H

#define MAP_X 40
#define MAP_Y 40

#include "Player.h"
#include "Point.h"
#include <vector>

class Map
{
public:
    Map();
    bool movePlayer(Player &Player, Point &Point);
    std::vector<Player> getPlayers();
private:

};

#endif //MAP_H



//Map.cpp

#include "Map.h"
#include "Player.h"
#include "Point.h"
#include <iostream>
#include <string>

using namespace std;

Map::Map()
{

}

bool Map::movePlayer(Player &Player, Point &Point)
{
    return true;
}
std::vector<Player> Map::getPlayers()
{
    Player vPlayer;
    vPlayer.setName(std::string("test"));
    std::vector<Player> Players;

    Players.push_back(vPlayer);

    return Players;
}

in main:

  std::vector<Player> Players = vMap.getPlayers();
  cout<<"Test:"<<Players.at(0).getName()<<endl;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:44:34+00:00Added an answer on June 17, 2026 at 10:44 pm

    Your vector will contain copies of the elements you add to it. These copies will be added using Player::Player(const Player&) constructor.

    This constructor (in your implementation) doesn’t set any value for the name.

    Solutions:

    • set the name in the copied object:

      Player::Player(const Player &Player)
      : _name(Player._name)
      {
      }

    (The same is true for your assignment operator)

    • Remove the copy and assignment functionality and rely on the default. Because the name is a std::string, it will get a copy of the source player name by default.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty simple question. I have a few ASP RequiredFieldValdators checking some text boxes. Out
I have set up a few models in a simple django project. When I
I have a simple parsing program that takes a few files and combines them.
I have 2 classes. One (A) collects some data and the other (B) sends
If I have a Class instance at runtime, can I get its byte[] representation?
I have a few simple scripts that are intended to daisy chain together to
I have written a small makefile for a few simple C programs that compiles
I have a simple html form with few fields in it. I wanted to
I have a simple aspx page with a few TextBoxes and a submit button.
I have a simple solution in Visual Studio 2010 containing a few static libraries,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.