const string & as function parameter
Hi all,
I got a compiler error when using "const string&" as function parameter,
while using "const string" results in success. The following is my code:
////////////////////////////////////a.h
#include <string>
class A
{
public:
//...
bool func(const char* c);
bool func(const std::string& s);
//...
}
/////////////////////////////////a.cpp
#include "a.h"
#include <iostream>
using namespace std;
//...
bool A::func(const char* c)
{
//...
}
bool A::func(const string& s)
{
return func(s.c_str());
}
////////////////////////////////main.cpp
#include "a.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s= "...";
A a;
if (a.func(s)) //here is line 11
cout << "succeed" << endl;
else
cout << "failed" << endl;
cin.get();
return 0;
}
//////////////////////////////End of my code
g++ outputs :
main.cpp:11: undefined reference to `A::func(std::string)'
But if I change the definition and declaration of "bool func(const
std::string& s)" to "bool func(const std::string s)",
everything is ok.
Anyone can tell me why? THANKS VERY MUCH!
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]