Jump to content

need help with multidimensional array in a class member C++

Pachuca

I'm trying to return a multidimensional array in a member class. It should be reading from my data file which contains 3 columns of 12 rows filled with double data type like -12.2 or 3 and so on. 

double lineType::readData(){
    double arrayA[12], arrayB[12], arrayC[12], retArray[12][3];
    int i = 0;
    ifstream inFile;
    inFile.open("data.txt");
    if(inFile.fail()){
        cout << "inFile failed to open.\n";
    }
    while(!inFile.eof()) {
        inFile >> arrayA[i];
        inFile >> arrayB[i];
        inFile >> arrayC[i];
        retArray[12][3] = {{arrayA[i]},{arrayB[i]},{arrayC[i]}};
        i++;
    }

    return retArray[12][3];

}

I'm getting this error when trying to complie: 

error: cannot convert ‘<brace-enclosed initializer list>’ to ‘double’ in assignment
         retArray[12][3] = {{arrayA[i]},{arrayB[i]},{arrayC[i]}};

Is this a syntax issue? I'm trying to return the multidimensional array so I can use the numbers from the data file to run math calculations by using other member classes. I was planning to return the multidimensional array than store it as a private value in the class itself. 

Link to comment
Share on other sites

Link to post
Share on other sites

Make an initialization of the class method  as an object to the class.

Main Rig

CPU: Ryzen 2700X 
Cooler: Corsair H150i PRO RGB 360mm Liquid Cooler
Motherboard: ASUS Crosshair VII Hero
RAM: 16GB (2x8) Trident Z RGB 3200MHZ
SSD: Samsung 960 EVO NVME SSD 1TB, Intel 1TB NVME

Graphics Card: Asus ROG Strix GTX 1080Ti OC

Case: Phanteks Evolv X
Power Supply: Corsair HX1000i Platinum-Rated

Radiator Fans: 3x Corsair ML120
Case Fans: 4x be quiet! Silent Wings 3

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Armakar said:

Make an initialization of the class method  as an object to the class.

ty

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Pachuca said:

ty

Can't tell if srs or not.

I'm just being a standard coder and replying with random phrases containing "class", "object" and "method". It's what every coder does when others ask for  help - reply with nonsensical shit.

Main Rig

CPU: Ryzen 2700X 
Cooler: Corsair H150i PRO RGB 360mm Liquid Cooler
Motherboard: ASUS Crosshair VII Hero
RAM: 16GB (2x8) Trident Z RGB 3200MHZ
SSD: Samsung 960 EVO NVME SSD 1TB, Intel 1TB NVME

Graphics Card: Asus ROG Strix GTX 1080Ti OC

Case: Phanteks Evolv X
Power Supply: Corsair HX1000i Platinum-Rated

Radiator Fans: 3x Corsair ML120
Case Fans: 4x be quiet! Silent Wings 3

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Armakar said:

Can't tell if srs or not.

I'm just being a standard coder and replying with random phrases containing "class", "object" and "method". It's what every coder does when others ask for  help - reply with nonsensical shit.

being serious, I'm thinking it might work if i initialize the member function. If not, I could try changing this member class to only open/read the data.txt file than call it in another member class to store the data. Might be easier that way too.

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Armakar said:

Make an initialization of the class method  as an object to the class.

this is what I tried... doesn't seem to fix anything though. I'm still getting the same error.

lineType::lineType(){
    lineFromDataA[12];
    lineFromDataB[12];
    lineFromDataC[12];
}

double lineType::readData(){
    double retArray[12][3];
    int i = 0;
    ifstream inFile;
    inFile.open("data.txt");
    if(inFile.fail()){
        cout << "inFile failed to open.\n";
    }
    while(!inFile.eof()) {
        inFile >> lineFromDataA[i];
        inFile >> lineFromDataB[i];
        inFile >> lineFromDataC[i];
        retArray[12][3] = {{lineFromDataA[i]},{lineFromDataB[i]},{lineFromDataC[i]}};
        i++;
    }

    return retArray[12][3];

}

 

Link to comment
Share on other sites

Link to post
Share on other sites

I don't know much C++, but 

error: cannot convert ‘<brace-enclosed initializer list>’ to ‘double’ in assignment
         retArray[12][3] = {{arrayA[i]},{arrayB[i]},{arrayC[i]}};

means one of two things:

A)Wherever your converting or setting the parameters to double is in code that's never triggered. Check the code is called, use testing methods or basic output to determine if any code in a control statement is being ran, etc. 

B) The code where the parameters are changed/initialized again isn't triggered. See above.


I know that's a basic solution, if not, I'd try creating this on a smaller scale to see if your methodology is correct ,for example try it with a single array instead. You could also try using a list?


I don't know C++ so I can't directly help you, but seeing as nobody replied that's what I'd do if it was my code.

Main Rig

CPU: Ryzen 2700X 
Cooler: Corsair H150i PRO RGB 360mm Liquid Cooler
Motherboard: ASUS Crosshair VII Hero
RAM: 16GB (2x8) Trident Z RGB 3200MHZ
SSD: Samsung 960 EVO NVME SSD 1TB, Intel 1TB NVME

Graphics Card: Asus ROG Strix GTX 1080Ti OC

Case: Phanteks Evolv X
Power Supply: Corsair HX1000i Platinum-Rated

Radiator Fans: 3x Corsair ML120
Case Fans: 4x be quiet! Silent Wings 3

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is that you're trying to assign the array

{{arrayA[i]},{arrayB[i]},{arrayC[i]}}

to a single element of the array (specifically [12][3], which is also out of the bounds of the array). If you get rid of the [12][3] and just assign it to retArray, it still won't work though, because you're now just trying to assign an array with dimensions [3][1] to an array of dimensions [12][3] (and I also don't think you can use array literals like that in C++ though I might be wrong on that).

I don't quite understand why you have arrayA, arrayB and arrayC - they are just used for temporary storage. If they aren't used for anything else, you can make the code much simpler by replacing the body of your loop with

        inFile >> retArray[i][0];
        inFile >> retArray[i][1];
        inFile >> retArray[i][2];
        i++;

I'm not 100% sure that this will read the columns correctly though - I haven't used ifstream for a long time.

Make sure that your file isn't more than 12 lines long, otherwise your code won't work (because it will go out of the bounds of the array).

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

23 minutes ago, colonel_mortis said:

The problem is that you're trying to assign the array


{{arrayA[i]},{arrayB[i]},{arrayC[i]}}

to a single element of the array (specifically [12][3], which is also out of the bounds of the array). If you get rid of the [12][3] and just assign it to retArray, it still won't work though, because you're now just trying to assign an array with dimensions [3][1] to an array of dimensions [12][3] (and I also don't think you can use array literals like that in C++ though I might be wrong on that).

I don't quite understand why you have arrayA, arrayB and arrayC - they are just used for temporary storage. If they aren't used for anything else, you can make the code much simpler by replacing the body of your loop with


        inFile >> retArray[i][0];
        inFile >> retArray[i][1];
        inFile >> retArray[i][2];
        i++;

I'm not 100% sure that this will read the columns correctly though - I haven#t used ifstream for a long time.

Make sure that your file isn't more than 12 lines long, otherwise your code won't work (because it will go out of the bounds of the array).

This works for me, thank you. This is actually better than what I was trying to do. 

 

edit: i was trying to store these values so I can have the class return one array to use in another member class.

Link to comment
Share on other sites

Link to post
Share on other sites

To me it makes no sense to have a method read a file for data in this case. A separate function should be just fine and is more general.

Here's something I might write:

#include <iostream>
#include <stdexcept>
#include <fstream>
#include <vector>
using namespace std;

struct Row {
	double x;
	double y;
	double z;
};

vector<Row> read_data(const string &path) {
	vector<Row> rows;
	ifstream in(path, ios_base::in);
	if(!in.is_open()) {
		throw runtime_error("Could not open");
	}
	while(!in.eof()) {
		Row row;
		in >> row.x;
		in >> row.y;
		in >> row.z;
		rows.push_back(row);
	}
	return rows;
}

int main()
{
	try {
		auto data = read_data("data.txt");
		for(auto& row : data)
		{
			cout << row.x << ", "
			     << row.y << ", "
			     << row.z << "\n";
		}
	}
	catch(runtime_error &e)
	{
		cout << e.what() << "\n";
	}
	return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×