error LNK2001: 无法解析的外部符号
代码如下:
//异常类代码 d_except.h
#ifndef EXCEPTION_CLASSES
#define EXCEPTION_CLASSES
#include <strstream>
#include <string>
using namespace std;
class baseException
{
public:
baseException(const string& str = ""):
msgString(str)
{
if (msgString == "")
msgString = "Unspecified exception";
}
string what() const
{
return msgString;
}
// protected allows a derived class to access msgString.
// chapter 13 discusses protected in detail
protected:
string msgString;
};
// failure to allocate memory (new() returns NULL)//内存分配错误
class memoryAllocationError: public baseException
{
public:
memoryAllocationError(const string& msg = ""):
baseException(msg)
{}
};
// function argument out of proper range //范围错误
class rangeError: public baseException
{
public:
rangeError(const string& msg = ""):
baseException(msg)
{}
};
// index out of range //索引范围错误
class indexRangeError: public baseException
{
public:
indexRangeError(const string& msg, int i, int size):
baseException()
{
char indexString[80];
ostrstream indexErr(indexString, 80);
indexErr << msg << " index " << i << " size = " << size << ends;
// indexRangeError can modify msgString, since it is in
// the protected section of baseException
msgString = indexString;
}
};
// attempt to erase from an empty container //下溢错误
class underflowError: public baseException
{
public:
underflowError(const string& msg = ""):
baseException(msg)
{}
};
// attempt to insert into a full container //溢出错误
class overflowError: public baseException
{
public:
overflowError(const string& msg = ""):
baseException(msg)
{}
};
// error in expression evaluation //表达式错误
class expressionError: public baseException
{
public:
expressionError(const string& msg = ""):
baseException(msg)
{}
};
// bad object reference //参数错误
class referenceError: public baseException
{
public:
referenceError(const string& msg = ""):
baseException(msg)
{}
};
// feature not implemented //不是执行错误
class notImplementedError: public baseException
{
public:
notImplementedError(const string& msg = ""):
baseException(msg)
{}
};
// date errors //日期错误
class dateError: public baseException
{
public:
dateError(const string& first, int v, const string& last):
baseException()
{
char dateStr[80];
ostrstream dateErr(dateStr, 80);
dateErr << first << ' ' << v << ' ' << last << ends;
// dateError can modify msgString, since it is in
// the protected section of baseException
msgString = dateStr;
}
};
// error in graph class//图错误
class graphError: public baseException
{
public:
graphError(const string& msg = ""):
baseException(msg)
{}
};
// file open error
class fileOpenError: public baseException
{
public:
fileOpenError(const string& fname):
baseException()
{
char errorStr[80];
ostrstream fileErr(errorStr, 80);
fileErr << "Cannot open \"" << fname << "\"" << ends;
// fileOpenError can modify msgString, since it is in
// the protected section of baseException
msgString = errorStr;
}
};
// error in graph class
class fileError: public baseException
{
public:
fileError(const string& msg = ""):
baseException(msg)
{}
};
#endif // EXCEPTION_CLASSES
//d_dynamicType.h
#include"d_except.h"
using namespace std;
template<typename T>
class dynamicType;
template<typename T>
ostream& operator<<(ostream& os,const dynamicType<T>& obj);
template<typename T>
class dynamicType
{
public:
dynamicType(const T& value)
{
ptr=new T(value);
cout<<"T构造函数执行"<<endl;
}
dynamicType(const dynamicType<T>& obj)
{
if(ptr==NULL)
throw memoryAllocationError("dynamicType类内存分配错误");
ptr=new T(*obj.ptr);//小括号中表达式相当于*(obj.ptr)
cout<<"T复制构造函数执行"<<endl;
}
~dynamicType()
{
delete ptr;
cout<<"T析构函数执行"<<endl;
}
dynamicType<T>& operator=(const dynamicType<T>& rhs)
{
*ptr=*rhs.ptr;
cout<<"T赋值运算符执行"<<endl;
return *this;
}
T getData()const
{
return *ptr;
}
friend ostream& operator<<(ostream& os,const dynamicType<T>& obj);
private:
T *ptr; //动态数据成员
};
template<typename T>
ostream& operator<<(ostream& os,const dynamicType<T>& obj)
{
os<<*obj.ptr;
return os;
}
// 11-40.cpp
#include<iostream>
#include<vector>
#include"d_dynamicType.h"
using namespace std;
template<typename T>
dynamicType<T> sum(const vector<dynamicType<T> >& v);
int main()
{
dynamicType<int> arr[5]={
dynamicType<int>(3),dynamicType<int>(5),dynamicType<int>(8),
dynamicType<int>(7),dynamicType<int>(2)};
vector<dynamicType<int> > v(arr,arr+5);
cout<<sum(v)<<endl; //operator<<函数不知错在哪?
return 0;
}
template<typename T>
dynamicType<T> sum(const vector<dynamicType<T> >& v)
{
T temp=T();
int i;
for(i=0;i!=v.size();i++)
temp+=v[i].getData();
return dynamicType<T>(temp);//用T类型的和初始化dynamicType<T>对象,作为返回值
}
编 译时出错:error LNK2001: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class dynamicType<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$dynamicType@H@@@Z)
fatal error LNK1120: 1 个无法解析的外部命令
请高师帮忙