I created this small program using multiple files, I removed most of the class members for simplicity
Program Description:
Class User has a service member object
Service class contains one member function to perform string splitting
Class User uses the stringSplitter member function from Service class
main() is empty, I just wanted to test the program
I have the following error (when I try to build and run):
undefined reference to `Service::stringSplitter
How can I solve it?
Here is my code:
User.h
#ifndef USER_H_INCLUDED
#define USER_H_INCLUDED
#include <bits/stdc++.h>
#include "Service.h"
using namespace std;
class User
{
public:
User ();
User (const string &);
Service getService ();
private:
Service generalService;
};
#endif // USER_H_INCLUDED
User.cpp
#include <bits/stdc++.h>
#include "User.h"
using namespace std;
User :: User (const string &str)
{
vector <string> pieces = getService().stringSplitter (str, ',');
}
Service User :: getService ()
{
return generalService;
}
Service.h
#ifndef SERVICE_H
#define SERVICE_H
#include <bits/stdc++.h>
using namespace std;
class Service
{
public:
vector <string> stringSplitter (string s, char delimiter);
};
#endif // SERVICE_H
Service.cpp
#include <bits/stdc++.h>
#include "Service.h"
using namespace std;
vector <string> stringSplitter (string s, char delimiter)
{
vector <string> vec;
string x;
for (int i = 0; i < (int) s.length(); i++)
{
x += s[i];
if (s[i] == delimiter)
{
x.erase (x.length() - 1, 1);
vec.push_back (x);
x.clear();
}
}
return vec;
}