Thread: Class Error

  1. #1
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63

    Class Error

    I yet again started another journey into C++, the class's. Now, in my class I have 1 line that has a few errors alone, and then 30 other errors spread around the code. Right now its the single line thats annoying me. This is the line:
    Code:
    char *dat_filename=new char[50];
    And the errors:
    Error 1: ISO C++ forbids initialization of member 'dat_filename'

    Line 2: making 'dat_filename' static

    Error 3: ISO C++ forbids in-class initialization of non-const static member 'dat_filename'

    Error 4: field initializer is not constant
    Thats the info, really annoying I have been trying for about 15 minutes but couldnt find out why it isnt allowing me to declare it.


    Oh and, just to ask, what is the max amount of characters in a windows filename? Because Im almost garenteed its not 50

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    I guess your code looks something like:
    Code:
    class SomeClass {
        // ...
        char *dat_filename=new char[50];
        // ...
    };
    You can't initialize member variables like that, you need to use a constructor to allocate memory, and a destructor to delete the allocated memory:
    Code:
    class SomeClass {
    
    public:
        SomeClass(void) {
            dat_filename = new char[50];
        }
        
        ~SomeClass(void) {
            delete [] dat_filename;
        }
    
    private:
        char *dat_filename;
    
    };
    And the maximum length for a filename in Windows is 256.
    Last edited by glUser3f; 08-08-2004 at 10:55 AM. Reason: added ;'s

  3. #3
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Thanks, that worked, 4 errors less.

    Ive been working with it for a while and fixed about 10 other errors, but Im stuck on this one because its my first time using a self made class.
    Error 1: ISO C++ forbids defining types within return type
    Error 2: return type specification for constructor invalid
    Seems something is screwed with my constructor
    Code:
    //In class:
    testclass(); //constructor declared
    
    //Defined:
    testclass::testclass() {
    }//Constructor
    Do I have to declare the variables, or what? It says return type is incorrect, but a constructor doesnt have one, does it?


    EDIT: Nevermind, didnt notice that I forgot the semicolon at the end of the class, the constructor was right after it.
    Last edited by LloydUzari; 08-08-2004 at 08:09 AM.

  4. #4
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    (sorry for double post, thought people might ignore it if I just edited again)

    New problem, tried to update it after I got it working, and more errors. I decided to work with the constructor//deconstructor, but it came with some bugs. I was wondering if its possible to allocate a 2D array the way I am, or if theres a problem:
    Code:
    testclass::testclass(char *filename) {
      dat_filename=new char[50]; //decided to keep 50 :D 
      setfile(filename);
      setsize();
      dat_array=new int[dat_x][dat_y];
    }
    The dat_array is declared: 'int **dat_array;', and the deconstructor deletes the allocated data. Its now giving me an error that says it 'cant convert int (*)dat_y to int**'. The wierd thing is, that dat_y is declared as a normal int variable.

    Info:
    setfile -- sets the variable 'char *dat_filename'
    setsize -- sets 'dat_x' and 'dat_y' from values in a textfile

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There should be a post either on this page, or somewhere close on allocating two dimensional arrays. I know it was covered in the past few days. Do a search and you should get lots of posts, rather than me just rehash how to do it again.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM