使用指针编辑二维数组

问题描述 投票:0回答:1

嗨,当我尝试使用指针为数组赋值时,我收到一条错误,指出 float[int] 不是有效类型

下面是我的代码,它需要一个包含

的文本文件
1 0.005
2 0.025
3 0.01
#include <iostream>
#include <fstream>
using namespace std;
string filename;

int number_of_cells(string filename);
void read(string filename,int i,float *id_area);

int id;     //int coloumn;
float area; //double tarea;

int main()
{
    string file_name="id-area-3.txt";
    int ncell = number_of_cells(file_name);
    cout<<" Number of lines are "<<ncell<<"\n";
    float id_area[ncell][2];
    read(file_name,ncell,*id_area);

}

int number_of_cells(string filename)
{
    int it=0;
    ifstream theFile(filename);
    while(theFile>>id>>area)
    {
        it++;
    }
    return(it);
}


void read(string filename,int i,float *id_area)
{
    ifstream theFile(filename);
    int j=0;
    while (theFile >> id >> area)

    {

        cout<<" ID:" << id << " , " << " area: " <<area << "|"<<"\n";
        for (j=1;j<i;j++)
        {
            id_area[j][0]=id;
            id_area[j][1]=area;
            j++;
        }

    }

    //cout<<typeid(arr).name();
}





以下是我的错误:

====================[ Build | Laser_ablation_nanoparticles | Debug ]============
"C:\Program Files\JetBrains\CLion 2023.1.5\bin\cmake\win\x64\bin\cmake.exe" --build C:\Users\Vishal\CLionProjects\Laser_ablation_nanoparticles\cmake-build-debug --target Laser_ablation_nanoparticles -j 6
[1/2] Building CXX object CMakeFiles/Laser_ablation_nanoparticles.dir/main.cpp.obj
FAILED: CMakeFiles/Laser_ablation_nanoparticles.dir/main.cpp.obj 
C:\PROGRA~1\JETBRA~1\CLION2~1.5\bin\mingw\bin\G__~1.EXE  -I"C:/Users/Vishal/Documents/Academic documents/MSc/Manchester/dissertation/Eigen/eigen-3.4.0" -g -fdiagnostics-color=always -std=gnu++23 -MD -MT CMakeFiles/Laser_ablation_nanoparticles.dir/main.cpp.obj -MF CMakeFiles\Laser_ablation_nanoparticles.dir\main.cpp.obj.d -o CMakeFiles/Laser_ablation_nanoparticles.dir/main.cpp.obj -c C:/Users/Vishal/CLionProjects/Laser_ablation_nanoparticles/main.cpp
C:/Users/Vishal/CLionProjects/Laser_ablation_nanoparticles/main.cpp: In function 'void read(std::string, int, float*)':
C:/Users/Vishal/CLionProjects/Laser_ablation_nanoparticles/main.cpp:45:**23: error: invalid types 'float[int]' for array subscript
   45 |             id_area[j][0]=id;
      |                       ^
C:/Users/Vishal/CLionProjects/Laser_ablation_nanoparticles/main.cpp:46:23: error: invalid types 'float[int]' for array subscript
   46 |             id_area[j][1]=area;
      |                       ^**
ninja: build stopped: subcommand failed.
c++ arrays pointers
1个回答
0
投票

在 C++ 中,我希望你的代码看起来像这样: 注意我使用 stringstream 来模仿您的文件(这样更容易测试)

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

struct cell_t
{
    int id;
    float area;
};

auto load(std::istream& is)
{
    std::vector<cell_t> cells;
    int id;
    float area;
    while ( is >> id >> area )
    {
        cells.push_back(cell_t{id,area});
    }
    return cells;
}


int main()
{
    std::istringstream is {"1 0.005 2 0.025 3 0.01"};
    //std::ifsteam is{"id-area-3.txt"}

    auto cells = load(is);
    for(const auto& cell : cells)
    {
        std::cout << "Cell id = " << cell.id << ", area = " << cell.area <<"\n";
    }

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.