mercredi 16 mai 2018

undefined reference error while calling a function from user defined header file and it's implementation is in .cpp file

What I have made is an fstreamExtension class that publically inherits fstream class.

file : fstreamExtension.h

#include <fstream>
#include <string>
#include <vector>
#ifndef FSTREAMEXTENSION_H
#define FSTREAMEXTENSION_H

class fstreamExtension : public std::fstream
{
 private:

    std::string fileIdentifier;

public:

    using std::fstream::fstream;
    using std::fstream::open;

    ~fstreamExtension();

    inline void fileName (std::string&);

    inline bool exists ();

    inline unsigned long long fileSize();
};
#endif

file : fstreamExtension.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "fstreamExtension.h"

inline void fstreamExtension::fileName (std::string& __fileIdentifier)
{
   fileIdentifier = __fileIdentifier;
}

inline bool fstreamExtension::exists ()
{
  if (FILE *file = fopen(fileIdentifier.c_str(), "r"))
  {
    fclose(file);
    return true;
  }

  else
    return false;
}


inline unsigned long long int fstreamExtension::fileSize()
{
  if(exists())
  {
    std::ifstream tempStream(fileIdentifier.c_str(), std::ios::ate |  std::ios::binary);
    unsigned long long int __size = tempStream.tellg();
    tempStream.close();
    return __size;
}

else return 0;
}

fstreamExtension::~fstreamExtension()
{
  std::fstream::close();
  std::cout << "stream closed";
}

when this code is implemented in main file as:

#include <iostream>
#include <fstream>
#include "fstreamExtension.h" 

int main()
{
  string s = "QBFdata.txt";
  fstreamExtension fs(s.c_str(), ios::in | ios::binary);
  fs.fileName(s); //error
  cout << fs.fileSize(); //error
}

there is a linker error when I call functions filename() and fileSize().

The codeblocks species the following error:

undefined reference to `fstreamExtension::fileName(std::string&)'

I have already tried a clean build and also have tried to delete these source files and recreate and place the code within them.

Thanks for your help, please suggest if any structure changes are required.

Aucun commentaire:

Enregistrer un commentaire