Class function with string parameter
May 27, 2009 at 9:52am UTC
Hi, I'm trying to send a string to a class function.
tmpClass.h:
1 2 3 4 5 6 7 8 9
#include <string>
using namespace std;
class Tmp
{
public :
void pointEasy(string bla);
};
tmpClass.cxx:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
using namespace std;
class Tmp
{
void pointEasy(string bla)
{
cout << "În pointEasy, bla:" << bla << "\n" ;
}
};
tmpMain.cxx:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
#include "tmpClass.h"
using namespace std;
int main()
{
Tmp tmp;
string input;
cin >> input;
tmp.pointEasy(input);
return 0;
}
When compiling, g++ spits this:
1 2 3
/tmp/ccYfAG3M.o: In function `main':
tmpMain.cxx:(.text+0x1be): undefined reference to `Tmp::pointEasy(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
Why does the compiler stumble like this? If bla is of string type, and I pass a string to pointEasy, what am I doing wrong?
May 27, 2009 at 1:02pm UTC
Your tmpClass.cxx file should be like this:
1 2 3 4 5 6 7 8 9
#include <iostream>
#include <string>
#include "tmpClass.h"
using namespace std;
void Tmp::pointEasy(string bla)
{
cout << "În pointEasy, bla:" << bla << "\n" ;
}
May 27, 2009 at 1:05pm UTC
That's not how you define a member function of a class. Try this:
tmpClass.cxx:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <string>
#include "tmpClass.h"
using namespace std;
void Tmp:: pointEasy(string bla)
{
cout << "În pointEasy, bla:" << bla << "\n" ;
}
May 27, 2009 at 1:05pm UTC
Dammit, too slow :)
May 27, 2009 at 1:08pm UTC
Argh.. sorry guys, it's been a while since I wrote c++ code... guess I need to skimp over a tutorial again.
Topic archived. No new replies allowed.