偶作的一个带有整数部分属性的字符串
编程时经常碰到像对学号,帐号的处理,用字符串无法实现自增或自减等功能,感觉很不方便,便自己作了一个“带有整数部分性质的字符串”类(不知这样叫合不合适),本来想等通过了后把注释都写好了贴上来,让各位大侠帮忙提点意见。但现在出现点问题,编译通过了,但链接时出现了问题,报错:
正在编译...
test.cpp
IntString.cpp
Generating Code...
正在链接...
test.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl SLHnamespace::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class SLHnamespace::IntString<8> const &)" (??6SLHnamespace@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABV?$IntString@$07@0@@Z),该符号在函数 _main 中被引用
test.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall SLHnamespace::IntString<8>::IntString<8>(char const *)" (??0?$IntString@$07@SLHnamespace@@QAE@PBD@Z),该符号在函数 _main 中被引用
I:\SourceCode\MySourceCode\MyProject\IntString带有整型部分属性的字符串\Debug\IntString带有整型部分属性的字符串.exe : fatal error LNK1120: 2 个无法解析的外部命令
编程时经常出现这种问题,到网上找也是众说纷纭,有人说是并发性问题,恳求各位大侠耐心帮忙解决一下,谢谢。
下边是代码:
//IntString.h
#ifndef __INTSTRING_H__
#define __INTSTRING_H__
#include <string>
#include <iostream>
#include <cstring>
#include <vector>
using std::string;
using std::istream;
using std::ostream;
using std::vector;
using std::iterator;
namespace SLHnamespace
{
typedef unsigned int size_t;
class size_not_fit
{
public:
explicit size_not_fit() : _size( 0 ) {}
explicit size_not_fit( size_t si ) : _size( si ) {}
size_t size() const { return _size; }
private:
const size_t _size;
};
class out_of_rang
{
public:
explicit out_of_rang() : _index( 0 ) {}
explicit out_of_rang( size_t in ) : _index( in ) {}
size_t index() const { return _index; }
private:
const unsigned int _index;
};
class ref_on_empty
{
public:
explicit ref_on_empty() : _index( 0 ) {}
explicit ref_on_empty( unsigned int in ) : _index( in ) {}
unsigned int index() const { return _index; }
private:
const unsigned int _index;
};
template <size_t _size = 8>
class IntString
{
typedef unsigned int size_type;
friend istream & operator >>( istream & , IntString <_size> & ) throw( size_not_fit );
friend ostream & operator < <( ostream & , const IntString <_size> & ) throw( ref_on_empty );
friend bool operator ==( const string & , const IntString <_size> & ) throw();
friend bool operator ==( const char * , const IntString <_size> & ) throw();
friend bool operator !=( const string & , const IntString <_size> & ) throw();
friend bool operator !=( const char * , const IntString <_size> & ) throw();
friend bool operator >( const string & , const IntString <_size> & ) throw();
friend bool operator >( const char * , const IntString <_size> & ) throw();
friend bool operator <( const string & , const IntString <_size> & ) throw();
friend bool operator <( const char * , const IntString <_size> & ) throw();
friend bool operator >=( const string & , const IntString <_size> & ) throw();
friend bool operator >=( const char * , const IntString <_size> & ) throw();
friend bool operator <=( const string & , const IntString <_size> & ) throw();
friend bool operator <=( const char * , const IntString <_size> & ) throw();
friend IntString <_size> operator +( int , const IntString <_size> & ) throw();
public:
explicit IntString() throw() { _empty = true; }
explicit IntString( const char * ) throw( size_not_fit );
explicit IntString( const string & ) throw( size_not_fit );
explicit IntString( const IntString <_size> & ) throw() { str = _str.to_str();_empty = false; }
size_type size() const throw() { return _size; }
bool empty() const throw() { return _empty; }
const char * c_str() const throw() { return str.c_str(); }
const string& to_str() const throw() { return _size; }
void swap( IntString &is ) throw();
char& operator []( size_type _index ) const throw(){ return str[ _index ]; }
char& at( size_type _index ) const throw( out_of_rang , ref_on_empty );
IntString <_size> operator +( int i ) const throw();
IntString <_size> operator -( int i ) const throw();
IntString <_size>& operator =( const IntString & ) throw();
IntString <_size>& operator =( const string & ) throw();
IntString <_size>& operator =( const char * ) throw();
IntString <_size>& operator +=( int i ) throw();
IntString <_size>& operator -=( int i ) throw();
IntString <_size>& operator ++() throw();
IntString <_size> operator ++( int ) throw();
bool operator ==( const IntString <_size> & ) const throw();
bool operator ==( const string & ) const throw();
bool operator ==( const char * ) const throw();
bool operator !=( const IntString <_size> & ) const throw();
bool operator !=( const string & ) const throw();
bool operator !=( const char * ) const throw();
bool operator >( const IntString <_size> & ) const throw();
bool operator >( const string & ) const throw();
bool operator >( const char * ) const throw();
bool operator <( const IntString <_size> & ) const throw();
bool operator <( const string & ) const throw();
bool operator <( const char * ) const throw();
bool operator >=( const IntString <_size> & ) const throw();
bool operator >=( const string & ) const throw();
bool operator >=( const char * ) const throw();
bool operator <=( const IntString <_size> & ) const throw();
bool operator <=( const string & ) const throw();
bool operator <=( const char * ) const throw();
private:
string plus_to_other( size_type i ) const throw();
void plus_on_self( size_type i ) throw();
string minus_to_other( size_type i ) const throw();
void minus_on_self( size_type i ) throw();
string str;
bool _empty;
};
}
#endif