调用重载函数&decomposeSentence(const string&)'很暧昧

时间:2018-02-28 04:06:04

标签: c++ nmea

我目前正在使用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)

1 个答案:

答案 0 :(得分:0)

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

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

  1. 删除using namespace GPS;行或
  2. 将全局函数放入另一个namespace并明确使用namespace
  3. namespace ThisFileNS
    {
       NMEAPair decomposeSentence(const string & nmeaSentence) 
       {
          ...
       }
    }
    

    并使用

    NMEAPair decomposedSentence = ThisFileNS::decomposeSentence(nmeaSentence);
    
    main中的