我正在尝试使用g ++ 5.4.0编译我的基本c ++项目,但出现错误
a b a1 b1
0 1.0 1.0 1.0 1.0
1 4.0 2.0 1.0 1.0
2 NaN 3.0 NaN 1.0
3 5.0 4.0 1.0 1.0
4 10.0 NaN 1.0 NaN
5 12.0 6.0 1.0 1.0
6 1.0 7.0 2.0 1.0
7 3.0 8.0 2.0 1.0
8 4.0 NaN 2.0 NaN
9 NaN 10.0 NaN 1.0
10 8.0 11.0 2.0 1.0
11 12.0 12.0 2.0 1.0
我已将其范围缩小为由定义基类的模板类型的方式引起的-如果删除模板及其所有相关成员函数并清除代码,则它将编译并运行。我不想定义BoolType的类型,因为我知道它总是布尔型的。我该如何正确地做到这一点?
datatype.h
df['a1'] = df[df.a.notnull()].a.diff().fillna(-1).lt(0).cumsum()
df['b1'] = df[df.b.notnull()].b.diff().fillna(-1).lt(0).cumsum()
datatype.cpp
undefined reference to DataType<bool>::DataType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned short)'
booltype.h
#ifndef _DATATYPE_H_
#define _DATATYPE_H_
#include <string>
template<class T>
class DataType
{
std::string const name;
unsigned short const width;
public:
DataType(std::string, unsigned short);
std::string getName() const;
unsigned short getWidth() const;
virtual void toBytes(T, char[]) const = 0;
virtual T fromBytes(char[]) const = 0;
virtual T zero() const = 0;
};
#endif
booltype.cpp
#include <string>
#include "datatype.h"
template <class T>
DataType<T>::DataType(std::string name, unsigned short width)
: name(name), width(width) {}
template <class T>
std::string DataType<T>::getName() const
{
return this.name;
}
template <class T>
unsigned short DataType<T>::getWidth() const
{
return this.width;
}
#ifndef _BOOLTYPE_H_
#define _BOOLTYPE_H_
#include "datatype.h"
class BoolType : public DataType<bool>
{
public:
BoolType();
void toBytes(bool, char[1]) const;
bool fromBytes(char[1]) const;
bool zero() const;
};
#endif
的结果:
// booltype.cpp
#include <string>
#include <iostream>
#include "datatype.h"
#include "booltype.h"
BoolType::BoolType()
: DataType(std::string("BOOL"), 1) {}
void BoolType::toBytes(bool v, char b[1]) const
{
b[0] = v ? 1 : 0;
}
bool BoolType::fromBytes(char b[1]) const
{
return b[0] != 0;
}
bool BoolType::zero() const
{
return false;
}
int main()
{
char toBytesResult[1];
BoolType BOOL;
BOOL.toBytes(false, toBytesResult);
std::cout << "toBytes(false): " << (int)toBytesResult[0] << std::endl;
BOOL.toBytes(true, toBytesResult);
std::cout << "toBytes(true): " << (int)toBytesResult[0] << std::endl;
std::cout << "zero(): " << (int)BOOL.zero() << std::endl;
}