双打阵列上的>>运算符?
>> operator on array of doubles?
将数据
输入数组的问题。
我正在使用开发C++来执行和编译。下面是我的代码,问题出在哪里:
for (int k=0;k<=149;k++){ // k - scanning through Y within single file #i
for (int l=0;l<=199;l++){ // l - scanning through X within single file #i
inp >> X[l]>> Y[k]>> Z>> Mx[l][k]>> My[l][k]>> Mz[l][k];
Z
没有缺少下标,而是这样打算的。
完整错误代码:
[Error] no match for 'operator>>' (operand types are
'std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}' and 'double [2]')
欢迎任何建议,谢谢。
完整代码:
#pragma warning(disable:4786)
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
string filename(int filenumber);
int main () {
char *flnmptr;
string str,tmpr;
char flnm[50];
!!! make sure that the array dimensions for input data are right - that might screw the output files
dimensions are written in the 2nd header line of the input file
double X[150], Y[200], Z[2], Mx[150][200][2], My[150][200][2], Mz[100][100][2], time; // data from file
// double hX[5001], vY[5001], hMx[5001],hMy[5001],hMz[5001], vMx[5001], vMy[5001], vMz[5001]; // data to be written to file
//int xcnt, ycnt; // variable to count how many values will be written to a file
const int N=600; //number of files
//const int ptN=20; // number of points in the line
const double timestep=1e-14; // time step of files
const double X0=2.749313e-1; // X coordinate of line to read into vertical stripe output
const double Y0=-9.614638e-1; // Y coordinate of line to read into horizontal stripe output
ofstream outp("SWstripeCoupledCorner50difference10_120GHz.txt"); //opening output file
if (!outp){
cout << "Can't open file.n";
return 1;
}
cout<<"Reading file #: ";
outp.setf (ios::scientific ); // set the output format
for (int i=1;i<=N;i++){
std::strcpy(flnm,"");
tmpr=filename(i);
flnmptr=&tmpr[0];
std::strcpy(flnm,flnmptr);
ifstream inp;
inp.open(flnm); // opens the file
if(!inp) // if file couldn't be opened
{
cerr << "Error: file could not be opened" << endl;
exit(1);
}
std::getline(inp,str,'n');
std::getline(inp,str,'n');
inp.setf (ios::scientific);
for (int k=0;k<=149;k++){ // k - scanning through Y within single file #i
for (int l=0;l<=199;l++){ // l - scanning through X within single file #i
inp >> X[l]>> Y[k]>> Z>> Mx[l][k]>> My[l][k]>> Mz[l][k];
}
}
}
}
你说
Z
没有缺少下标,而是这样打算的。
但是在您的代码中Z
被声明为double[2]
。
错误消息看起来很可怕
[错误] 与"运算符>>"不匹配(操作数类型为 'std::basic_istream::__istream_type {aka std::basic_istream}' 和 'double [2]'(
但它必不可少意味着:
[错误] 与"运算符>>"不匹配(操作数类型为 "std::ifstream"和"double [2]"(
您要么必须Z
单个double
,要么通过索引访问Z
,例如inp >> Z[i]
我不可能知道您的程序中需要哪一个,因此这是您必须考虑的事情或将问题简化为简约版本。
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 是否需要使用 - &gt;运算符在C 中调用成员函数时