error LNK2001: 无法解析的外部符号

YUSHUIHE 2015-09-17 09:31:38
代码如下:
//异常类代码 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 个无法解析的外部命令
请高师帮忙
...全文
223 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ztenv 版主 2015-09-18
  • 打赏
  • 举报
回复
把模板参去掉,写上固定的类型,看会不会出错
YUSHUIHE 2015-09-18
  • 打赏
  • 举报
回复
按3楼的老师指教的,修改后,顺利通过编译和运行,谢谢。还问一下老师:是不是重载运算符函数的运算符后边都得加空参数表<>啊?
iyomumx 2015-09-18
  • 打赏
  • 举报
回复
friend ostream& operator<<(ostream& os,const dynamicType<T>& obj);
改成
friend ostream& operator<< <>(ostream& os,const dynamicType<T>& obj);
liubo_1993 2015-09-18
  • 打赏
  • 举报
回复
应该是<<重载写错了

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧