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

  • SEARCH
  • Home
  • 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 6374539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:30:03+00:00 2026-05-25T01:30:03+00:00

My variables aren’t producing data like they should be. For whatever reason, my string

  • 0

My variables aren’t producing data like they should be. For whatever reason, my string is empty, yet when I try to execute my function, my character clearly passes the argument “holland” to the name parameter upon construction. Despite this, when I make a call to get_name(), the string is returned as empty. Ontop of that, I have a static integer called “character_count”, which is supposed to initialize at 0 and then increment upon every character creation to pass values to the character’s unique id. Despite this, it’s still not working as it should.

Could someone tell me please what I’m doing wrong?

here’s my source:

#include "character.h"
#include <iostream>


int Character::char_count = 0;

Character::Character()
{
    this->char_id = char_count;
    char_count++;
}

Character::Character(std::string char_name, Race char_race, Gender char_gender)
{
    char_id = char_count;
    char_count++;
    name = char_name;
    race = char_race;
    gender = char_gender;
}

Character::~Character()
{

}

std::string Character::get_name()
{
    return this->name;
}

int Character::get_id()
{
    return this->char_id;
}

Race Character::get_race()
{
    return this->race;
}

Gender Character::get_gender()
{
    return this->gender;
}

Here’s my header file:

#include <iostream>
#ifndef CHARACTER_H
#define CHARACTER_H

enum Race {HUMAN, DARK_ELF};
enum Gender {MALE, FEMALE};
class Character
{
public:
    Character();
    Character(std::string& char_name, Race& char_race, Gender& char_gender);
    ~Character();

    int get_id();
    std::string get_name();
    Race get_race();
    Gender get_gender();

private:
    int char_id;
    static int char_count;
    std::string name;
    Race race;
    Gender gender;
};

#endif // CHARACTER_H

Here’s my main:

#include <QtCore/QCoreApplication>
#include <tinyxml/tinyxml.h>
#include "classowner.h"
#include "character.h"

int main(int argc, char *argv[])
{
    std::string holland_name = "holland";

    Character * holland = new Character(holland_name, HUMAN, MALE);
    delete holland;
    std::cout << "testing, mic...1, 2, 3...testing, testing. Bow, chica wow wowww!!1" << std::endl;
    std::cout << holland->get_id() << std::endl; //outputs as an 8 digit letter, when it should be either 0, or 1.
    std::cout << holland->get_name() << std::endl; //does not output at all.

    return 0;
}
  • 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-05-25T01:30:04+00:00Added an answer on May 25, 2026 at 1:30 am

    That’s because you delete the memory before you’ve finished with it. Try this instead

    Character * holland = new Character(holland_name, HUMAN, MALE);
    std::cout << "testing, mic...1, 2, 3...testing, testing. Bow, chica wow wowww!!1" << std::endl;
    std::cout << holland->get_id() << std::endl;
    std::cout << holland->get_name() << std::endl;
    delete holland;
    

    Only delete after you’ve finished with the object concerned. This kind of mistake invokes Undefined Behaviour, which means all bets are off and anything may happen when you run your code. In some ways you are lucky that there was an obvious error. Your program could have appeared to run successfully, and you would never have known there was a problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.