0

"I am new to programing. I am learning it by myself so its challenging. I have a problem with this code in C++, i have a global variable, but when i want to use a function, it says that it was not declared in this scope. I tried with using :: before variables but it was not working. i have 3 files, ttt.cppp ttt.hpp and ttt_funk.cpp"

//ttt.cpp
#include <iostream>
#include "ttt.hpp"
#include <vector>
std::vector<char> prvi = {' ',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ',' ',' ',' '};
std::vector<char> drugi = {' ',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ',' ',' ',' '};
std::vector<char> treci = {' ',' ',' ',' ','|',' ',' ',' ',' ',' ','|',' ',' ',' ',' '};



int main(){
  


std::vector<int> player1;
std::vector<int> player2;
int turn =0;
int choice;
  

gameDesign ();

std::cout<<"Player 1, enter your choice!\n";
std::cin>>choice;
player1.push_back(choice);
if (choice == 1){
  prvi[2] = 'X';
} else if (choice == 2){
  prvi[7] = 'X';
} else if (choice == 3){
  prvi[12] = 'X';
}

std::cout<<"Player 2, enter your choice!\n";
std::cin>> choice;
player2.push_back(choice);



printgrid();

}
//ttt.hpp
void gameDesign ();
void printgrid();
//ttt_funk.cpp
#include <iostream>
#include <vector>



void gameDesign (){
std::cout<<"Welcome to Tic-Tac-Toe\n\n";
std::cout<<" 1  │  2  │ 3  \n";
std::cout<<"──────────────\n";
std::cout<<" 4  │  5  │ 6 \n";
std::cout<<"──────────────\n";
std::cout<<" 7  │  8  │  9\n";
};

void printgrid(){

for(int i=0; i<prvi.size(); i++){
std::cout<<prvi[i];};
std::cout<<"\n";
for(int i=0; i<drugi.size(); i++){
std::cout<<drugi[i];};
std::cout<<"\n";
for(int i=0; i<treci.size(); i++){
std::cout<<treci[i];};
std::cout<<"\n";
};

  
  • 1
    Which variable is giving you trouble? Also, can you post the error? – Raphael Sauer Jul 16 '21 at 17:38
  • Are you missing export statements? – stefan_aus_hannover Jul 16 '21 at 17:42
  • I do not see a loop anywhere – do you really want every player just to do one single move? – Aconcagua Jul 16 '21 at 17:43
  • The problem is that the code in "ttt_funk.cpp" doesn't know about those globals. They need to be declared: `extern std::vector prvi;` etc. I'd be inclined to put those declarations in "ttt.h" and `#include` it into "ttt_funk.cpp". That way, the `#include` in main.cpp will pull the declarations into "main.cpp", and if they're wrong, you'll get an error. – Pete Becker Jul 16 '21 at 17:44
  • Why would you need vectors for the player's choices? Printing some history? Otherwise you might just drop them. I'd recommend you to create the field as `std::array, 3> field;`, that makes index translation a bit simpler. You might want to allow the user to enter field indices like `13` or `3 2` then or similar to a chess board as `c3`. If you want to retain the numbers from 1 to 9 as input you get coordinates by: `std::cin >> n; --n /* 0-based index! */; field[n / 3][n % 3] /* = ... */;`. The line printing I'd add separately. – Aconcagua Jul 16 '21 at 17:51
  • 1
    *I am learning C++ by myself so it is challenging.* I admire your courage, ingenuity, and fortitude, because C++ is a daunting language! You may find it easier to learn with a [good C++ book](https://stackoverflow.com/a/388282/4641116). – Eljay Jul 16 '21 at 18:09
  • It is the game Tic-Tac-Toe. I am not done, but I am trying to test every step before I move on on the next one. Everithing workes well, excepts for function printgrid( ). I will deal more with the game later, but I don´t want to move to another step untill previous is working. If I take the functions body and paste it into Main() it works without a problem. Howerver when its in the functions it has problem to acces vectors PRVI, DRUGI, TRECI. I get message that variables PRVI, DRUGI, TRECI are not declared in that scope. – dali989 Jul 16 '21 at 18:46
  • Well, see @PeteBecker 's comment: You need to declare the variables in the other file (or even better: in a separate header you include there), too. `extern` keyword is important then, it tells the compiler that the variables do exist, but are defined/located elsewhere (the linker will resolve that after compilation). – Aconcagua Jul 16 '21 at 19:26
  • Thanks a lot. It did help with `extern` key word. I just had a trouble because I thought that I can declare and define variable in `ttt.hpp` at the same time, but that didn't work. I declared them in `ttt.hpp` with `extern` key word and defined them in `ttt_funk.cpp`. It's working perfectly now. :) – dali989 Jul 20 '21 at 15:04

0 Answers0