DEV-C++中 [Linker error] undefined reference to `void consumme<std::string>(buffer<std::string>&, std::string, std::string&)'
我用的DEV-C++ 4.9.2
-----------------------------main.cpp------------------------------
#include <cstdlib>
#include <iostream>
#include <string>
#include "buffer.h "
using std::cout;
using std::endl;
using std::string;
template <class T>
void consumme(buffer <T> &destination, string dest_name, T &fetched_product);
int main(void)
{
buffer <string> share_buf(20);
string fetched_product;
consumme(share_buf, "share_buf ", fetched_product);
system( "PAUSE ");
return EXIT_SUCCESS;
}
---------------------------consummer.cpp--------------------------------
#include "buffer.h "
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
template <class T>
void consumme(buffer <T> & destination, string dest_name, T &fetched_product)
{
int print1 = 0;
int print2 = 0;
while(destination.buf_is_empty())
{
if (print1 == 0)
{
print1 = 1;
cout < < "the " < < dest_name < < " is empty, so the consummer must wait... " < < endl;
}
}
while(destination.buf_is_locked())
{
if (print2 == 0)
{
print2 = 1;
cout < < "the " < < dest_name < < " is unaccessiable now, so the producer must wait... " < < endl;
}
}
destination.lock_buf();
if(destination.fetch_data(fetched_product));
else
cout < < "unpredictable fetch data failure! " < < endl;
destination.unlock_buf();
}
----------------------------------buffer.h-------------------------------
#ifndef BUFFER_MODEL_CLASS
#define BUFFER_MODEL_CLASS
enum swit_stat{opened, closed};
const unsigned default_size = 50; //设公共缓冲区的默认容量为50
template <class T>
class buffer
{
public:
buffer(void);
buffer(unsigned);
bool buf_is_full(void);
bool buf_is_empty(void);
void lock_buf(void);
void unlock_buf(void);
bool is_locked(void);
bool put_data(T);
bool fetch_data(T &);
~buffer()
{delete []buf_pt;}
private:
swit_stat mutex;
unsigned maxsize; //实际最大可容纳的数据个数
unsigned in; //放数据的位置
unsigned out_prior; //取数据位置的前一个位置,也就是说每次取数据,应该取out_prior前面的那一个,而它本身所指的那个位置永远都不放数据。
T *buf_pt;
};
/*--模板类实现--*/
template <class T>
buffer <T> ::buffer(void)
{
buf_pt = new T[default_size + 1];
maxsize = default_size;
in = 1;
out_prior = 0;
mutex = opened;
}
template <class T>
buffer <T> ::buffer(unsigned n)
{
buf_pt = new T[n];
maxsize = n;
in = 1;
out_prior = 0;
mutex = opened;
}
template <class T>
bool buffer <T> ::buf_is_full(void)
{return (in == out_prior);}
template <class T>
bool buffer <T> ::buf_is_empty(void)
{return (in == (out_prior + 1)%(maxsize + 1));}
template <class T>
void buffer <T> ::lock_buf(void)
{
mutex = closed;
}
template <class T>
void buffer <T> ::unlock_buf(void)
{
mutex = opened;
}
template <class T>
bool buffer <T> ::is_locked(void)
{return (mutex == closed);}
template <class T>
bool buffer <T> ::put_data(T data)
{
if (buf_is_full())
return false;
else
{
buf_pt[in] = data;
in = (in + 1) % (maxsize + 1);
return true;
}
}
template <class T>
bool buffer <T> ::fetch_data(T & fetched_data)
{
if (buf_is_empty())
return false;
else
{
fetched_data = buf_pt[out_prior + 1];
out_prior = (out_prior + 1) % (maxsize + 1);
return true;
}
}
#endif //BUFFER_MODEL_CLASS
[Linker error] undefined reference to `void consumme <std::string> (buffer <std::string> &, std::string, std::string&) '
ld returned 1 exit status
在网上查了一下,估计是类模板的问题,但仍然不能定位。哪位能解答一下,谢谢!
[解决办法]
标准规定模板申明定义要在一起。也就说每个申明后要跟上实现
[解决办法]
1 显式实例化声明所在的文本中,函数模板的定义必须首先被给出。
如果把函数模板定义放在.h中,那么就会在#include的地方展开,这样就没有任何问题。
2 不要对编译器的分离编译模式抱太大的希望。
你那几行代码也算多?没见STL代码全部组织在头文件中的。你可以分下类,多写几个.h。
如果你非要按你的做,可以在.h中#include *.cpp来实现,加上预处理。
[解决办法]
给楼主一个投机取巧的办法: 不用大手术,只需在consummer.cpp后面加上如下几行:
void fun()
{
string s;
consumme(buffer <string> (), "aaa ", s);
}
fan()不会被调用,它的唯一用处是告诉编译器要用到consumme <buffer <String> >
另外,楼主的consummer.cpp中有一个笔误:
while(destination.buf_is_locked())
似应为
while(destination.is_locked())