0

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;
}
  • 2
    Needs to be `Service::stringSplitter` in Service.cpp – TheUndeadFish Sep 11 '20 at 18:51
  • 1
    Woe, woe and thrice woe. Do not use `#include `, especially with `using namespace std`, and even more so in a header file. It is non-portable and puts the entire standard library into the global namespace, which can only lead to trouble somewhere down the line. Instead, #include just the headers you need, no more and no less, and qualify names in the standard library with `std::` in your code. – Paul Sanders Sep 11 '20 at 18:54

0 Answers0