-2

So GCC keeps spouting out this error that keeps boggling my mind

undefined reference to 'GPS::isValidSentence(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

my header file parseNMEA.h is as follows:

#ifndef PARSENMEA_H_211217
#define PARSENMEA_H_211217

#include <string>
#include <list>
#include <vector>
#include <utility>

#include "position.h"

namespace GPS
{
  using std::string;
  using std::vector;
  using std::pair;

  bool isValidSentence(const string &);
}
#endif

and my source file parseNMEA.cpp is:

#include "parseNMEA.h"

using namespace GPS;

bool isValidSentence(const string &n)
{
  string test = n;
  bool result;
  if (test.find("$GP") != string::npos)    //Find the prefix
  {
      if (test.size() > 5)         //Check if it has 3 char identifier
      {
          //check if it has ',' after identifier
          if (test[6] == ',')
          {
              //check for a '*'
              if(test.find("*",7))
              {
                result = true;
              }
          }
      }

  }
  else
      result = false;
  return result;
}

Any ideas as to whats going on? I've tried bool isValidSentence (const string &n) in the header file but the same error code

Lucky38i
  • 125
  • 1
  • 16
  • Show your compile command. What do you compile with what flags? – erenon Feb 13 '18 at 12:06
  • 1
    And after you read your answer, maybe then you will finally understand why `using namespace`, of any kind, [is considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), at least not until one has some experience under the belt. – Sam Varshavchik Feb 13 '18 at 12:08
  • From the linked question; https://stackoverflow.com/a/12574407/3747990, in particular the piece related to "A common mistake is forgetting to qualify the name" – Niall Feb 13 '18 at 12:09

2 Answers2

4

using namespace GPS; only allows you to use names from that namespace as though they were declared in the current scope. But that use does not extend to defining them. So the function definition must be

bool GPS::isValidSentence(const string &n)

If that doesn't appeal to you, you can wrap the content of the definitions you put inside the cpp file in namespace GPS { } as well.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
0

In the source file, when you do

using namespace GPS;

you pull the symbols from the GPS namespace into the current (global) namespace. But it doesn't actually "use" the namespace. When you define the isValidSentence function, it is defined in the global namespace, not in the GPS namespace.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621