Search code examples
c++identifier

Identifier not found for function defined in a .h file


I keep getting the "Identifier not found"-error, when I try to run my program... I've declared the function in a .h file, implemented it in a .cpp file and I try to use it in my main file.

Can you help?

//main.cpp
#include <iostream>    // using IO functions
#include<fstream>
#include "SokobanSolver.h"

using namespace std;

int main() {
    
    loadFile();

    system("pause");
}

As seen above, I remembered to include the SokobanSolver.h in the main.cpp file...

//SokobanSolver.h
#ifndef SOKOBANSOLVER_H
#define SOKOBANSOLVER_H

#include"Position.h"
#include<vector>
#include <iostream>    // using IO functions

using namespace std;

class SokobanSolver {
private:
    vector<Position> walls;
    vector<Position> goals;
    vector<Position> boxes;


//member functions
public:
    void loadFile();

};

#endif

//SokobanSolver.cpp
#include"SokobanSolver.h"
#include"Position.h"
#include<vector>
#include <iostream>
#include <fstream>

void SokobanSolver::loadFile()
{
    ifstream input("problemFile.txt");
    char ch;

    int row = 0;
    int col = 0;

    while (input.get(ch))
    {
        //new line?
        if (ch == '\n')
        {
            row++;
            col = 0;
        }
        else
        {
            if (ch == '#')
                walls.push_back(Position(row, col));
            else if (ch == '.')
                goals.push_back(Position(row, col));
            else if (ch == '$')
                boxes.push_back(Position(row, col));
            col++;
        }
    }
}

I also put the SokobanSolver.h in the SokobanSolver.cpp file... So I don't get, why it can't find the function loadFile.


Solution

  • As has been pointed out in the comments, you need a SokobanSolver object to call the member function loadFile:

    int main() {
        
        SokobanSolver solver; 
        solver.loadFile();
    
        system("pause");
    }