Thread: syntax problems =/

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    9

    syntax problems =/

    I'm making a class with various functions for finding the shortest path from one spot on a map to another around "terrain". I haven't used c++ in awhile, and I am not quite sure how to go about passing 2d char array of an unknown size into my class.

    Here is just a general layout of my class so far:
    Code:
    class ShortPath
    {
    	private:
    
    		struct List
    		{
    			List* node;
    			int f;
    			int h;
    			int g;
    		}*oRoot,*cRoot;
    
    		void listAdd(List* l);
    		
    	public:
    
    		ShortPath(char** m);
    		ShortPath(char** m, char* d);
    		~ShortPath();
    		int getShortPathSteps(void);
    		void findShortPath(char** m);
    		void getShortPath(char** m, char* d);
    };
    How do I pass into my constructor ShortPath(char** m) a 2d array of [8][8].

    ShortPath find = new ShortPath(arr2d); // doesn't work

    I've tried many other things to get it to work too >.>, most it does is go in and doesn't get the even the values. I stored into arr2d. Basically what I THINK I want to do is pass in a pointer to my 2d array.

    Probably a better one to structure my class, any recommendations would be awesome <3.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can only pass in a 2d array if you know the second dimension at compile time. So you want vector< vector<char> >.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You want to rely more on std::string and std::vector. Perhaps even std::list? I do not know if it tries to use a linked list or not...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    using namespace std;
    
    class ShortPath
    {
    	private:
    
    		struct List
    		{
    			List* node;
    			int f;
    			int h;
    			int g;
    		}*oRoot,*cRoot;
    
    		void listAdd(List* l);//operates list recursively
    		
    	public:
    
    		ShortPath(vector<vector<char> >& m);//if mem just wants to just ge
    		ShortPath(vector<vector<char> >& m, vector<char>& data);//if mem wants to store path too.
    		~ShortPath();//don't forget to free up memory.
    		int getShortPathSteps(void);//returns number of steps from path
    		void findShortPath(vector<vector<char> >& m);//does not store path. Only FINDS
    		void getShortPath(vector<vector<char> >& m, vector<char>& d);//finds AND stores path
    };
    
    //constructor definition(1)[user doesn't want to store path]
    ShortPath::ShortPath(vector<vector<char> >& maze)
    {
    	oRoot = NULL;
    	cRoot = NULL;
    
    	findShortPath(maze);
    }
    
    //constructor definition(2)[user wants to retrieve path]:
    ShortPath::ShortPath(vector<vector<char> >& maze, vector<char>& data)
    {
    	oRoot = NULL;
    	cRoot = NULL;
    
    	getShortPath(maze,data);
    }
    
    void ShortPath::listAdd(List* node)
    {
    	//add method for list recursively
    }
    //getShortPath definition(1)[finds path only]
    
    void ShortPath::findShortPath(vector<vector<char> >& maze)
    {
    	int x = 0;
    	int y = 0;
    	int c = 0;
    
    	//algorithm goes here
    }
    
    void ShortPath::getShortPath(vector<vector<char> >& maze, vector<char>& data)
    {
    	int x = 0;
    	int y = 0;
    	int c = 0;
    
    	//algorithm goes here
    }
    
    int ShortPath::getShortPathSteps(void)
    {
    	int count = 0;
    	//just recurse through cRoot and count how many nodes there are
    	return count;
    }
    
    ShortPath::~ShortPath(){}
    Pretty sure that's what you meant right? Never got proper experience with vectors... I tried creating an instance of my class and passing a 2d vector in and it seemed to work. I put a cout statement in my constructor to print out the contents of the passed in 2d vector and it worked.

    can someone tell me about std::map or w/e I heard about... it sounded like something that might be of interest to me.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A map is an association of key to value; the keys and values may be different types. Each key can have only one value (if you want more, that's multimap); and the objects are stored so that finding a key is quick. If you're familiar with say the Perl hash, this is the same sort of deal ("associative array" is the fancy term).

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    can I do an if(maze[x][y]=='s'){ blah } if maze is a 2d vector? I keep getting a segmentation problem with the following code:

    Code:
    int main(int argc, char* argv[])
    {
    	vector<vector<char> > maze(8,vector<char>(8,'x'));
    	ifstream maze_file("maze.txt");
    	int s_x=0;
    	int f_y=0;
    	int s_y=0;
    	int f_x=0;
    	int x=0;
    	int y=0;
    	if(!maze_file.is_open())
    	{
    		cout << "Error opening file!";
    	}
    
    	else
    	{	
    		while(!maze_file.eof())
    		{
    			for(y = 0; y < 8; y++)
    			{
    				maze_file >> maze[x][y];
    
    				if(maze[x][y]=='s')
    				{
    //					s_x=x;
    //					s_y=y;
    				}
    				else if(maze[x][y]=='f')
    				{
    //					f_x=x;
    //					f_y=y;
    				}
    			}
    			x++;
    		}
    	} 
    
    	maze_file.close();
    
    
    //	ShortPath find(maze,s_x,s_y);
    
    	cin.get();
    	return 0;
    }
    also it's not letting me initialize same variable types on 1 line to a value with segmenting as well =/. I don't remember it being like this. If I take out those if's it works fine.

    EDIT:

    nvm, figured it out... my outer loop (the while loop) was going through my maze.txt 1 row more than expected so I needed to size my vector appropriately(so it was checking a place that didn't exist).
    Last edited by アストラル; 11-02-2008 at 11:34 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would guess that eof isn't true until you fail to read in the ninth (nonexistent) line, but you're stuck in the for loop anyway, trying to write to memory (maze[8][0]) that doesn't exist. I didn't have any problem with the four commented out lines.

    Edit: The easy way, and one of the many points of vector, is that you should be using push_back instead of assignment anyway, right?
    Last edited by tabstop; 11-02-2008 at 11:34 PM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can also use the .at member function to make the class throw an exception if you try to access an out-of-bounds element. Helps in debugging.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  3. What's wrong with this code??
    By Xanth in forum C Programming
    Replies: 11
    Last Post: 12-23-2004, 02:41 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Need Help with syntax and overall problems!
    By Simon in forum C Programming
    Replies: 1
    Last Post: 09-11-2002, 11:15 PM