tic tac toe Class, printing a 2D Array board issue

I'm not sure what I'm doing. Any suggestions will be greatly appreciated.

I have to make a tic tac toe class. First obstacle, print a 2D array. I’m trying to get this set up one function at a time.

Main hindrance:I keep getting this wicked compiling error

printtest.cpp: In function ‘int main()’:
printtest.cpp:20: error: expected primary-expression before ‘char’


This makes it so I can't verify if I am on the right track to finish the rest of my class. If I can figure out how to do this, I think I'll be able to finish the rest of tic tac toe class.

heres all 3 files TTT.h TTT.cpp printtest.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**********************HEADER FILE********************************/ 
#ifndef TTT_H  
#define TTT_H

class TTT
{
  
public:
  TTT();
  ~TTT();
  void printboard(char board[3][3]);  
private:
  int i;
  int j;
  char board[3][3];
}; 
#endif 

/**********************IMPLEMENTATION FILE********************************/ 
#include "TTT.h"
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <iomanip>

//default constructor
TTT::TTT() //private part of header goes in here
{
  int i;
  int j;
  char board[3][3];
}
TTT::~TTT()
{
  cout<<"This is destructor"<<endl;
}
//prints the tic tac toe board using a 2d array.
void TTT::printboard(char board[3][3])
{
  for(i=0;i<3;i++)
    {
      for(j=0;j<3;j++)
	{
	  cout<<board[i][j]<<"|";
	} 
      cout<<"\n";
      cout<<"-|-|-"<<endl;		
    }
}

/**********************TEST/DRIVER FILE********************************/ 
#include "TTT.h"
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <iomanip>

int main ()
{
  TTT TTT1;  		
  
  TTT1.printboard(char board[3][3]);
  return 0;
}



Last edited on
On line 62: You can't pass parameters to a function like that. You need to create the board, then pass it:

1
2
char board[3][3]
TTT1.printboard(board);
thanks, it compiled! Unfortunately, This leds me into a different problem. this is the result:

-|-|-

-|-|-
K
-|-|-



not sure how to make it look like a standard tictactoe board given my code. It always turns out all scrambled like above.
Heres an update of my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//TTT.h
#ifndef TTT_H  
#define TTT_H
class TTT
{
 public:
  TTT();
  ~TTT();
  void printboard(char [3][3]);  
 private:
  int i;
  int j;
  char board[3][3];
}; 
#endif  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//implementation file

#include "TTT.h"
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <iomanip>

//default constructor
TTT::TTT() //private part of header goes in here
{
  int i;
  int j;
  char board[3][3];
}
TTT::~TTT()
{
  cout<<"This is destructor"<<endl;
}
//prints the tic tac toe board using a 2d array.
void TTT::printboard(char[3][3])
{
  for(i=0;i<3;i++)
    {
      for(j=0;j<3;j++)
	{
	  cout<<board[i][j]<<"|";
	} 
      cout<<"\n";
      cout<<"-|-|-"<<endl;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//test.cpp
#include "TTT.h"
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <iomanip>

int main ()
{
  char board[3][3] = {{'x', 'x', 'o'},
		     {'o', 'o', 'x'},
		      {'x', 'o', ' '}	};
  int i;
  int j;
  TTT TTT1;  	
  TTT1.printboard(board);
  return 0;
}

updated with attempt to initialize. As you see I am new at OOP, I mean I don't even know if this is the proper way to input this initialization.
Last edited on
Did you initialize the array? If you didn't, it will (probably) be full of garbage data like K's and question marks.
Topic archived. No new replies allowed.