When I declare a class or an instance of the class, I get the error,
error: redefinition of 'class player'
then after, I get, error: previous definition of 'class player'
by the way, I am using gc++.

If you want to see the source, just ask.
thanx,
JT

The most common reason for that error is including the same header file more thn once in the same *.c or *.cpp file. To prevent the problem you need to add code guards in the header file

#ifndef MYHEADER_H
#include MYHEADER_H

// declare class and other stuff here

#endif

That bit that says

previous definition of 'class player'

will tell you where you already defined it. If you look at that, you'll be able to see where you defined it already.

Sounds obvious, I know.

I have tried these tips and they havn't worked.

You don't think it's an issue with the compiler...

Here's the code

class player
{
    public: int money;
    int roll;
    int spotID;
    private:
    char name[45];
    bool banker;
    void getOriginalValues()
    {

    }
};
player p1,p2,p3,p4,p5,p6;

I get errors for all of the instances of the player class.

Thanx,
jt

>>You don't think it's an issue with the compiler...
It is definitely not an issue with the compiler. You can rule that out.

If there is anywhere in your code a repetition of the form "class player {", then one of them must go.

Make sure you use the header guards.

The code you posted is not the error, unless this was part of a header file that is not guarded. It should be like so:

#ifndef PLAYER_H
#define PLAYER_H

class player
{
    public: int money;
    int roll;
    int spotID;
    private:
    char name[45];
    bool banker;
    void getOriginalValues()
    {

    }
};
player p1,p2,p3,p4,p5,p6;

#endif

Edited 5 Years Ago by mike_2000_17: n/a

I have tried these tips and they havn't worked.

I'm not convinced that you have. My tip was to look at the error message and identify the lines where you repeatedly define the same thing over and over.

Edited 5 Years Ago by Moschops: n/a

I'm not sure if it's considered 'bad practice' but I typically include

#pragma once

at the beginning of all my header files.

Edited 5 Years Ago by Duki: n/a

'#pragma once' is by no means bad practice, but it is non-standard, and unrecognised #pragma statements tend to be ignored rather than flagged, so it would be possible to write code that worked on one preprocessor/compiler/linker chain and not another; of course, you'd get a bunch of errors from multiple includes and discover the unsupported #pragma, so it wouldn't be catastrophic.

I have tried these tips and they havn't worked.

You don't think it's an issue with the compiler...

Here's the code

class player
{
    public: int money;
    int roll;
    int spotID;
    private:
    char name[45];
    bool banker;
    void getOriginalValues()
    {

    }
};
player p1,p2,p3,p4,p5,p6;

I get errors for all of the instances of the player class.

Thanx,
jt

In and of itself, this code is fine. Are you positive that you have you added "header guards" as suggested? There is no indication of them in this snippet.

As long as your header guards are in place, and this is the only code in the header, I think at this point you need to look at other files as the likely culprit(s).

Please post the relevant section(s) of your main *.cpp file (or whatever you may have named it).

Edited 5 Years Ago by Fbody: n/a

I don't think the problem is with his header being included multiple times. It appears to me that the issue is that he is instantiating the player objects p1, p2, p3, etc. within the header file. It's hard to tell without seeing the other files as well and what is included. I don't see a "main", so this is apparently not the only file in the program.

Maybe you could provide us with more details?

Instantiating objects in a header file isn't forbidden, it's just (very) bad practice and would only be a problem if the header were included multiple times. I wonder if there are error messages about redefinition of existing objects p1, p2, p3... as well, tucked away in the error messages list.

We certainly could do with seeing the rest of the code and all the error messages.

Edited 5 Years Ago by Moschops: n/a

If the objects were instantiated in a header file and the header file included in two or more *.c or *.cpp files, then the linker would complain about duplicate declarations, not "redefinition of 'class player'". That's why its more likely a problem with missing header guards, or the class is in fact declared multiple times in the same header and/or *.cpp file. Even lack of header guards would not be a problem is the header file is included only once in the same *.cpp file.

Edited 5 Years Ago by Ancient Dragon: n/a

Agree, AD, but based on the snippet that was provided, it doesn't give one a warm, fuzzy feeling. There is no way of being deterministic without seeing all the code.

I've got a board class in another file (board.cpp (is it correct to use .cpp files?))
And of course, that's not called player.

So, I'll put the Header Guareds in again and I'll see if it works.

Thanxs,
jt

Now when I do it, it stops giving me the errors about defining the class multiple times.
However, it gives me errors about the instances being defined twice or "Multiple times" in the code... I definitely didn't define it twice.

Another thing is that it mentions some things in the *.o files (for main.cpp, board.cpp, and player.cpp). Is that normal. I've never seen it before.

If it's throwing errors related to *.o files, they're probably LINKER errors, not COMPILER errors, but we really can't say.

You must post relevant code and error messages when requesting assistance. Without them, everything we post is educated guesses and conjecture. It may be close, or it may be "out in Left-Field". Please post the details of the errors, and the relevant code.

This article has been dead for over six months. Start a new discussion instead.