从用户定义的头文件调用函数时出现未定义的引用错误,它的实现在 .cpp 文件中
undefined reference error while calling a function from user defined header file and it's implementation is in .cpp file
我做的是一个公开继承fstream
class
.
的fstreamExtension
class
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 :
#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";
}
当此 code
在 main
文件中实现时:
#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
}
当我调用函数 filename()
和 fileSize()
.
时,有一个 linker
error
codeblocks
种出现以下错误:
undefined reference to fstreamExtension::fileName(std::string&)
感谢您的帮助,如果需要更改任何结构,请提出建议。
从函数声明和定义中删除 inline
以修复链接器错误。
inline
对头文件中定义的函数有意义。对于其他地方定义的函数 inline
使它们对其他翻译单元不可用,从而导致您观察到的链接器错误。
我做的是一个公开继承fstream
class
.
fstreamExtension
class
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 :
#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";
}
当此 code
在 main
文件中实现时:
#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
}
当我调用函数 filename()
和 fileSize()
.
linker
error
codeblocks
种出现以下错误:
undefined reference to fstreamExtension::fileName(std::string&)
感谢您的帮助,如果需要更改任何结构,请提出建议。
从函数声明和定义中删除 inline
以修复链接器错误。
inline
对头文件中定义的函数有意义。对于其他地方定义的函数 inline
使它们对其他翻译单元不可用,从而导致您观察到的链接器错误。