1

I am trying to connect two cpp files - MIPSConversion.cpp and TestMIPSConversion.cpp - to the same header file - MIPSConversion.h. However, when I try to call the method readInTheFile() (located in MIPSConversion.cpp) from main() (located in TestMIPSConversion.cpp) I get the error use of undeclared identifier 'readInTheFile'. How do I correct this issue?

MIPSConversion.h

#include <iostream>
using namespace std;

class MIPSConversion
{
    public: 
    MIPSConversion();
    ~MIPSConversion();

    void readInTheFile();
};

MIPSConversion.cpp

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

   MIPSConversion::MIPSConversion(){}
   MIPSConversion::~MIPSConversion(){}

    void MIPSConversion::readInTheFile(){
        string inputFileName; 
        char* arrayString = new char[31];

        cout << "Enter the name of the file you want to import: " << endl;
        inputFileName = cin;
        getline(cin, inputFileName);
        ifstream inputFile (inputFileName, ifstream::in);

        while (inputFile.good()) {
            for(int i = 0; i < 31; i++){
                arrayString[i] = inputFile.get();
            }  
            cout << arrayString; 
            if (inputFile.peek() == std::ifstream::traits_type::eof()){
                break;
            }
        }

        inputFile.close();
    }

TestMIPSConversion.cpp

#include <iostream>
#include "MIPSConversion.h"
using namespace std;

int main()
{   
    readInTheFile();
    return(0);
}
Jason
  • 2,278
  • 2
  • 17
  • 25
  • 1
    This has nothing to do with headers, or with linking, or with CPP (which is the preprocessor). This is a basic question about how to invoke member functions on classes. You could have narrowed down the code substantially. [mcve] – Lightness Races in Orbit Mar 28 '16 at 02:46

3 Answers3

3

You need to create an object of MIPSConversion in main. Then you can use its member function readInTheFile(). Like the following:

MIPSConversion myObj;
myObj.readInTheFile();
MIbrah
  • 1,007
  • 1
  • 9
  • 17
1

readInTheFile is a non-static member of MIPSConversion, so you must create a MIPSConversion object on which to invoke readInTheFile:

MIPConversion mipsConversion;
mipsConversion.readInTheFile();

If there's actually no data you want stored in mipsConversion by readInTheFile or other functions, you can get rid of the mipsConversion class altogether, and make readInTheFile a file-scope (non-member) function. Or, if you have some other use for the MIPSConversion class but readInTheFile doesn't need to access its data, you could leave readInTheFile in MIPSConversion but make it static, which means it can be called even without an object instance of MIPSConversion, as in:

MIPSConversion::readInTheFile();
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
1

You've currently declared readInTheFile as a member function of the class MIPSConversion, meaning you need an instance of the class MIPSConversion in order to actually call the function. You can solve this in multiple ways.

  1. declare the function as static. This means that the function is available to be called without an instance of MIPSConversion (which considering the implementation, appears to be what you're going for). Easy to do,

    class MIPSConversion
    {
    public: 
        MIPSConversion();
        ~MIPSConversion();
    
        static void readInTheFile();
    };
    

And then when you call the function, prefix it with the class name

    MIPSConversion::readInTheFile();
  1. Create an instance of the class. Just create an instance and then call the function from it.

    MIPSConversion conversionVariable;
    conversionVariable.readInTheFile();
    
  2. If you want to skip the class schemantics altogether (maybe it doesn't make sense to have an "instance" of MIPSConversion, just replace class with Namespace and follow the same calling convention as with static functions.

    namespace MIPSConversion
    {
        void readInTheFile();
    }
    

and in TestMIPSConversion.cpp

MIPSConversion::readInTheFile();