Thread: Rules of the class

  1. #1
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68

    Rules of the class

    Hello everyone, If you read my previous thread you may have hear me say I would provide a demo of my tic tact toe game but, I decided not to because I didn't really think it was that cool. It just tic tact toe with a dumb AI. Something I've been working on that more interesting is my isometric game play. I put almost everything in a class but, now I'm getting a error I don't know how to approach.

    The error is called LNK2005 from visual studios 2010. It's saying that my constructor & destructor Game(); ~Game(); are already defined in another cpp file. Originally when I started I had 2 cpp files with one header to hold the class Game and I only used the constructor Game::Game() ; in one cpp file. Now I'm doing this in two cop files. Is there anyway to make the compiler not confused or how would you suggest I organize the code better?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Example code that demonstrates the problem would be good.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    the code is very heavy and not organized but this is the only error I'm getting. I had a lot of errors everywhere but that has been taken care of.

    I did something like this.


    Header file
    Class
    {
    Game();
    ~Game();
    }

    CPP file 1
    //Declarations
    Game:Game()
    {
    //variable = value;
    }

    CPP file 2
    //Declarations
    Game::Game();
    {
    //variables = value;
    }
    Last edited by LAURENT*; 05-26-2014 at 11:03 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Post a minimal compilable example that demonstrates the problem. Strip down the code until only the specific error you have remains and post it here.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by LAURENT*
    I did something like this.
    Then don't. You defined the default constructor for Game in both file 1 and file 2, thus violating the one definition rule. Pick the definition in one file and delete the definition in the other.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    Quote Originally Posted by laserlight View Post
    Then don't. You defined the default constructor for Game in both file 1 and file 2, thus violating the one definition rule. Pick the definition in one file and delete the definition in the other.
    Oh that the rule I missed thanks. Maybe I can create a new CPP file and put the default in it and link all cop files to it. Can you make a default constructor inside a header file? This may get too complex.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by LAURENT*
    Can you make a default constructor inside a header file?
    Yes, you can define the default constructor in a header file, so long as you either define it within the class definition, or declare it as inline.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    Hey you got a link to how this is done? I wanna try it. Also I kinda forgot what class definition meant.....

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by LAURENT*
    Hey you got a link to how this is done? I wanna try it.
    You would probably have seen it (as in defining a member function within a class definition) in whatever materials you are using to learn C++.

    Quote Originally Posted by LAURENT*
    Also I kinda forgot what class definition meant.....
    The definition of a class named X looks like:
    Code:
    class X
    {
        // ...
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    Thanks now the code work and I found new error. It's just game logic I messed up, no big deal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The rules
    By Shawn Belcher in forum General Discussions
    Replies: 4
    Last Post: 09-22-2011, 02:45 PM
  2. Pls let me know the rules in C
    By ice_breaekr in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-28-2005, 06:27 AM