943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1211
  • C++ RSS
Nov 24th, 2008
0

setting size of 2-D array at constructor issue

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  1. In file included from Main.cpp:1:
  2. AdjacencyMatrix.h: In constructor ‘AdjacencyMatrix::AdjacencyMatrix(int, std::string)’:
  3. AdjacencyMatrix.h:13: error: ‘numberOfNodes’ cannot appear in a constant-expression
Line 13 is the constructor declaration.

Here is my code
CPP Syntax (Toggle Plain Text)
  1. #include<string>
  2. #include<fstream>
  3.  
  4. using namespace std;
  5.  
  6. class AdjacencyMatrix {
  7. private:
  8. bool* matrix;
  9. public:
  10. AdjacencyMatrix(int const numberOfNodes, string fileName)
  11. {
  12. matrix = new bool[numberOfNodes[numberOfNodes]; //two dimensional array
  13. populateArray(fileName);
  14. }
  15. void populateArray(string filName);
  16. };

Thanks in advance
Similar Threads
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Nov 24th, 2008
0

Re: setting size of 2-D array at constructor issue

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)
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 24th, 2008
0

Re: setting size of 2-D array at constructor issue

o ok that makes sense. thanks
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Key input
Next Thread in C++ Forum Timeline: annother prolem with arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC