none
Error_1_error LNK2019: 无法解析的外部符号 RRS feed

  • 问题

  • 以下为程序代码:

    Relation.h

    --------------------------------------------------------------------

    #ifndef RELATION_H_
    #define RELATION_H_
    #include <iostream>
    #include <set>
    #include <string>
    #include <exception>
    #include <sstream>
    using namespace std;

    template<class A,class B>
    class Relation{
    private:
    string nom;
    string symbole;
    const set<A>* ptr_a;
    const set<B>* ptr_b;
    set<pair<A,B>> ensemble;
    public:
    Relation(const string& n,const string& s,const set<A>& source,const set<B>& destination);
    const string& get_nom()const{return nom;}
    const string& get_symbole()const{return symbole;}
    const set<A>* get_source()const{return ptr_a;}
    const set<B>* get_destination()const{return ptr_b;}
    void ajouter(const A& a,const B& b);
    bool est_en_relation(const A& a,const B& b);
    };

    template<class A,class B>
    class Relation_exception:public exception{
    private:
    A a;
    B b;
    public:
    Relation_exception(const A& source,const B& destination):a(source),b(destination){}
    const char* what();
    };

    template<class A,class B>
    ostream& operator<<(ostream& os,Relation<A,B>& r);

    #endif

    --------------------------------------------------------------------------------------------------------------------

    Relation.cpp

    #include "Relation.h"

    template<class A,class B>
    Relation<A,B>::Relation(const string& n,const string& s,const set<A>& source,
    const set<B>& destination):nom(n),symbole(s),ptr_a(&source),ptr_b(&destination){

    for (ptr_a::iterator it=ptr_a->begin();it!=ptr_a->end();it++)
    {
    for (ptr_b::iterator it2=ptr_b->begin();it2!=ptr_b->end();it2++)
    {
    ensemble.insert(pair<A,B>(*it,*it2));
    }
    }

    }

    template<class A,class B>
    void Relation<A,B>::ajouter(const A& a,const B& b){
    if (ptr_a->find(a)!=ptr_a->end()||ptr_b->find(b)!=ptr_b->end())
    {
    throw Relation_exception(a,b);
    }
    else{
    ensemble.insert(pair<A,B>(a,b));
    }
    }

    template<class A,class B>
    bool Relation<A,B>::est_en_relation(const A& a,const B& b){
    if (ensemble.find(pair<A,B>(a,b))==ensemble.end())
    {
    return false;
    }
    else{
    return true;
    }
    }

    template<class A,class B>
    const char* Relation_exception<A,B>::what(){
    stringstream ss;
    ss<<a;
    string temp = "The relation between "+ss.str();
    ss.clear();
    ss.str("");
    ss<<b;
    temp+="and"+ss.str()+"exist already\n";
    return temp.c_str();
    }

    template<class A,class B>
    ostream& operator<<(ostream& os,Relation<A,B>& r){
    os<<"Relation "<<r.get_nom()<<":"<<endl;
    for (r.ensemble::iterator it=r.ensemble.begin();it!=r.ensemble.end();it++){
    cout<<*it.first<<r.get_symbole()<<*it.second<<endl;
    }
    return os;
    }

    --------------------------------------------------------------------------------------------------------------

    main.cpp

    #include "Relation.h"

    void main(){
    set<int> s;
    s.insert(68);s.insert(72);s.insert(99);s.insert(123);
    set<string> d;
    d.insert("abs");d.insert("DSSD");d.insert("XBDR");d.insert("FCCD");
    Relation<int,string> r("Complex","X",s,d);
    try{
    cout<<r;
    }
    catch(Relation_exception<int,string>& e){
    cout<<e.what();
    }
    }

    错误代码:

    Error 2 error LNK1120: 1 个无法解析的外部命令 G:\code\Relation\Debug\Relation.exe Relation
    Error 1 error LNK2019: 无法解析的外部符号 "public: __thiscall Relation<int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::Relation<int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::set<int,struct std::less<int>,class std::allocator<int> > const &,class std::set<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > const &)" (??0?$Relation@HV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0ABV?$set@HU?$less@H@std@@V?$allocator@H@2@@2@ABV?$set@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@2@@Z),该符号在函数 _main 中被引用 G:\code\Relation\Relation\main.obj Relation

    麻烦各位指正,在此谢过。

    2012年10月29日 11:10

答案

  • #include "stdafx.h"
    #include <iostream>
    #include <set>
    #include <string>
    #include <exception>
    #include <sstream>
    using namespace std;
    
    template<class A,class B>
    class Relation{
    	friend ostream& operator<< <>(ostream& os,Relation<A,B>& r);
    private:
    	string nom;
    	string symbole;
    	const set<A>* ptr_a;
    	const set<B>* ptr_b;
    	set<pair<A,B>> ensemble;
    public:
    	Relation(const string& n,const string& s,const set<A>& source,const set<B>& destination);
    	const string& get_nom()const{return nom;}
    	const string& get_symbole()const{return symbole;}
    	const set<A>* get_source()const{return ptr_a;}
    	const set<B>* get_destination()const{return ptr_b;}
    	void ajouter(const A& a,const B& b);
    	bool est_en_relation(const A& a,const B& b);
    };
    
    template<class A,class B>
    class Relation_exception:public exception{
    private:
    	A a;
    	B b;
    public:
    	Relation_exception(const A& source,const B& destination):a(source),b(destination){}
    	const char* what();
    };
    
    template<class A,class B>
    Relation<A,B>::Relation(const string& n,const string& s,const set<A>& source,const set<B>& destination)
    :nom(n),symbole(s),ptr_a(&source),ptr_b(&destination)
    {
    	for (set<A>::const_iterator it=ptr_a->begin();it!=ptr_a->end();it++)
    	{
    		for (set<B>::const_iterator it2=ptr_b->begin();it2!=ptr_b->end();it2++)
    		{
    			ensemble.insert(pair<A,B>(*it,*it2));
    		}
    	} 
    }
    
    template<class A,class B>
    void Relation<A,B>::ajouter(const A& a,const B& b){
    	if (ptr_a->find(a)!=ptr_a->end()||ptr_b->find(b)!=ptr_b->end())
    	{
    		throw Relation_exception(a,b);
    	}
    	else{
    		ensemble.insert(pair<A,B>(a,b));
    	}
    }
    
    template<class A,class B>
    bool Relation<A,B>::est_en_relation(const A& a,const B& b){
    	if (ensemble.find(pair<A,B>(a,b))==ensemble.end())
    	{
    		return false;
    	}
    	else{
    		return true;
    	}
    }
    
    template<class A,class B>
    const char* Relation_exception<A,B>::what(){
    	stringstream ss;
    	ss<<a;
    	string temp = "The relation between "+ss.str();
    	ss.clear();
    	ss.str("");
    	ss<<b;
    	temp+="and"+ss.str()+"exist already\n";
    	return temp.c_str();
    }
    
    template<class A,class B>
    ostream& operator<< <>(ostream& os,Relation<A,B>& r)
    {
    	os<<"Relation "<<r.get_nom()<<":"<<endl;
    	for (set<pair<A,B>>::iterator it=r.ensemble.begin();it!=r.ensemble.end();it++)
    	{
    		os<<(*it).first<<r.get_symbole()<<(*it).second<<endl;
    	}
    	return os;
    }
    
    void main()
    {
    	set<int> s;
    	s.insert(68);s.insert(72);s.insert(99);s.insert(123);
    	set<string> d;
    	d.insert("abs");d.insert("DSSD");d.insert("XBDR");d.insert("FCCD");
    	Relation<int,string> r("Complex","X",s,d);
    	try
    	{
    		cout<<r;
    	}
    	catch(Relation_exception<int,string>& e)
    	{
    		cout<<e.what();
    	}
    } 
    

    修改了一下,放在一个.cpp文件中VS2008编译通过~

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2012年10月29日 14:15
    版主

全部回复

  • #include "stdafx.h"
    #include <iostream>
    #include <set>
    #include <string>
    #include <exception>
    #include <sstream>
    using namespace std;
    
    template<class A,class B>
    class Relation{
    	friend ostream& operator<< <>(ostream& os,Relation<A,B>& r);
    private:
    	string nom;
    	string symbole;
    	const set<A>* ptr_a;
    	const set<B>* ptr_b;
    	set<pair<A,B>> ensemble;
    public:
    	Relation(const string& n,const string& s,const set<A>& source,const set<B>& destination);
    	const string& get_nom()const{return nom;}
    	const string& get_symbole()const{return symbole;}
    	const set<A>* get_source()const{return ptr_a;}
    	const set<B>* get_destination()const{return ptr_b;}
    	void ajouter(const A& a,const B& b);
    	bool est_en_relation(const A& a,const B& b);
    };
    
    template<class A,class B>
    class Relation_exception:public exception{
    private:
    	A a;
    	B b;
    public:
    	Relation_exception(const A& source,const B& destination):a(source),b(destination){}
    	const char* what();
    };
    
    template<class A,class B>
    Relation<A,B>::Relation(const string& n,const string& s,const set<A>& source,const set<B>& destination)
    :nom(n),symbole(s),ptr_a(&source),ptr_b(&destination)
    {
    	for (set<A>::const_iterator it=ptr_a->begin();it!=ptr_a->end();it++)
    	{
    		for (set<B>::const_iterator it2=ptr_b->begin();it2!=ptr_b->end();it2++)
    		{
    			ensemble.insert(pair<A,B>(*it,*it2));
    		}
    	} 
    }
    
    template<class A,class B>
    void Relation<A,B>::ajouter(const A& a,const B& b){
    	if (ptr_a->find(a)!=ptr_a->end()||ptr_b->find(b)!=ptr_b->end())
    	{
    		throw Relation_exception(a,b);
    	}
    	else{
    		ensemble.insert(pair<A,B>(a,b));
    	}
    }
    
    template<class A,class B>
    bool Relation<A,B>::est_en_relation(const A& a,const B& b){
    	if (ensemble.find(pair<A,B>(a,b))==ensemble.end())
    	{
    		return false;
    	}
    	else{
    		return true;
    	}
    }
    
    template<class A,class B>
    const char* Relation_exception<A,B>::what(){
    	stringstream ss;
    	ss<<a;
    	string temp = "The relation between "+ss.str();
    	ss.clear();
    	ss.str("");
    	ss<<b;
    	temp+="and"+ss.str()+"exist already\n";
    	return temp.c_str();
    }
    
    template<class A,class B>
    ostream& operator<< <>(ostream& os,Relation<A,B>& r)
    {
    	os<<"Relation "<<r.get_nom()<<":"<<endl;
    	for (set<pair<A,B>>::iterator it=r.ensemble.begin();it!=r.ensemble.end();it++)
    	{
    		os<<(*it).first<<r.get_symbole()<<(*it).second<<endl;
    	}
    	return os;
    }
    
    void main()
    {
    	set<int> s;
    	s.insert(68);s.insert(72);s.insert(99);s.insert(123);
    	set<string> d;
    	d.insert("abs");d.insert("DSSD");d.insert("XBDR");d.insert("FCCD");
    	Relation<int,string> r("Complex","X",s,d);
    	try
    	{
    		cout<<r;
    	}
    	catch(Relation_exception<int,string>& e)
    	{
    		cout<<e.what();
    	}
    } 
    

    修改了一下,放在一个.cpp文件中VS2008编译通过~

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2012年10月29日 14:15
    版主
  • 感谢版主回答。

    请问template<class A,class B>

    ostream& operator<< <>(ostream& os,Relation<A,B>& r)中的"<>"是为了和template配合而弄的吗?我第一次弄模板类,不是很清楚......。

    另外,为什么一定要并成一个?而不是多个文件的形式?实际上,我将你的程序分开来方也会发生问题......,我当时编译的时候也是没问题的,当我要调试的时候才报错的。

    2012年10月29日 14:47