Initializing and declaring arrays in classes

This is a discussion on Initializing and declaring arrays in classes within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm picking up C++ by translating java code to C++. Currently I'm working on translating dijkstra's algorithm. I have ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    Initializing and declaring arrays in classes

    Hey, I'm picking up C++ by translating java code to C++. Currently I'm working on translating dijkstra's algorithm. I have the completed code in java and it works perfectly. I need some help with the C++ syntax. I'm not going to paste the whole code, this is a part of what I have:

    Code:
    class Graph
    {
    	final int MAX_VERTS = 20;
    	final int INFINITY = 1000000;
    	Vertex vertexList[];                      //list of vertices
    	int adjMat[][];                           //adjacency matrix
    
    	int nVerts;                               //current number of vertices
    	int nTree;                                //number of vertices in tree
    	DistPar sPath[];                          //array for shortest-path data
    	int currentVert;                          //current vertex
    	int startToCurrent;                       //distance to currentVert
    
    	//-----------------------------------------------------------------------------------
    
    	public Graph()                                    //constructor
    	{
    		vertexList = new Vertex[MAX_VERTS];
    		adjMat = new int[MAX_VERTS][MAX_VERTS];
    
    		nVerts = 0;
    		nTree = 0;
    
    		for (int j = 0; j < MAX_VERTS; j++)           //set adjacency
    			for (int k = 0; k < MAX_VERTS; k++)       //matrix
    				adjMat[j][k] = INFINITY;              //to infinity
    
    	
    	} //end constructor
    
    	public void addVertex( char lab)
    	{
    		vertexList[nVerts++] = new Vertex(lab);
    	}
    ... and the functions continue. I just need help with declaring and initialising in C++. Here is what I have in C++. I can't for the life of me get it to compile.

    Code:
    class Graph
    {
    public:
       
      int MAX_VERTS;                            //Need to change later to match user input
      int INFINITY;
    
      Vertex vertexList[];        		    //list of all our vertices
      int adjMat[][];   			    //This is our adjacency matrix for vertice layout representation
      DistPar sPath[];                          //shortest path array
      
      int nTree;                                //Number of vertices in our tree
      int nVerts;                               //current number of vertices
        
      int currentVert;                          //current vertex
      int startToCurrent;                       //distance to currentVert from start 
       
      Graph()                                   //Needs to take input parameters later
      {
        MAX_VERTS = 999;
        INFINITY = 1000000;
      	
        nVerts = 0;
        nTree = 0;
    
        vertexList = new Vertex[MAX_VERTS];
        adjMat = new int[MAX_VERTS][MAX_VERTS];
    
        //Set adjMatrix to infinity
        for ( int a = 0; a < MAX_VERTS; a++)
          for ( int b = 0; b < MAX_VERTS; b++)
    		adjMat[a][b] = INFINITY;
    
        //Initialize sPath to all infinities
        //sPath = new DistPar[MAX_VERTS];
      }
    
      void addVertex(char lab)
      {
        nVerts++;
        vertexList[nVerts] = Vertex(lab);
      }
    I know it looks almost exactly the same as its java counterpart. I have several sources opened, but it's all bits and pieces of what I need. So I've decided to ask the community to point me in the right direction instead.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    Hey, I'm picking up C++ by translating java code to C++.
    Probably the worst way to do it.

    Since you want to get direction, try picking and reading a book about C++ from here. If you want something online, Brice Eckel's Thinking in C++ is freely and legally, completely available in HTML format here.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    You can't dynamically allocate a multidimensional array in C++ in the same way as in Java. For example,
    Code:
    new int[X][Y]
    is invalid. See Multi-Dimensional Arrays - C++ Forum.
    Last edited by Time Traveler; 01-23-2012 at 08:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring classes and defining later?
    By jw232 in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2008, 05:13 PM
  2. Declaring an array of classes...
    By Tozar in forum C++ Programming
    Replies: 11
    Last Post: 10-28-2006, 11:37 AM
  3. Declaring classes in global space
    By Rune Hunter in forum C++ Programming
    Replies: 12
    Last Post: 10-12-2005, 03:46 PM
  4. On declaring classes. (Newbie)
    By suzakugaiden in forum C++ Programming
    Replies: 26
    Last Post: 01-31-2005, 06:15 PM
  5. different ways of declaring classes
    By confuted in forum C++ Programming
    Replies: 7
    Last Post: 08-19-2003, 09:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21