2

So I have three files for my program. My main.cpp looks like this:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include "dtt0055HW4func.cpp"
#include "dtt0055HW4func.h"
using namespace std;
int main()
{   
    int size = 7;
    char (*boardOfGame)[7] = new char[7][7];
    int* sizeOfBoardGame = NULL;
    sizeOfBoardGame = &size;
    initializeBoardGame(boardOfGame, sizeOfBoardGame);
    return 0;
}

And my func.h looks like this:

#ifndef dtt0055HW4func 
#define dtt0055HW4func 

enum typeOfTiles{CROSS = '+', HORIZONTAL = '-', VERTICAL = '|', LOCKED = 'X', EMPTY};
enum colorOfTiles{RED, BLUE};
struct tile{
    typeOfTiles newTile;
    colorOfTiles newTileColor;
    int positionOfNewTile;
};
void initializeBoardGame(char (*boardOfGame)[7],int* sizeOfBoard);

#endif

Thanks, guy for your help on the first error. I have modified my program and it gives me a new issue in func.cpp. Now my func.cpp looks like this:

#include "dtt0055HW4func.h"
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void initializeBoardGame(char (*boardOfGame)[7],int* sizeOfBoard)
{
    char nameOfFile[30],c;
    ifstream inFS;

    cout << "Please enter the name of the input file: ";
    cin >> nameOfFile;

    inFS.open(nameOfFile);
    if (inFS.fail())
    {
        while(inFS.fail())
        {
            cout << "Unable to open the file, please enter the name of the file again. " << endl;
            cin >> nameOfFile;
            inFS.open(nameOfFile);
        }
    }

    while (!inFS.eof())
    {
        for (int i = 0; i < *sizeOfBoard; i++)
        {
            for (int j = 0; j < *sizeOfBoard; j++)
            {
                boardOfGame[i][j] = &inFS.get(c);
                cout << boardOfGame[i][j];
            }
            cout << endl;
        }
    }
    inFS.close();
}

Now my compiler gives me an error in the func.cpp error: invalid conversion from ‘std::basic_istream::__istream_type* {aka std::basic_istream*}’ to ‘char’ [-fpermissive] boardOfGame[i][j] = &inFS.get(c); What I'm trying to do in this line is to assign one character in the file to one index of my array. If i'm using boardOfGame[i][j] = inFS.get(c);, it will give me more errors.

Dat To
  • 45
  • 7
  • 1
    I'm pretty sure you don't want to `#include "dtt0055HW4func.cpp"`. – Rafał Górczewski Nov 19 '18 at 08:47
  • (1) Don't include source (.cpp) files in each other. (2) You're using the same macro for the include guard in both your func.h and func.cpp, and then including both in main.cpp. Because of the order of inclusion, the compiler sees the content of the header, but not the content of func.cpp. The content of func.cpp defines a function and, since the compiler doesn't see it, it is not defined when you link. Hence the error. – Peter Nov 19 '18 at 08:49
  • Your `initializeBoardGame` won't compile once you get the include guards correct. – molbdnilo Nov 19 '18 at 09:36
  • You should read [Why is `iostream::eof` inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – molbdnilo Nov 19 '18 at 09:37

2 Answers2

3

The include guard belongs only in the header file, remove

#ifndef dtt0055HW4func
#define dtt0055HW4func

#endif

from func.cpp. Because it includes func.h at the top before this check, the guard will already be defined and skip the rest of the func.cpp.

Also implementation files (.cpp) should never be #included. They are to be provided directly to the compiler. Remove #include "dtt0055HW4func.cpp".

2

You can replace the ifndefs with #pragma once and it will work for you.

You should not include the cpp file, you should include only headers.

use (on Linux):

g++ -c file.cpp -o file.o
g++ -c main.cpp -o main.o
g++  main.o file.o -o program 

this will build project from 2 cpp files.

SHR
  • 7,940
  • 9
  • 38
  • 57