Hi, I've been revising object orientated programming, and decided to have a look at accessing parts of my code in other files.

The problem is, I get error messages such as "Player1" not declared, and I can't figure out how to declare them.


For example, in my main.cpp file I have:

#include <iostream>
#include "players.hpp"
using namespace std;
int main()
{
    players();

return 0;
}

And in my players.cpp I have:

#include <iostream>
#include "players.hpp"

using namespace std;

class player{
    string name;
    int score;

public:
    void get_name();
    void set_name();
};

void player::get_name()
{
    cin >> name;
}

void player::set_name()
{
    cout << "Player name: " << name;
}

void players()
{
    player player1, player2;

    cout << "Player 1 name is: ";
    player1.get_name();
    player1.set_name();

    cout << endl;

    cout << "Player 2 name is: ";
    player2.get_name();
    player2.set_name();

    return;
}

and in players.hpp

#ifndef PLAYERS_HPP_INCLUDED
#define PLAYERS_HPP_INCLUDED

void players();

#endif // PLAYERS_HPP_INCLUDED

How would I go about using player1.name in main.cpp?

Thanks,

--Burnout.

Your code is kinda mixed up. Stuff that normally goes in cpp files is in header files, and stuff that normally goes in header files is in cpp files. Try desk checking your program. I've fixed it for you :)

//main.cpp
#include <iostream>
#include "player.cpp"

int main(){
    players();
    return 0;
}

Note that i've included player.cpp, not player.h (Which i accidentally named header.h, soz)

//player.cpp
#include "header.h"
#include <iostream>

void player::get_name()
{
    cin >> name;
}

void player::set_name()
{
    cout << "Player name: " << name;
}

void players()
{
    player player1, player2;

    cout << "Player 1 name is: ";
    player1.get_name();
    player1.set_name();

    cout << endl;

    cout << "Player 2 name is: ";
    player2.get_name();
    player2.set_name();

    return;
}

And header.h:

#ifndef HEADER_H_INCLUDED

class player{
    string name;
    int score;

public:
    void get_name();
    void set_name();
};

It works now :) hope it helped.

Sorry but I don't think I explained my question clearly.

The code works. It does exactly what I want it to do.

My question is:

How do I access something in the main.cpp file I made.
For example, If I were to write:

cout << player1.name;

How would I do this? How can I access player1.name in main.cpp?

Oh sorry. I've just realised what you've done.
Thank you - I'll have a go and see if it works.

Edit: The code works, thanks. But I still don't understand how I could use player1.name in main.cpp.

Thanks.

Sorry but I don't think I explained my question clearly.

The code works. It does exactly what I want it to do.

My question is:

How do I access something in the main.cpp file I made.
For example, If I were to write:

cout << player1.name;

How would I do this? How can I access player1.name in main.cpp?

Well, i'm not really sure, i believe its a matter of scope. Thats from the top of my head, but i think its something to do with that.

*update
Ok, was just messing with the code, it really does seem like a problem with scopes. I put the declaration

player player1, player2

outside player(), and still a scope error came. Maybe someone more experianced can help.

Thanks for the help - you put me on the right track.

I've been messing around with the code for a bit now, and putting player1.name in main.cpp does work - but I have to put the class objects in main.cpp aswell.

At the moment I'm still working out a way to get around the public and private options. Using name works when public, so I've left it as that for now. I'll have another go later on and try and get it to work when private.

Thanks.

This is a better way to have it:
Player.h

#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
#include <string>
using std::string;
class Player
{
	string name;
	int score;

	public:
		Player();
		Player(string name);
		string Get_name();
		void Set_name(string name); 
		void Input_name();
};
#endif

Player.cpp

#include "player.h"
using std::cout;
using std::cin;

Player::Player()
{
	name = "Default";
	score = 0;
}

Player::Player(string name)
{
	this->name = name;
}

string Player::Get_name()
{
	return name;
}

void Player::Set_name(string name)
{
	this->name = name;
}

void Player::Input_name()
{
	cout <<"Enter a name: ";
	cin >> name;
}

playermain.cpp

#include "player.h"
using std::cout;
using std::endl;
int main()
{
	Player player1,player2("Bob");
	player1.Input_name();
	cout <<player1.Get_name()<<endl;
	cout <<player2.Get_name()<<endl;
	
}

Let your getters and setters be just that, a way to get or set your private member variables. Make a header for your class declarations, make a cpp file for your class method definitions, make a cpp for main() and include your header file in both of the cpp files. It's generally not necessary to include a cpp file in another. If you are compiling at the command line, compile Player.cpp then Playermain.cpp or include them in the same project in your IDE.

NB: Normally I would let you get most of this by yourself but the other poster kinda steered you in the wrong direction a bit so I figured having an example would get you back on track.

//main.cpp
#include <iostream>
#include "player.cpp"

int main(){
    players();
    return 0;
}

Note that i've included player.cpp, not player.h (Which i accidentally named header.h, soz)

This is a terrible way of doing things, and can easily end up in creating a mess - please don't encourage others to #include a source (cpp) file.

the correct code should look like this:

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

.cpp files are intended to be used to hold definitions - they're not to be included within other source files unless you want a mess on your hands (Except in the rare situation when you might be playing around with splitting templates between files, but that's not the case here). .

So, unless you have some compelling reason to do otherwise (such as with templates) you should only use #include on a header file.

commented: tnks :) +1

This is a terrible way of doing things, and can easily end up in creating a mess - please don't encourage others to #include a source (cpp) file.

the correct code should look like this:

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

.cpp files are intended to be used to hold definitions - they're not to be included within other source files unless you want a mess on your hands (Except in the rare situation when you might be playing around with splitting templates between files, but that's not the case here). .

So, unless you have some compelling reason to do otherwise (such as with templates) you should only use #include on a header file.

Thank you, i myself am a student (Self taught) and post here so that i can also learn stuff, and get my self set to the right path :)

Err, so even if we don't include one .cpp file in the other, are they still kinda linked to each other?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.