CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2008
    Posts
    60

    problem with constructors and classes

    I have a class like this:
    Code:
    #include "History.h"
    class arena
    {
     public:
       arena(some arguments)
        : m_history(int num1, int num2) {};
    
     private:
        History m_hisory;
    }
    and history class is basically something like this:
    Code:
    //history.h
    class history
    {
     public:
        history(int num1, int num2){row=num1; col = num2;}
    private:
        int row, col;
    }
    the compiler says that i can't have 2 argument for m_history, in the arena constructors member initialization list. How can i overcome this problem?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problem with constructors and classes

    Please take the time to post the real code you're compiling. Don't hurredly type stuff in the message that looks like your code.
    Code:
    class history
    {
     public:
        history(int num1, int num2){row=num1; col = num2;}
    private:
        int row, col;
    };
    
    class arena
    {
     public:
       arena()
        : m_history(int num1, int num2) {};
    
     private:
        History m_hisory;
    };
    Code:
    Thank you for testing your code with Comeau C/C++!
    Tell others about http://www.comeaucomputing.com/tryitout !
    
    Your Comeau C/C++ test results are as follows:
    
    Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2007 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 16: error: identifier "History" is undefined
          History m_hisory;
          ^
    
    "ComeauTest.c", line 13: error: "m_history" is not a nonstatic data member or base
              class of class "arena"
          : m_history(int num1, int num2) {};
            ^
    
    "ComeauTest.c", line 13: error: type name is not allowed
          : m_history(int num1, int num2) {};
                      ^
    
    "ComeauTest.c", line 13: error: expected a ")"
          : m_history(int num1, int num2) {};
                          ^
    
    4 errors detected in the compilation of "ComeauTest.c".
    
    In strict mode, with -tused, Compile failed
    This was after I had to add the semicolons to the end of the class definitions.

    http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2008
    Posts
    60

    Re: problem with constructors and classes

    I will
    sorry for the trouble you went through

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: problem with constructors and classes

    When you're initialising the object in the initialiser list, you don't repeat the types. Just supply actual arguments for the constructor.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Jan 2008
    Posts
    60

    Re: problem with constructors and classes

    That was my mistake wen i posted it. The problem like i said is that in the arena constructor, m_block() can't take more than one argument.
    However, m_block's constructor does take 2 argumets....

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: problem with constructors and classes

    That was my mistake wen i posted it. The problem like i said is that in the arena constructor, m_block() can't take more than one argument.
    However, m_block's constructor does take 2 argumets....
    You probably have one or more typographical errors in your code. If you can make such errors when typing code to be posted (and you have made many such errors, by your own admission and obvious with casual inspection), you can make such errors when typing code to be compiled.

    So, post your actual test program by copying and pasting the code from your IDE or text editor.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured