C++ question

Discussion in 'OT Technology' started by D1G1T4L, May 14, 2005.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    i am creating a header file with the following

    Code:
    ...
    private:
    	const int N; 
    	// the dimensions of the puzzle
    	
    	int board[N][N]; 
    	// the board of the puzzle
    	
    	
    	struct position 
    	{ 
            char pos[N*N];            
            long prev;              
    		long empty; 
    	}; 
    ...
    
    a few questions here, first of all i get N undeclared identifier error but the problem is you cant initilize varibles in a header file (at least from what i understand?), so i think the way to do it is in the cpp file that uses the header file, to declare it in the constructor.. something like this
    Code:
    Puzzle::Puzzle():N(3)
    
    i still get an error, what is the proper way to do it?


    i am also not too sure if structs are allowed in the header files? (but i think they are)

    ok thanks in advance to anyone that helps :hsd:
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Since you have the private tag, I'm assuming that N is a member of a class.
    You can initialize const members like this in the global scope:

    ClassName::N = 3;

    So, yes, you should put that in a cpp file in the global scope.

    Struct definitions are fine in header files, and it is appropriate if the struct definition is used in more than one cpp file.
     
  3. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    It is difficult to understand what you're trying to do when you only post a code fragment. I would change two things:

    1) I am not sure about declaring a struct inside the private member section of a class. I would declare it outside the private scope of the class just to be safe.

    2) I would change your char array in your struct to a dynamically allocated array that is created inside the constructor. That way, you can give the N integer value as an argument for the constructor and create the appropriately sized array.
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    If the struct is only used within the scope of the class, then it makes sense to declare it there. It's encapsulation.

    If you plan on allowing for use of the copy constructor, it's probably prefereable to leave it as a static array to avoid headaches, especially if the size is going to remain constant.
     
  5. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    Doesnt work, i put Puzzle:: N = 3; in Puzzle.cpp just before the Puzzle::puzzle constructor and it doesnt work, still gives me

    "Puzzle.h(52): error C2065: 'N' : undeclared identifier" Line 52

    and line 52 in the header file is "int board[N][N];"
     
  6. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    so it looks like the line in h file

    Code:
    const int N; 
    	// the dimensions of the puzzle
    
    is being initialized

    the only problem is with the array in the header file

    Code:
    int board[N][N]; 
    	// the board of the puzzle
    
    thats not being intialized

    btw this is my code in cpp file that initializes the N

    Code:
    #include <iostream>
    using namespace std;
    
    #include <cstdlib>
    using std::srand;
    
    #include <time.h>
    
    #include <iomanip>
    using std::setw;
    
    #include "Puzzle.h"
    
    Puzzle::Puzzle()[b]:N(3)[/b]
    {
    	int i=0;
    	int j=0;
    	int found = 0;
    ....
    
     
  7. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    It looks like you need to make it a static const. If you do that, you can initialize it inside the class definition.

    static const int N = 3;

    You don't initialize it in the constuctor.
     
  8. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    thank you, now all my stupid errors are gone EXCEPT, i dont get this at all
    these errors

    Code:
    c:\Documents and Settings\Digital\Desktop\Puzzle\Puzzle.cpp(104): error C2511: 'int Puzzle::down(Puzzle::position *,Puzzle::position *)' : overloaded member function not found in 'Puzzle'
    c:\Documents and Settings\Digital\Desktop\Puzzle\Puzzle.cpp(131): error C2511: 'int Puzzle::goal(Puzzle::position *)' : overloaded member function not found in 'Puzzle'
    c:\Documents and Settings\Digital\Desktop\Puzzle\Puzzle.cpp(113): error C2511: 'int Puzzle::left(Puzzle::position *,Puzzle::position *)' : overloaded member function not found in 'Puzzle'
    c:\Documents and Settings\Digital\Desktop\Puzzle\Puzzle.cpp(174): error C2511: 'int Puzzle::posExists(Puzzle::position *)' : overloaded member function not found in 'Puzzle'
    c:\Documents and Settings\Digital\Desktop\Puzzle\Puzzle.cpp(122): error C2511: 'int Puzzle::right(Puzzle::position *,Puzzle::position *)' : overloaded member function not found in 'Puzzle'
    c:\Documents and Settings\Digital\Desktop\Puzzle\Puzzle.cpp(93): error C2511: 'int Puzzle::up(Puzzle::position *,Puzzle::position *)' : overloaded member function not found in 'Puzzle'
    c:\Documents and Settings\Digital\Desktop\Puzzle\Puzzle.cpp(244): error C2511: 'void Puzzle::loadBoard(int)' : overloaded member function not found in 'Puzzle'
    c:\Documents and Settings\Digital\Desktop\Puzzle\Puzzle.cpp(157): error C2511: 'void Puzzle::printPos(Puzzle::position *)' : overloaded member function not found in 'Puzzle'
    

    this doesnt make sense to me because all my functions are declared in the Puzzle.h

    here is the header file

    Code:
    #ifndef PUZZLE_H
    #define PUZZLE_H
    
    class Puzzle
    {
    public:
    
    	Puzzle(); 
    	// Puzzle constructor
    	
    	// struct for storing the states of the puzzle
    
    	int up(struct position *dest, struct position *src); 
    	// move the empty tile up while switching with the upper tile
    	
    	int down(struct position *dest, struct position *src); 
    	// move the empty tile down while switching with the lower tile
    	
    	int left(struct position *dest, struct position *src); 
    	// move the empty tile left while switching with the left tile
    	
    	int right(struct position *dest, struct position *src); 
    	// move the empty tile right while switching with the right tile
    	
    	int goal(struct position *p); 
    	// if it has reach the goal
    	
    	int permIndex(const char pit[]); 
    	// creates the index of a state
    	
    	int posExists(struct position *p); 
    	// check if the state exist
    	
    	int checkParity(); 
    	// check the parity in the puzzle
    	
    	void loadBoard(); 
    	// randomizes the puzzle and loads it
    	
    	void printPos(struct position *p); 
    	// prints out the state of the puzzle 
    	
    	void showBoard(); 
    	// prints out the puzzle
    	
    	void fixParity(); 
    	// fix the parity (tried to use to fix parity but did not work)
    	
    	static const int N = 3;
    	// the dimensions of the puzzle
    
    	int board[N][N]; 
    	// the board of the puzzle
    	
    	
    	struct position 
    	{ 
            char pos[N*N];            
            long prev;              
    		long empty; 
    	}; 
    	// store the states of the puzzle when finding solution
    
    
    
    private:
    	
    
    	int closed[45360]; 
    	// closed list stores indexs
    	
    	int front, end;
    	
    	position bf[181450]; 
    	// a huge array size to store the states
    };
    #endif
    
    
     
  9. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    anyways skinjob helped me on aim, thx to everyone who posted in this thread! =)
     

Share This Page