several link problems
These problems is driving me crazy...
1. I am confused,isn't #ifndef suppose to guard against multiple definition?
2. no matter where the ~Query friend function is (in Query.h or Ouery.cpp),the compiler always complain the same error--error 2019 and 2001
what problems I can't see,please help me,thank you very much.
1>Query.obj : error LNK2005: "class Query __cdecl operator~(class Query const &)" (??S@YA?AVQuery@@ABV0@@Z) already defined in main.obj
1>Query.obj : error LNK2005: "class Query __cdecl operator&(class Query const &,class Query const &)" (??I@YA?AVQuery@@ABV0@0@Z) already defined in main.obj
1>Query.obj : error LNK2005: "class Query __cdecl operator|(class Query const &,class Query const &)" (??U@YA?AVQuery@@ABV0@0@Z) already defined in main.obj
1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Query_Base::~Query_Base(void)" (??1Query_Base@@UAE@XZ) referenced in function __unwindfunclet$??0NotQuery@@AAE@VQuery@@@Z$0
1>Query.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall Query_Base::~Query_Base(void)" (??1Query_Base@@UAE@XZ)
1>E:\C语言\My project\C++ Project\inherit class\inherit class\Debug\inherit class.exe : fatal error LNK1120: 1 unresolved externals
All Replies
- Hello,
The #ifndef/#ifdef directive checks if a definition exists. It does not guard against multiple definition. Take the code snippet as below for an example:
#ifndef identifier
cout << "we did not define identifier" << endl; // instruction one
#else
cout << "we defined identifier" << endl; // instruction two
#endif
if we defined identifier either in the .h file or in our .cpp file, instruction one will be executed, if we did not define it or we undefine it using #undef directive, instruction two will be executed.
For more information about the #ifndef/#ifdef directive, please refer to:
http://msdn.microsoft.com/en-us/library/2a1b21sf(VS.80).aspx
As for the second issue, the error here indicates that the compiler could not find the startup function. We could go to the Property page and set the Subsystem option to Console if it is really a console project, or else, please tell us more information about your project.
Best regards,
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback, please tell us.
Welcome to the All-In-One Code Framework! - I use the null project. Following is my code:
#ifndef QUERY_H #define QUERY_H #include<iostream> #include<string> #include<vector> #include<set> #include<map> #include"TextQuery.h" using namespace std; class Query_Base { friend class Query; public: typedef TextQuery::line_no line_no; ~Query_Base(); // error link2019,link2001 virtual set<line_no> eval(const TextQuery &) const=0; virtual ostream& display(ostream& =cout) const=0; }; class WordQuery:public Query_Base { friend class Query; WordQuery(const string& s) { word=s; } set<line_no> eval(const TextQuery& file)const { return file.runQuery(word); } ostream& display(ostream &os) const { return os<<word; } string word; }; class Query { friend Query operator ~(const Query& ) ; friend Query operator &(const Query&,const Query& ); friend Query operator |(const Query&,const Query& ) ; friend ostream& operator<<(ostream& os,const Query& ); public: Query(const string &s):q(new WordQuery(s)),use(new size_t(1)){} Query(const Query& c):q(c.q),use(c.use) { ++*use; } ~Query() { desr_use(); } Query &operator=(const Query &); set<TextQuery::line_no> eval(const TextQuery &file)const { return q->eval(file); } ostream& display(ostream &os)const { return q->display(os); } private: Query (Query_Base *c):q(c){} Query_Base *q; size_t *use; void desr_use() { delete q; delete use; } }; class NotQuery:public Query_Base { friend Query operator~(const Query&); set<TextQuery::line_no> real_line; Query qn; NotQuery( Query l):qn(l){} set<line_no> eval(const TextQuery &)const; ostream &display(ostream& os) const { return os<<"( ~ "<<qn<<" )"; } }; class BinaryQuery:public Query_Base { friend class Query; protected: const Query lhs,rhs; const string op; BinaryQuery(const Query& l,const Query& r,const string oper):lhs(l),rhs(r),op(oper){} ostream& display(ostream &os) const { return os<<" ( "<<lhs<<" "<<op<<" "<<rhs<<" )"; } }; class AndQuery:public BinaryQuery { friend Query operator &(const Query&,const Query& ) ; set<line_no> real_line; AndQuery(const Query &l,const Query &r):BinaryQuery(l,r,"&"){} set<line_no> eval(const TextQuery &)const; }; class OrQuery:public BinaryQuery { friend Query operator |(const Query&,const Query& ) ; set<line_no> real_line; OrQuery(const Query &l,const Query &r):BinaryQuery(l,r,"|"){} set<line_no> eval(const TextQuery &t)const; }; #endif
I think the second problem's reason is that When using C++, including a function prototype in a class definition and failing to include the implementation of the function for that class can cause LNK2001.
The destructor ~Query_Base() should be ~Query_Base(){} ,I don't include the implementation of the Query_Base function. - Hello,
Thanks for your feedback. And yes, if we did not implement our methods but called them in our program, the error LNK2001 will appear. How bout the link errors now? Do they still occur?
Best regards,
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback, please tell us.
Welcome to the All-In-One Code Framework! - Hello,
Beacause I had an exam on Turtshday,so I did not have time to surf the internet.
The error LNK2001 dispeared after I modified the function --Ouery_Base.
But the link2005 also exist. - when I remove the segment of the code ------
the implement of friend funtion from .h into the .cpp,then the error lnk2005 disapeared,but why?


