视觉C++错误 C2451 条件字符串是非法的
visual C++ Error C2451 conditional String is illegal
我对C++相当陌生。 我想编写一个程序/函数来检查字符串输入(来自控制台或其他来源,在这里并不重要)是否已经在数组中。如果不是,那么它应该被写入数组。否则什么都不做。 我的问题是 for 循环和 if 条件。我错过了什么?
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
using namespace std;
typedef struct {
string id[10];
string foo1[10];
string type[10];
string func[10];
}Device;
int main() {
Device fooDevice;
string mystring;
int i = 0;
mystring = "foo";
ofstream temp;
temp.open("temp.txt", ios::out | ios::app);
for (fooDevice.id[i]; fooDevice.id[9]; i++) {
if (fooDevice.id[i] != mystring) {
fooDevice.id[i] = mystring;
temp << mystring << endl;
} else {
//do nothing
}
}
return 0;
}
问题在于for
循环的结构。我不确定你认为你的条件意味着什么,但它应该是这样的:
for (std::size_t i = 0; i < 10; ++i) {
这会将索引值i
从0
增加到9
(含)。然后,您可以检查fooDevice[i]
的值。
目前,您似乎正在尝试用新字符串覆盖数组的每个元素。我不确定你怎么知道数组在任何给定时间有多满。假设您在到达第一个空字符串时停止:
for (std::size_t i = 0; i < 10; ++i) {
if (myString == fooDevice.id[i]) {
// already there, stop looping
break;
}
else if (fooDevice.id[i].empty()) {
// none of the currently set elements matches
fooDevice.id[i] = myString;
temp << myString << 'n';
break;
}
}
您也可以将其替换为基于范围的for
:
for (auto& deviceId: fooDevice.id) {
if (myString == deviceId) {
// already there, stop looping
break;
}
else if (deviceId.empty()) {
// none of the currently set elements matches
deviceId = myString;
temp << myString << 'n';
break;
}
}
更好的是,使用带有<algorithm>
标头中std::find
的std::vector
。(警告:未经测试的代码):
struct Device {
// ...
std::vector<std::string> id;
// ...
};
// ...
auto foundId = std::find(fooDevice.id.begin(), fooDevice.id.end(), myString);
if (fooDevice.id.end() == foundId) {
// not already there
fooDevice.id.push_back(myString);
temp << myString << 'n';
}
您似乎对 C 和 C++ 之间的区别也有点困惑:
<stdio.h>
的C++版本是<cstdio>
,但您在这里根本不需要它(而且您通常不需要在C++程序中)。- 您无需
typedef
C++struct
的名称,只需struct Device { ... };
关于C++风格,请重新考虑您对通常被认为是不良做法的使用:using namespace std;
和endl
。
for (fooDevice.id[i]; fooDevice.id[9]; i++)
似乎没有光彩。我想你想要:
for(; i<9; ++i)
不能将string
值用作循环条件。仅计算结果为布尔值的表达式。
此外,您也不会在搜索设备阵列之前对其进行初始化。
尝试更多类似的东西:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Device {
string id[10];
string foo1[10];
string type[10];
string func[10];
};
int main() {
Device fooDevice;
string mystring;
// fill fooDevice as needed...
mystring = "foo";
ofstream temp;
temp.open("temp.txt", ios::out | ios::app);
bool found = false;
int idx_available = -1;
for (int i = 0; i < 10; ++i) {
if (fooDevice.id[i] == mystring) {
//do nothing
found = true;
break;
}
if ((idx_available == -1) && fooDevice.id[i].empty())
idx_available = i;
}
if ((!found) && (idx_available != -1)) {
fooDevice.id[idx_available] = mystring;
temp << mystring << endl;
}
return 0;
}
最好通过一些重写来处理:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
struct Device {
std::string id;
std::string foo1;
std::string type;
std::string func;
};
struct isDeviceId {
const std::string &m_id;
isDeviceId(const std::string &id) : m_id(id) {
}
bool operator()(const Device &device) {
return (device.id == m_id);
}
};
int main() {
std::vector<Device> devices;
std::string mystring;
// fill devices as needed...
mystring = "foo";
if (std::find_if(devices.begin(), devices.end(), isDeviceId(mystring)) == devices.end()) {
Device device;
device.id = mystring;
devices.push_back(device);
std::ofstream temp;
temp.open("temp.txt", std::ios::out | std::ios::app);
temp << mystring << std::endl;
}
return 0;
}
或者,在 C++11 及更高版本中:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
struct Device {
std::string id;
std::string foo1;
std::string type;
std::string func;
};
int main() {
std::vector<Device> devices;
std::string mystring;
// fill devices as needed...
mystring = "foo";
if (std::find_if(devices.begin(), devices.end(),
[mystring&](const Device &device) { return (device.id == mystring); }
) == devices.end()) {
devices.emplace_back();
devices.back().id = mystring;
// or, in C++17:
// devices.emplace_back().id = mystring;
std::ofstream temp;
temp.open("temp.txt", std::ios::out | std::ios::app);
temp << mystring << std::endl;
}
return 0;
}