Thread: Silly question but..

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Silly question but..

    I am working on my final homework assignment, and I have just started.

    All I want to do is initalize the array of pointers in a function with values. I declared it correctly but according to the compiler im not allowed to initalize it in an independant function?

    The way around it is to declare the array and initalize it, but then I would have to take it out of the class member list and I do not want to do that, somthing like:

    Code:
    *pFilmList[  5 ] = { "", "", "", "", "" };
    As you know, the above would work as an altenative. But I dont understand what is wrong with the way I have done it below? I have posted the errors messages below.

    Code:
    class Cinema
    {
    public:
    	Cinema();
    	~Cinema();
    
    	static const int MAX_ROWS = 20;
    	static const int MAX_COLS = 20;
    	static const int MAX_FILM = 5;
    
    	void initSeating ( char[][ 20 ] );
    	void initFilms ( char*[] );
    
    private:
    	char m_Seating[ MAX_ROWS ][ MAX_COLS ];
    	char *pm_Films[ MAX_FILM ];
    };
    
    Cinema::Cinema()
    {
    	initSeating ( m_Seating );
    	initFilms ( pm_Films );
    }
    
    Cinema::~Cinema() 
    {
    }
    
    // function 
    void Cinema::initSeating ( char seats[][ 20 ] )
    {
    	for ( int i = 0; i < 20; i++ )
    	{
    		for ( int j = 0; j < 20; j++ )
    			seats[ i ][ j ] = 0;
    	}
    }
    
    // FUNCTION WITH ERRORS
    void Cinema::initFilms ( char *pFilms[] )
    {
    	*pFilms[ MAX_FILM ] = { "sdds", "ddsd", "sdsd", "Wdeds", "sdsd" };
    }
    Errors:

    exercises\11 cinema system\11 cinema system\main.cpp(65) : error C2059: syntax error : '{
    exercises\11 cinema system\11 cinema system\main.cpp(65) : error C2143: syntax error : missing ';' before '}'
    Double Helix STL

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You cannot use initialization lists for anything but initializers.
    You'll just have to do this:
    Code:
    pm_Films[0] = ...;
    pm_Films[1] = ...;
    And don't assign string literals to non-const char pointers. For heaven's sake, you've been a member for nearly three years.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Quote Originally Posted by CornedBee View Post
    You cannot use initialization lists for anything but initializers.
    You'll just have to do this:
    Code:
    pm_Films[0] = ...;
    pm_Films[1] = ...;
    And don't assign string literals to non-const char pointers. For heaven's sake, you've been a member for nearly three years.
    Thanks. And sorry, I didnt mean to upset you, il remember your advice, I should of known better about the const char issue. I take full responsibility for such a dangerous error.
    Double Helix STL

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I detect a hint of sarcasm I don't think she is upset though, its just a mistake one theoretically would not make if they did a forum search before asking.

    Really quick though, compile this:
    Erroneous Example
    Code:
    #include <cstring>
    #include <iostream>
    
    char *global = "hello world";
    
    int main(void)
    {
        strcpy(global, "Look at me! I am breaking my own program. Writing code like this makes windows what it is today!");
    
        std::cout << global << std::endl;
    
        return 0;
    }
    You can see the implications of the code without running it. Don't think CornedBee is being a stickler, she isn't. One needs to remember, non-new programmers tend to use a lot of libraries to save us the time and money of writing code. The implication of libraries is that we may not read every line of the library's code, thus it may not be our code making such an obvious mistake.

    Most compilers try to give you the heads up about these things. That is why.
    Last edited by laserlight; 05-05-2008 at 12:45 PM. Reason: Fixed color tag.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. silly printf("%d", var) question
    By nacho4d in forum C Programming
    Replies: 2
    Last Post: 10-30-2008, 04:08 PM
  2. A silly question but it's my doubt ;)
    By chottachatri in forum C++ Programming
    Replies: 19
    Last Post: 04-23-2008, 01:26 PM
  3. Really quick silly question.
    By Jozrael in forum C++ Programming
    Replies: 36
    Last Post: 04-04-2008, 08:31 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM