Cplex C++ 音乐会技术中的多维数组
multi dimensional array in cplex c++ concert tech
我正在尝试在c ++音乐会技术中创建一个布尔矩阵。我已经定义了 typedef IloArray<IloBoolArray> BoolMatrix;
之后我宣布:BoolMatrix Assigned(env)
; 但是在尝试按 file >> Assigned
输入数据时,它显示错误(没有运算符">>"匹配这些操作数)。请帮助我摆脱这个错误。谢谢
大多数情况下,这意味着您忘记在类中使用传递参数类型声明运算符">>",或者您正在使用的库类不支持这些参数的此类操作。你能更具体地回答你的问题吗?
错误告诉您的是,编译器无法找到任何带有您为其提供的参数的operator>>(Type1 LHS, Type2 RHS)
定义。 Type1
可能是一个std::istream
,Type2
是BoolMatrix
。
为了允许你尝试做的事情,你需要定义你自己的operator<<
实现,它接受你给它的类型,并对这些类型进行操作。函数原型可能如下所示:
void operator<<(std::istream& file, BoolMatrix& matrix) {
// Read in the values from the stream and add them to matrix
}
此外,从 CPLEX 文档中,它说:
模板运算符>>可以从格式为 [x, y, z, ...] 的文件中读取数值,其中 x、y、z 是类 X 的运算符>>的结果。类 X 必须提供默认构造函数,运算符>>才能工作。也就是说,语句 X x;必须为 X 工作。此输入运算符仅限于数值。
你有两个IloArrayX
数组,其中 X 是外部数组的布尔数组,bool
是内部数组的布尔数组。您可以使用流从文件中读取布尔值,将它们添加到内部布尔数组 (IloBoolArray
),然后将内部IloBoolArray
添加到IloArray
中。
此外,从我在网上找到的一些代码示例中,您似乎需要初始化每个IloBoolArray
,然后再添加到它。
以下是我理解的将起作用的示例(这是未经测试的):
typedef IloArray<IloBoolArray>> BoolMatrix;
static constexpr int totalRows = 8; // Total IloBoolArrays
static constexpr int rowElements = 4; // Total elements in each bool array
int main() {
// Create the bool Matrix
BoolMatrix Assigned(env);
bool streamInputVar; // Temp variable to read from stream
const char* inputFilename = "input.txt"; // File to get data from
std::ifstream inputFile(inputFileName, ios::in); // Try and open file
if (!inputFile)
// Error for failing to open file
// File open, so read stream into Matrix
for (int i = 0; i < totalRows; ++i) {
Assigned.add(IlBoolArray(env)); // Add an IloBoolArray to BoolMatrix
for (int j = 0; j < rowElements; ++j) {
if (!(inputFile >> streamInputVar)) // Check that the stream is okay
// Error for bad stream
else
Assigned[i].add(streamInputVar);
}
}
}
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '