none
c2512 : no appropriate default constructor?

    Question

  • Getting C2512 error when there IS a default constructor.

                             Form1.h(23) : error C2512: 'formPointer2::testClass' : no appropriate default constructor available

    I'm guessing it relates somehow to my use of forward declarations but I can't see how to cure it. Any suggestions appreciated. Code below:

    Class without appropriate default constructor:

    #pragma once;

    //forward declaration for namespace
    namespace formPointer2 {ref class Form1;}

    using namespace formPointer2;

    public ref class testClass
    {
    public:
        testClass(){}

        testClass(Form1^ z) {x=z;}
       
    private:
        Form1^ x;

    };

     

    Class that is trying to instantiate it:

    #pragma once

    #include "testClass.h"

    namespace formPointer2 {

        using namespace System;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;

        //forward declaration for testClass
        ref class testClass;

        public ref class Form1 : public System::Windows::Forms::Form
        {
        public:
            Form1(void)
            {
                InitializeComponent();

                formPointer2::testClass^ tc = gcnew formPointer2::testClass;                  //Here is where the error is generated
            }

        protected:
            ~Form1()
            {
                if (components)
                {
                    delete components;
                }
            }
        private: System::Windows::Forms::Button^  button1;
        protected:
       

        private:
            System::ComponentModel::Container ^components;

    #pragma region Windows Form Designer generated code
            void InitializeComponent(void)
            {
                this->button1 = (gcnew System::Windows::Forms::Button());
                this->SuspendLayout();
                //
                // button1
                //
                this->button1->Location = System::Drawing::Point(92, 188);
                this->button1->Name = L"button1";
                this->button1->Size = System::Drawing::Size(96, 24);
                this->button1->TabIndex = 0;
                this->button1->Text = L"button1";
                this->button1->UseVisualStyleBackColor = true;
                //
                // Form1
                //
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(284, 262);
                this->Controls->Add(this->button1);
                this->Name = L"Form1";
                this->Text = L"Form1";
                this->ResumeLayout(false);

            }
    #pragma endregion
        };
    }



    David Pantaleoni
    Tuesday, June 28, 2011 3:14 PM

Answers

  • You class 'testClass' is declared in the global namespace, while in Form1.h, you have forward declared it within the scope of the formPointer2 namespace, and then you are also trying to use with explicit scope resolution of formPointer2 namespace, i.e. as 'formPointer2::testClass'.

    Remove that forward declaration in Form1.h - it's redundant. Then, enclose the class 'testClass' in the namespace 'formPointer2' as well. That should solve your issues.

    - Shoaib.

    • Marked as answer by DGPBrooklyn Tuesday, June 28, 2011 5:01 PM
    Tuesday, June 28, 2011 4:08 PM

All replies

  • On 6/28/2011 11:14 AM, DGPBrooklyn wrote:

    Getting C2512 error when there IS a default constructor.

                              Form1.h(23) : error C2512: 'formPointer2::testClass' : no appropriate default constructor available

    #pragma once;

    //forward declaration for namespace
    namespace formPointer2 {ref class Form1;}

    using namespace formPointer2;

    public ref class testClass

    This defines ::testClass - a class in the global namespace.

    namespace formPointer2 {

         //forward declaration for testClass
         ref class testClass;

    This forward-delcares formPointer2::testClass - a class in formPointer2 namespace that is never defined anywhere.  This class is completely unrelated to ::testClass.

             Form1(void)
             {
                 InitializeComponent();

                 formPointer2::testClass^ tc = gcnew formPointer2::testClass;                  //Here is where the error is generated

    This tries to use formPointer2::testClass which, again, is only declared but never defined.


    Igor Tandetnik

    Tuesday, June 28, 2011 4:06 PM
  • You class 'testClass' is declared in the global namespace, while in Form1.h, you have forward declared it within the scope of the formPointer2 namespace, and then you are also trying to use with explicit scope resolution of formPointer2 namespace, i.e. as 'formPointer2::testClass'.

    Remove that forward declaration in Form1.h - it's redundant. Then, enclose the class 'testClass' in the namespace 'formPointer2' as well. That should solve your issues.

    - Shoaib.

    • Marked as answer by DGPBrooklyn Tuesday, June 28, 2011 5:01 PM
    Tuesday, June 28, 2011 4:08 PM
  • Thanks all, problem solved. I thought it was something like this but getting it to work was taking a toll on my patience.

     

    Dave


    David Pantaleoni
    Tuesday, June 28, 2011 4:34 PM
  • It would be helpful to future readers of this thread if you could mark Igor's response as the answer to your question. Thanks.
    http://blog.voidnish.com
    Tuesday, June 28, 2011 4:53 PM
  • Thanks to both M. Shoaib Surya and Igor Tandetnik. Changes made and successful compile and link.

    Now I have a followup problem.

    When I modified the testClass constructor to accept a Form1^ parameter I am unable to use it and get a c2027 error.

       error c2027: use of undefined type 'formPointer2::Form1'

     

    modified constructor here:

    testClass(Form1^ z)

    {

        x = z;

        x->button1->text = "bye";

    }

     


    David Pantaleoni
    Tuesday, June 28, 2011 5:16 PM
  • On 6/28/2011 1:16 PM, DGPBrooklyn wrote:

    Thanks to both M. Shoaib Surya and Igor Tandetnik. Changes made and successful compile and link.

    Now I have a followup problem.

    When I modified the testClass constructor to accept a Form1^ parameter I am unable to use it and get a c2027 error.

        error c2027: use of undefined type 'formPointer2::Form1'

    Declare your methods in testClass.h file, without implementation. Place the implementation into a separate cpp file (say, testClass.cpp) which would include both testClass.h and Form1.h


    Igor Tandetnik

    Tuesday, June 28, 2011 8:20 PM