簡體   English   中英

>>對雙精度數組進行運算符?

[英]>> operator on array of doubles?

將數據輸入數組的問題。

我正在使用Dev 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]

錯誤消息看起來很可怕

[錯誤]'operator >>'不匹配(操作數類型為'std :: basic_istream :: __ istream_type {aka std :: basic_istream}'和'double [2]')

但這必不可少的意思是:

[錯誤]'operator >>'不匹配(操作數類型為'std :: ifstream'和'double [2]')

您將不得不將Z設為單個double inp >> Z[i]或者通過諸如inp >> Z[i]的索引訪問Z我無法知道程序中需要哪一個,因此您必須考慮或減少問題到極簡版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM