From: weii on
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(a)netfront.net ---

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: SG on
On 3 Mrz., 10:06, weii wrote:
> 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:

Apart from a missing semicolon I don't see anything wrong with it. I
managed to compile it: http://codepad.org/CknNuB9t

> 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?

You probably overlooked something.

Cheers,
SG


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]