Accessing Functions from another file

Hello everyone. I am writing a program that requires pointers, constructors/deconstructors, functions, and classes. I am at the beginning stages of this project and am asking for a little advice. Since I am at the beginning, I am only testing that the functions that were initialized in my header file, then defined in a separate.cpp file, being reached in my main.cpp. I am not totally sure on how to this. I somehow was able to get the first one, but trying to follow what I managed to do the first time is seeming a bit more over my head. It has been a while since I have coded and I am a bit rusty. Here is a snippet of what I am doing.

Header File (Character.h):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  class Character
{
private:
	char m_sName[64];
	int m_iClass, m_iAlignment, m_iHitPoints;
	int m_iCharTraits[6];

public:
	Character();
	~Character();
	Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr);
	void getName(char *name);
	void setName(char *name);
};


.cpp File with defined functions (Character.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include "Character.h"

using namespace std;
	/********************************
	*  Define Character Constructor *
	*********************************/
	Character::Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr)
	{
		strcpy(m_sName, name);
		m_iClass = cl;
		m_iAlignment = al;
		m_iHitPoints = hp;
		m_iCharTraits[0] = str;
		m_iCharTraits[1] = dex;
		m_iCharTraits[2] = con;
		m_iCharTraits[3] = itl;
		m_iCharTraits[4] = wis;
		m_iCharTraits[5] = chr;

		cout << "I have reached the Define Character Constructor Function.\n";
	}
	/********************************
	* Character Default Constructor *
	*********************************/
	Character::Character()
	{
		cout << "I have reached the Character Default Constructor Function.\n";
	}
	/********************************
	*   Character Deconstructor     *
	*********************************/
	Character::~Character()
	{
		cout << "I have reached the Character Deconstructor Function.\n";
	}
	/********************************
	*       Get Player Name         *
	*********************************/
	void Character::getName(char *name)
	{
		cout << "I have reached the Get Player Name Function.\n";

	}
	/********************************
	*       Set Player Name         *
	*********************************/
	void Character::setName(char *name) 
	{

		cout << "I have reached the Set Player Name Function.\n";
	}


Main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "Character.h"
#include <string>

using namespace std;

int main()
{
	Character *character = new Character();
	Character *name = new getName();

	return 0;
}

Obviously line 10 in main.cpp is wrong, but again, I am not sure how to reach the function in my main.cpp. Thanks in advance for any help.
Last edited on
I'm not sure what you're asking. It looks like you got the right idea.

Example.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once

#include <iostream>

using namespace std;

class Example
{
private:
   int i;
public:
   Example(int);
   void print();
}


Example.cpp
1
2
3
4
#include "Example.h"

Example::Example(int val) : i(val) {}
void Example::print() { cout << i << endl; }


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

#include "Example.h"

using namespace std;

int main()
{
   Example ex(1);
   ex.print();
   return 0;
}
Last edited on
More or less, I am trying to access my functions from the Character.h and Character.cpp files in my main.cpp file. Line 9 in main.cpp will print line 29 in Character.cpp. Line 10 in main.cpp doesn't print anything but basically crashes the program. I am not even exactly sure what line 10 in main.cpp is doing at all, just makes the program act crazy.
I'm not entirely sure that the design makes sense, but still, I put all the code in a single file and fixed main(). Also added const and strcpy() in a couple of places.

By all means keep the separate files. I put them together for my own convenience.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cstring>

class Character
{
private:
    char m_sName[64];
    int m_iClass, m_iAlignment, m_iHitPoints;
    int m_iCharTraits[6];

public:
    Character();
    ~Character();
    Character(const char *name, int cl, int al, int hp, 
        int str, int dex, int con, int itl, int wis, int chr);
    void getName(char *name) const;
    void setName(const char *name);
};


int main()
{
    Character *character = new Character;
    
    character->setName("Fred");
    
    char name[100] = "";
    
    character->getName(name);
    
    delete character;
    
    std::cout << "name = " << name << '\n';
    
    //-----------------
    
    Character ch("Suzy", 1, 2, 3, 5, 6, 6, 7, 8, 9);
        
    ch.getName(name);
    
    std::cout << "name = " << name << '\n';
    
}

using namespace std;

/********************************
*  Define Character Constructor *
*********************************/
Character::Character(const char *name, int cl, int al, int hp, 
        int str, int dex, int con, int itl, int wis, int chr)
{
    strcpy(m_sName, name);
    m_iClass         = cl;
    m_iAlignment     = al;
    m_iHitPoints     = hp;
    m_iCharTraits[0] = str;
    m_iCharTraits[1] = dex;
    m_iCharTraits[2] = con;
    m_iCharTraits[3] = itl;
    m_iCharTraits[4] = wis;
    m_iCharTraits[5] = chr;

    cout << "I have reached the Define Character Constructor Function.\n";
}
/********************************
* Character Default Constructor *
*********************************/
Character::Character()
{
    cout << "I have reached the Character Default Constructor Function.\n";
}
/********************************
*   Character Deconstructor     *
*********************************/
Character::~Character()
{
    cout << "I have reached the Character Deconstructor Function.\n";
}
/********************************
*       Get Player Name         *
*********************************/
void Character::getName(char *name) const
{
    strcpy(name, m_sName);
    cout << "I have reached the Get Player Name Function.\n";

}
/********************************
*       Set Player Name         *
*********************************/
void Character::setName(const char *name) 
{
    strcpy(m_sName, name);
    cout << "I have reached the Set Player Name Function.\n";
}
Last edited on
So, I did have those strcpy statements within my code as well, I just commented them out while I was just trying to test that I can actually reach the functions from my main.cpp. I run your version and it hits everything! Let me see if I can break down what your main is doing.
Character *character = new Character(); creating a new Character from the Character class
character->setName("Fred"); passing the name "Fred" into the new Character
char name[100] = ""; creating a character array called "name"
character->getName(name); putting the name "Fred" in the character array for storage
delete character; deleting the no longer needed pointer to character as the name is being stored in the character array
std::cout << "name = " << name << '\n'; prints "Fred"
Character ch("Suzy", 1, 2, 3, 5, 6, 6, 7, 8, 9); kind of lost me here. Can I assume you are testing the Define Character Constructor function?
I will load your code into my compiler and step through with the debugger so I can see exactly what is happening at each line.
Your descriptions are close enough.

There's one thing I should change in the code:
Line 23
 
    Character *character = new Character();

would be better as
 
    Character *character = new Character;

The parentheses are not necessary and can cause problems.

Consider this. In the second case, I used the constructor which takes lots of parameters:
 
    Character ch("Suzy", 1, 2, 3, 5, 6, 6, 7, 8, 9);

But what if I wanted to use the default constructor. Should it be changed to this?
 
    Character ch();


That's a problem. It doesn't declare a variable named ch. It actually declares a function named ch() which has a return type of Character

The proper variable declaration is just
 
    Character ch;

Ok, going off the previous reply I have come up with this, and it seems to work. Not sure if this is the best way to code it though. Is there a better way to write these test statements?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include "Character.h"
#include <string>

using namespace std;

int main()
{
	Character *character = new Character();
	character -> setName("Elivs");
	char name[64] = "";
	character -> getName(name);

	int cl1 = NULL;
	character -> setClass(1);
	character -> getClass(cl1);

	int align = NULL;
	character -> setAlignment(2);
	character -> getAlignment(align);

	int hit = NULL;
	character -> setHitPoints(3);
	character -> getHitPoints(hit);

	int stren[100];
	character -> setStrength(4);
	character -> getStrength(stren);

	int dex[100];
	character -> setDexterity(5);
	character -> getDexterity(dex);

	int cons[100];
	character -> setConstitution(6);
	character -> getConstitution(cons);

	int tell[100];
	character -> setIntelligence(7);
	character -> getIntelligence(tell);

	int wis[100];
	character -> setWisdom(8);
	character -> getWisdom(wis);

	int cha[100];
	character -> setCharisma(9);
	character -> getCharisma(cha);

	character -> printAll();
	delete character;

	return 0;
}


and the output looks like this:
I have reached the Character Default Constructor Function.
I have reached the Set Player Name Function.
I have reached the Get Player Name Function.
I have reached the Set Player Class Function.
I have reached the Get Player Class Function.
I have reached the Set Player Alignment Function.
I have reached the Get Player Alignment Function.
I have reached the Set Player Hit Points Function.
I have reached the Get Player Hit Points Function.
I have reached the Set Player Strength Function.
I have reached the Get Player Strength Function.
I have reached the Set Player Dexterity Function.
I have reached the Get Player Dexterity Function.
I have reached the Set Player Constitution Function.
I have reached the Get Player Constitution Function.
I have reached the Set Player Intelligence Function.
I have reached the Get Player Intelligence Function.
I have reached the Set Player Wisdom Function.
I have reached the Get Player Wisdom Function.
I have reached the Set Player Charisma Function.
I have reached the Get Player Charisma Function.
Name: Elivs  Class: 1  Alignment: 2  Hit Points: 3
  Strength: 4  Dexterity: 5  Constitution: 6
  Intelligence: 7  Wisdom: 8  Charisma: 9
I have reached the Character Deconstructor Function.
Press any key to continue . . .
I think you missed my previous reply.
 
Character *character = new Character();

would be better as
 
Character *character = new Character;

The parentheses are not necessary.


..........................

However, moving on. Here's where I'm not sure you have a good design.
1
2
3
    int cl1 = NULL;
    character->setClass(1);
    character->getClass(cl1);

As an aside, NULL is normally used for pointers (though superseded by nullptr). For an integer, just use 0 instead.
 
    int cl1 = 0;


But my main point is this. Wouldn't it be better to write it like this:
1
2
    character->setClass(1);
    int cl1 = character->getClass();

The same suggestion would apply to all the getter functions.

Even the one which gets the name. This
1
2
    char name[64] = "";
    character->getName(name);

might become
 
    string name = character->getName();


It all depends - are you working to some specific rules for an assignment? Or is this just something of your own?
There are specific rules to the assignment. The instructor doesn't really like using strings but character arrays instead. I only set the array
char name[64] = "";
because that's the default size of the array the instructor set. I'm new to pointers, and rusty on the more basic syntax so I am basically brushing up on the old while learning some new stuff. Judging by the lectures from class, there will be a significant amount of pointers used throughout the course, which is Data Structures. Each assignment piggybacks off each other, so an effective and efficient design is necessary for future modifications to the program.

Could you explain why the parentheses are not necessary? I thought I was calling the Character(); function. I did change it as you suggested and it does still work just fine, but I am unsure the reasoning behind it. Thanks.
Here is the actual assignment so you can see what I am up against:

In role-playing adventure games such as Dungeons and Dragons each person creates a player character which they play in the game. In this program a C++ class will be created which can be used to define a player character according to the rules of Dungeons and Dragons. This programming assignment requires an understanding of pointers, and C++ functions and classes. Simply put, a class is a data structure which contains data and the functions to work on that data. A pointer is a variable that holds the memory address of another variable, data structure, or class. This program will provide the opportunity to explore pointers, functions, and classes and learn how to use them.
1.Requirements
a. The student shall define, develop, document, prototype, test, and modify as required the software system.
b. This software system shall define a player character class called Character (file names shall be Character.h and Character.cpp) which contains all information to define and maintain a player character for a Dungeons and Dragons style role playing adventure game.
i. The Character class shall contain private variables as described below:
ii. A character array, called m_sName capable of holding strings of up to 64 characters.
iii. Three ints called m_iClass, m_iAlignment, and m_iHitPoints.
iv. An array of six integers called m_iCharTraits used to represent the character qualities of: Strength, Dexterity, Constitution, Intelligence, Wisdom, and Charisma
v. The Character class shall contain public functions as described below.
vi. Character(), ~Character() A default constructor and destructor.
vii. Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr) A parameterized constructor which shall take as arguments a character array giving the name and integer arguments specifying the class, alignment, hit points, strength, dexterity, constitution, intelligence, wisdom, and charisma of the character.
c. Get/Set functions There will be a getVariable() and a setVaraible() designed to return the value stored in or set the value stored in a character's data. Specifically these functions shall be prototyped as:
i.void getName(char *name), void setName(char *name) - Get and set the player name.
ii. void getClass(int& cl), void setClass(int cl) - Get and set the player class. The get function shall be a reference function.
iii. void getAlignment(int& al), void setAlignment(int al) - Get and set the player alignment value. The get function shall be a reference function.
iv. void getHitPoints(int& hp), void setHitPoints(int hp) - Get and set the player hit points. The get function shall be a reference function.
v. Six pairs of functions to get and set the various character traits stored in the m_iCharTraits array. All of the get functions shall be pointer functions. The functions are:
void getStrength(int *str), void setStrength(int str) - Get and set the player Strength score (m_iCharTraits[0]).
void getDexterity(int *dex), void setDexterity(int dex) - Get and set the player Dexterity score (m_iCharTraits[1]).
void getConstitution(int *cn), void setConstitution(int cn) - Get and set the player Constitution score (m_iCharTraits[2]).
void getIntelligence(int *itl), void setIntelligence(int itl) - Get and set the player Intelligence score (m_iCharTraits[3]).
void getWisdom(int *wis), void setWisdom(int wis) - Get and set the player Wisdom score (m_iCharTraits[4]).
void getCharisma(int *chr), void setCharisma(int chr) - Get and set the player Charisma score (m_iCharTraits[5]).
vi. void printAll() - Print all information on this character. This includes, Name, Class, Alignment, Hit points, and all 6 character trait values.
d. The class file and its' associated header file must be capable of being compiled and linked in with the instructor's driver program (which will contain main) for testing. Do not turn in your source file containing main. It will not be used for testing.
Topic archived. No new replies allowed.