Здравствуйте.
Суть вопроса: есть класс битового поля, компилятор выдаёт следующие ошибки:
Ошибка LNK1120 неразрешенных внешних элементов: 1 TBitField C:\Git Project\TBitField\Debug\TBitField.e xe 1
Ошибка LNK2019 ссылка на неразрешенный внешний символ "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class TBitField &)" (??6@YAAAV?$basic_ostream@DU?$char_ traits@D@std@@@std@@AAV01@AAVTBitFi eld@@@Z) в функции _main TBitField C:\Git Project\TBitField\TBitField\main.ob j 1
Код:
.h
Код:
#include<iostream>
#include "stdafx.h"
#pragma once
using namespace std;
typedef unsigned int TELEM;
class TBitField
{
int BitLen; //длина битового поля
int MemLen; //размер массива
TELEM *pMem;
int GetMemIndex(const int n) const;
TELEM GetMemMask(const int n) const; //битовая маска
public:
TBitField(int Len);
TBitField(const TBitField &);
~TBitField();
int GetLenght(void) const ;
void SetBit(const int n);
void ClrBit(const int n);
int GetBit(const int n) const;
const TBitField& operator= (const TBitField&);
TBitField operator |(const TBitField &);
TBitField operator &(const TBitField &);
TBitField operator ~(void);
bool operator== (const TBitField&);
friend istream & operator>>(istream &/*istr*/, TBitField &);
friend ostream & operator<<(ostream &/*ostr*/, TBitField &);
};
.cpp
Код:
#include "stdafx.h"
#include "TBitField.h"
#include<iostream>
using namespace std;
int TBitField::GetMemIndex(const int n) const
{
return n>>5;
}
TELEM TBitField::GetMemMask(const int n) const
{
return 1 <<(n & 31);
}
TBitField::TBitField(int Len)
{
BitLen = Len;
MemLen = (Len + 31) >> 5;
pMem = new TELEM[MemLen];
if (pMem != NULL)
for (int i = 0; i < MemLen; i++)
pMem[i] = 0;
}
TBitField::TBitField(const TBitField & BF)
{
BitLen = BF.BitLen;
MemLen = BF.MemLen;
pMem = new TELEM[MemLen];
if (pMem != NULL)
for (int i = 0; i < MemLen; i++)
pMem[i] = BF.pMem[i];
}
TBitField::~TBitField()
{
delete[]pMem;
}
int TBitField::GetLenght(void) const
{
return BitLen;
}
void TBitField::SetBit(const int n)
{
if ((n >= 0) && (n < BitLen))
pMem[GetMemIndex(n)] |= GetMemMask(n);
}
void TBitField::ClrBit(const int n)
{
if ((n >= 0) && (n < BitLen))
pMem[GetMemIndex(n)] &= ~GetMemMask(n);
}
int TBitField::GetBit(const int n) const
{
if ((n >= 0) && (n < BitLen))
return pMem[GetMemIndex(n)] & GetMemMask(n);
else return 0;
}
const TBitField& TBitField::operator= (const TBitField & BF)
{
BitLen = BF.BitLen;
if (MemLen != BF.MemLen) {
MemLen = BF.MemLen;
if (pMem != NULL)
delete[]pMem;
pMem = new TELEM[MemLen];
}
for (int i = 0; i < MemLen; i++)
pMem[i] = BF.pMem[i];
return *this;
}
TBitField TBitField::operator&(const TBitField & BF)
{
int MaxMemLen;
if (MemLen > BF.MemLen)
MaxMemLen = MemLen;
else MaxMemLen = BF.MemLen;
TBitField Res(MaxMemLen);
for (int i = 0; i < MemLen; i++)
Res.pMem[i] = pMem[i];
for (int i = 0; i < BF.MemLen; i++)
Res.pMem[i] = BF.pMem[i];
return Res;
}
bool TBitField::operator== (const TBitField& BF) {
bool flag = true;
int min = MemLen > BF.MemLen ? BF.MemLen : MemLen;
for (int i = 0; i < min; ++i)
if (pMem[i] != BF.pMem[i]) {
flag = false;
break;
}
if (MemLen > BF.MemLen) {
for (int i = BF.MemLen; i < MemLen; ++i)
if (BF.pMem[i] != 0) {
flag = false;
break;
}
}
else
for (int i = MemLen; i < BF.MemLen; ++i)
if (BF.pMem[i] != 0) {
flag = false;
break;
}
return flag;
}
TBitField TBitField::operator| (const TBitField& BF) {
int len = BitLen > BF.BitLen ? BitLen : BF.BitLen;
TBitField tmp(len);
for (int i = 0; i < tmp.MemLen; ++i)
tmp.pMem[i] = BF.pMem[i];
int min = MemLen > tmp.MemLen ? tmp.MemLen : MemLen;
for (int i = 0; i < min; ++i)
tmp.pMem[i] |= pMem[i];
return tmp;
}
TBitField TBitField::operator~ (void) {
int len = BitLen;
TBitField tmp(len);
for (int i = 0; i < MemLen; ++i)
tmp.pMem[i] = ~pMem[i];
return tmp;
}
istream & operator>>(istream &istr, TBitField &BF)
{
int i = 0;
char ch;
do
{
istr.get(ch);
} while (ch != ' ' && ch != '\n');
while (1)
{
istr.get(ch);
if (ch == '0')
{
BF.ClrBit(i++);
}
else
if (ch == '1')
{
BF.SetBit(i++);
}
else
break;
}
return istr;
}
ostream& operator<< (ostream& os, const TBitField& BF) {
int len = BF.GetLenght();
for (int i = 0; i < len; ++i) {
if (!(i % 8))
os << endl;
if (BF.GetBit(i))
os << '1';
else
os << '0';
}
return os;
}
Помогите, пожалуйста