简体   繁体   English

从用户定义的头文件调用函数时发生未定义的引用错误,并且其实现位于.cpp文件中

[英]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 . 我所做的是一个fstreamExtension类,该类公开继承了fstream class

fstreamExtension.h : 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

fstreamExtension.cpp : 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 : 当此codemain文件中实现为:

#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() . 当我调用函数filename()fileSize()时,出现linker error

The codeblocks species the following error : codeblocks产生以下错误:

undefined reference to fstreamExtension::fileName(std::string&) 未定义对fstreamExtension :: fileName(std :: string&)的引用

Thanks for your help, please suggest if any structure changes are required. 感谢您的帮助,请提出是否需要任何结构更改的建议。

Remove inline from function declarations and definitions to fix the linker errors. 从函数声明和定义中删除inline以修复链接器错误。

inline makes sense for functions defined in header files. inline对于头文件中定义的功能有意义。 For functions defined elsewhere inline makes them unavailable to other translation units, causing the linker error which you observe. 对于其他地方定义的函数, inline使其无法用于其他翻译单元,从而导致您观察到链接器错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Function 在header中定义并从cpp文件中调用出错 - Function defined in the header and called from the cpp file error 在标头中声明并在cpp文件中实现的函数的未定义引用 - undefined reference to function declared in header and implemented in cpp file 在 .cpp 文件中定义静态类成员时的未定义引用错误 - undefined reference error for static class member when it is defined in .cpp file Linker 错误:未定义对 header 文件中的 function 的引用 - Linker error: undefined reference to function from header file C++ 在cpp 中使用头文件中的引用函数? - C++ Using a reference function from header file in the cpp? CPP从另一个.cpp文件调用函数 - CPP Calling a function from another .cpp file 在不同的 .cpp 文件中定义的 .cpp 文件中使用函数的 C++ 未定义错误? - C++ undefined error for using function in .cpp file that is defined in a different .cpp file? C ++未定义对头文件外部定义的成员函数的引用 - c++ undefined reference to member function defined outside of header file Android NDK / JNI - 自定义 header 文件中定义的对 function 的未定义引用 - Android NDK / JNI - undefined reference to function defined in custom header file 对实现文件中定义的构造函数的未定义引用 - Undefined reference to constructor defined in implementation file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM