Author Topic: Need Help with Using Classes in Separate Files  (Read 649 times)

0 Members and 1 Guest are viewing this topic.

Offline Recon

  • Serf
  • *
  • Posts: 46
  • Cookies: 23
  • Arguing with Computer
    • View Profile
Need Help with Using Classes in Separate Files
« on: April 11, 2014, 03:16:01 am »
I have searched Google for half an hour now and can't seem to figure out how to do this properly. I never had any such problems with Java, but C++ seems to be much more complicated. Lend a hand and tell me what I'm doing wrong?

Sandbox.cpp
Code: (cpp) [Select]
/*
    AUTHOR: Recon
    DATE:   9 April 2014
*/

#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <time.h>
#include <cstring>
#include <string>
#include "Die.cpp"

using namespace std;

int randRange(int minimum, int maximum);

int main(int argc, char argv[])
{
    Die myDie;
    cout << myDie.roll();
}

int randRange(int minimum, int maximum) {
    return (rand() % (maximum - minimum)) + minimum;
}

Die.cpp
Code: (cpp) [Select]
/*
    AUTHOR: Recon
    DATE:   9 April 2014
*/

#include <stdlib.h>

using namespace std;

class Die
{
    public:
        Die();
        Die(short faceCount);
        ~Die();
        short roll();
    private:
        short defaultFaceCount = 6;
        short faceCount;
};

Die::Die() {
    faceCount = defaultFaceCount;
}

Die::Die(short faceCount) {
    this->faceCount = faceCount;
}

short Die::roll() {
    return (short) (rand() % faceCount - 1) + 1;
}
« Last Edit: April 11, 2014, 08:33:42 am by Phage »

Offline iTpHo3NiX

  • EZ's Pirate Captain
  • Administrator
  • Titan
  • *
  • Posts: 2920
  • Cookies: 328
    • View Profile
    • EvilZone
Re: Need Help with Using Classes in Separate Files
« Reply #1 on: April 11, 2014, 03:30:01 am »
Use the [ code=cpp ] opening bbcode tag to use the syntax highlighter for those with knowledge in c++ to be able to help you out :P
[09:27] (+lenoch) iTpHo3NiX can even manipulate me to suck dick
[09:27] (+lenoch) oh no that's voluntary
[09:27] (+lenoch) sorry

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Need Help with Using Classes in Separate Files
« Reply #2 on: April 11, 2014, 05:59:52 am »
Well besides your fucked up main statement:

int main(int argc, char argv[])

should be

int main(int argc, char *argv[])

and missing a destructor which you defined/declare but don't implement (~Die), it compiles fine for me.

Code: [Select]
[11:58 PM Thu Apr 10][0][bluechill /Users/bluechill/test]$ clang++ -stdlib=libc++ -std=c++11 main.cpp -o test
[11:58 PM Thu Apr 10][0][bluechill /Users/bluechill/test]$ ./test
1[11:59 PM Thu Apr 10][0][bluechill /Users/bluechill/test]$

However, you're really supposed to use headers and source files.  Put the implementations of Die in Die.cpp and #include "Die.h" where you define the class (the class Die { ... } portion).  And then in main.cpp #include "Die.h".  When you compile, then you must do the following:

clang++ -stdlib=libc++ -std=c++11 -c Die.cpp -o die.o
clang++ -stdlib=libc++ -std=c++11 -c main.cpp -o main.o
clang++-stdlib=libc++ -std=c++11 -o test main.o die.o
« Last Edit: April 11, 2014, 06:01:38 am by bluechill »
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Need Help with Using Classes in Separate Files
« Reply #3 on: April 11, 2014, 10:04:53 am »
Try to first do this with normal functions and as bluechill says drop your direct cpp file include and use a header with header guards like

die.h
Code: (cpp) [Select]
#ifndef DIE_HEADER_GUARD
#define DIE_HEADER_GAURD

class Die {
/***/
}

/**/
#endif

« Last Edit: April 11, 2014, 10:05:27 am by Factionwars »
~Factionwars