954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

setting size of 2-D array at constructor issue

I am trying to pass the size of the matrix array during the constructor but its giving me the following error. Is there a different way to do this?

In file included from Main.cpp:1:
AdjacencyMatrix.h: In constructor ‘AdjacencyMatrix::AdjacencyMatrix(int, std::string)’:
AdjacencyMatrix.h:13: error: ‘numberOfNodes’ cannot appear in a constant-expression

Line 13 is the constructor declaration.

Here is my code

#include<string>
#include<fstream>

using namespace std;

class AdjacencyMatrix {
private:
	bool* matrix;
public:
	AdjacencyMatrix(int const numberOfNodes, string fileName)
	{
		matrix = new bool[numberOfNodes[numberOfNodes];		//two dimensional array
		populateArray(fileName);
	}
	void populateArray(string filName);
};


Thanks in advance

chunalt787
Junior Poster in Training
84 posts since Apr 2008
Reputation Points: 39
Solved Threads: 1
 

I find that it compiles fine, after you take the excess [numberOfNodes .

I would think that removing the const keyword from your parameter list would clear the error you're getting.

FYI, you're not declaring or allocating a 2D array. bool* matrix; will be a 1D array only.

(using VC++ 2005)

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

o ok that makes sense. thanks

chunalt787
Junior Poster in Training
84 posts since Apr 2008
Reputation Points: 39
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You