我目前正在使用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)
答案 0 :(得分:0)
听起来你的代码中有using namespace GPS;
。因此,GPS::decomposeSentence
和全局decomposeSentence
被编译器选为潜在的重载。
您可以通过以下方式解决问题:
using namespace GPS;
行或namespace
并明确使用namespace
。namespace ThisFileNS
{
NMEAPair decomposeSentence(const string & nmeaSentence)
{
...
}
}
并使用
NMEAPair decomposedSentence = ThisFileNS::decomposeSentence(nmeaSentence);
main
中的。