Xcode C++ classes different files

I am trying to make a C++ poker program using different classes. The classes are going to be in different files for organizations sake (if possible). So far my program should just print the deck out, but even though the program builds correctly it does not launch because of a permission denied issue. A window pops up that says "error: ::posix_spawnp ( pid => 674, path = '/Users/toddbaker/Documents/Poker/Build/Poker/Build/Products/Debug/Poker', file_actions = 0x11c6b7c98, attr = 0x11c6b7cd8, argv = 0x7fe6dcf1a6a0, envp = 0x7fe6db9f3650 ) err = Permission denied (0x0000000d)"


I have the files:

main.cpp (Performs main() function)

Deck.cpp (Defines Deck class)

Deck.h (Declares Deck class)

Card.cpp (Declares Card class)

Card.h (Defines Card class)


Would the settings of the files cause a permission denial error? Is it not possible to define classes in different files? Does anyone have any tips? I can post the code that I have in each file if you think that will help. Thanks so much in advance.

null-OTHER, OS X Yosemite (10.10.1), Xcode C++

Posted on Dec 1, 2014 6:51 PM

Reply
10 replies

Dec 4, 2014 4:36 PM in response to etresoft

It still isn't working for me. Maybe there is something wrong with my code. Can you take a look at my code?


Main.cpp:

#include "Deck.h"


int main() {

Deck d;

d.print();

}


Deck.cpp:

#include "Deck.h"

#include <string>

#include <iostream>

#include <cstdlib>


Deck::Deck(){

for (int i = 0; i < 4; i++) {

for (int j = 0; j < 13; j++) {

cards[i * 13 + j].suit = i;

cards[i * 13 + j].rank = j;

}

}

Card::suits[0] = "D";

Card::suits[1] = "S";

Card::suits[2] = "H";

Card::suits[3] = "C";

Card::ranks[0] = "2";

Card::ranks[1] = "3";

Card::ranks[2] = "4";

Card::ranks[3] = "5";

Card::ranks[4] = "6";

Card::ranks[5] = "7";

Card::ranks[6] = "8";

Card::ranks[7] = "9";

Card::ranks[8] = "T";

Card::ranks[9] = "J";

Card::ranks[10] = "Q";

Card::ranks[11] = "K";

Card::ranks[12] = "A";

}


void Deck::print(){

cout << "Printing deck..." << endl;

for (int i = 0; i < 52; i++) {

cout << Card::ranks[cards[i].rank] << Card::suits[cards[i].suit] << endl;

}

cout << endl;

}


void Deck::shuffle(){

int x;

Card tempCard;

Deck();

cout << "Shuffling the deck..." << endl;

for (int i = 0; i < 52; i++) {

x = rand() % 52;

tempCard = cards[i];

cards[i] = cards[x];

cards[x] = tempCard;

}

}


Deck.h:

#ifndef __Poker__Deck__

#define __Poker__Deck__


#include <stdio.h>

#include "Card.h"


class Deck{

public:

Deck();

void print();

void shuffle();

private:

Card cards[52];

};


#endif /* defined(__Poker__Deck__) */


Card.cpp:

#include "Card.h"


Card.h:

#ifndef __Poker__Card__

#define __Poker__Card__


#include <stdio.h>

#include <string>

using namespace std;


class Card{

public:

static string suits[4];

static string ranks[13];

int suit;

int rank;

};


#endif /* defined(__Poker__Card__) */

Dec 4, 2014 5:28 PM in response to ttbake

I had to flesh out Card.cpp like so:


#include "Card.h"

#include <string>


usingnamespacestd;


string Card::suits[4];

string Card::ranks[13];


But then it compiled and ran fine from both the command line and Xcode.


From the command line I did:


clang++ -c -o Deck.o Deck.cpp

clang++ -c -o Card.o Card.cpp

clang++ main.cpp Deck.o Card.o

./a.out


Then, in Xcode, I created a new command-line tool based on C++. I replaced the boilerplate main.cpp with your main.cpp. I then added your other files to the project. It builds and runs fine:


Printing deck...

2D

3D

4D

5D

6D

7D

8D

9D

TD

JD

QD

KD

AD

2S

3S

4S

5S

6S

7S

8S

9S

TS

JS

QS

KS

AS

2H

3H

4H

5H

6H

7H

8H

9H

TH

JH

QH

KH

AH

2C

3C

4C

5C

6C

7C

8C

9C

TC

JC

QC

KC

AC


Program ended with exit code: 0

Dec 5, 2014 6:43 PM in response to ttbake

In C++, nothing in the header files ever actually exists. All the headers do is define how functions will look, how classes and structs are defined, and what structures might exist. In order to make any of these things exist, they have to be instantiated in a source file. As the last step, the linker goes through and uses those definitions in the headers to connect all the dots between the different source files.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Xcode C++ classes different files

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.