我正在尝试使用 Eclipse 通过以下函数测试字符串输入是否合法C++:
#include <string>
bool testLegal::checkData(std::string &input, int ®) {
//test to see if pitchnames are legal
std::string toneClasses = "CDEFGAB";
std::string accidentals = "#b";
std::string toTest;
try {
//TEST 1 IS the pitch name legal?
toTest = input.substr(0, 1);
if (_isLegal(toTest, toneClasses) == false)
throw dataClean(2, "The pitch name you entered is illegal. Please correct it!");
//TEST 2 to see if there is a second character in the pitch name and whether it is a sharp or flat
toTest = input.substr(1);
if (input.length() == 2 && _isLegal(toTest, accidentals) == false)
throw dataClean(3, "The second character in the pitch name must be a sharp or flat");
} catch (const cExceptions& e) {
throw dataClean(4, "There is an error in the data you inputed. Please re-enter");
}
return true;
}
但是,我收到有关 .substr 参数的"无效参数"错误。我尝试了各种解决方案但没有成功。奇怪的是,该函数在Netbeans和Codelite中运行没有任何问题。我将不胜感激任何想法是如何解决这个问题的。期待中的感谢...
使用 NDK 时,我在 Eclipse 上遇到了类似的子字符串问题。我不得不将整数转换为字符串::size_type
string twoLetters = hexStr.substr((string::size_type)i,(string::size_type)2);
我看不到任何验证您的输入包含足够字符的检查:尝试获取超过字符串大小的子字符串可能会产生您引用的错误。同样,从无效索引开始也可能导致此问题。也就是说,我猜你实际上input
是空的。尝试添加:
if (input.empty()) {
throw std::runtime_error("received an empty string");
}
这就是我为 Eclipse CDT 修复它的方式。打开属性并进入路径和符号部分。如果C++代码的其余部分工作正常,您可能有某种行要包含C++头文件。例如,在我的Mac上是这样的:
/usr/include/c++/4.2.1
现在,添加一个包含"tr"子目录的新条目:
/usr/include/c++/4.2.1/tr
之后重新生成索引,您就完成了。