重载函数 'decomposeSentence(const string&)' 的调用不明确

call of overloaded function 'decomposeSentence(const string&)' is ambiguous

我目前正在用 C++ 编写 NMEA 解析器,并且已经获得了一些预先声明的函数。我正在测试每个函数,但我在编程 decomposeSentence 函数时遇到了障碍。函数代码如下:

NMEAPair decomposeSentence(const string & nmeaSentence) {
    /* Extract each comma-separated value from the string, and place into an
     * array. */
    string newSentence = nmeaSentence;
    vector<string> sep = split(newSentence, ',');

    /* Extract the sentence type (e.g. GPGLL) from the vector, remove the "$",
     * and remove from the vector. */
    string sentenceType = sep[0];
    sentenceType.erase(sentenceType.begin());
    sep.erase(sep.begin());

    /* Extract the part of the vector containing the checksum, and discard the
     * checksum. Remove the part of the vector still containing the checksum,
     * and replace this with the newly created value. */
    string discardChecksum = sep.back();
    discardChecksum = discardChecksum.substr(0, discardChecksum.find("*"));
    sep.erase(sep.end());
    sep.push_back(discardChecksum);

    /* Combine the sentence type and the vector elements into a pair. */
    NMEAPair decomposedSentence = make_pair(sentenceType, sep);

    /* Return the decomposed sentence. */
    return decomposedSentence;
}

当我尝试 运行 main() 中的函数时,像这样...

int main() {
    const string nmeaSentence = "$GPGGA,091138.000,5320.4819,N,00136.3714,W,1,0,,395.0,M,,M,,*46";
    NMEAPair decomposedSentence = decomposeSentence(nmeaSentence);
}

...编译器抛出错误call of overloaded 'decomposeSentence(const string&)' is ambiguous'

我意识到这可能是一个很容易解决的问题,但如果有人能帮助我解决它,我将不胜感激。

编辑:完整错误消息

../gps/src/main.cpp:52:65: error: call of overloaded ‘decomposeSentence(const string&)’ is ambiguous
     NMEAPair decomposedSentence = decomposeSentence(nmeaSentence);
                                                                 ^
../gps/src/main.cpp:23:10: note: candidate: GPS::NMEAPair decomposeSentence(const string&)
 NMEAPair decomposeSentence(const string & nmeaSentence) {
          ^~~~~~~~~~~~~~~~~
In file included from ../gps/src/main.cpp:1:0:
../gps/headers/parseNMEA.h:47:12: note: candidate: GPS::NMEAPair GPS::decomposeSentence(const string&)
   NMEAPair decomposeSentence(const string & nmeaSentence);
            ^~~~~~~~~~~~~~~~~
make: *** [Makefile:721: main.o] Error 1
04:00:01: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project GPS (kit: Desktop)

听起来您的代码中某处有 using namespace GPS;。因此,GPS::decomposeSentence 和全局 decomposeSentence 被编译器选为潜在的重载。

您可以通过以下方式解决问题:

  1. 删除 using namespace GPS; 行,或
  2. 将全局函数放在另一个 namespace 中并从那个 namespace 中显式使用它。

namespace ThisFileNS
{
   NMEAPair decomposeSentence(const string & nmeaSentence) 
   {
      ...
   }
}

并使用

NMEAPair decomposedSentence = ThisFileNS::decomposeSentence(nmeaSentence);

main.