Thread: Help needed in C++/CLI with error C2512: no appropriate default constructor available

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Help needed in C++/CLI with error C2512: no appropriate default constructor available

    Having this issue with C++/CLI that's troubling me for a real long time and almost everything I've tried has failed. The problem is when I'm trying to create an instance of the GenDen reference managed type. I keep encountering error C2512: 'Instruction::GenDen' : no appropriate default constructor available. I'm posting the pieces of code here:


    Code:

    Code:
    // GenDen.h
    #pragma once
    
    #include "prm.h"
    
    namespace Triad{
    
    public ref class GenDen : public Prm
    {
    	public:
    		GenDen();
    		GenDen(Slots slot);
    };
    }
    Code:
    // GenDen.cpp
    #include "stdafx.h"
    #include " GenDen.h"
    
    using namespace Triad;
    
    GenDen::GenDen (): Prm(){} //default constructor
    
    GenDen::GenDen(Slots slot) : Prm()
    {
    	try{
    		Initialize(slot);
    	}
    	catch(DenException^ e){
    		throw e;
    	}
    }
    
    void GenDen::Initialize(Slots slot){
    	//Whole bunch of Code related to this Initialize function
    }
    Main Project File:
    Code:
    // Instruction.cpp
    
    #include "stdafx.h"
    #include "Form1.h"
    #include "GenDen.h"
    #include "prm.h"
    
    using namespace Instruction;
    
    void Form1::OpenInstruction(void)
    {
    	try {
    		
    		Den = gcnew GenDen (Triad::Slots::Slot8); //results in error C2512: 'Instruction::GenDen' : no appropriate default constructor available
    
    		// Den = gcnew GenDen ();	//declaring it like this also results in the same error C2512	
    		
    	}
    	catch (Pier::PierException^ ge) {
    		throw ge;
    	}
    
    }
    Code:
    //Form1.h
    #pragma once
    
    #include "prm.h"
    namespace Instruction {
    
    	ref class GenDen; 
    
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
    
    	protected:
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private:
    		void Open Instruction ();
    	private:
    		GenDen^ Den; //declaring a handle of GenDen managed type
                   };
    }
    Code:
    //Prm.h
    #pragma once
    
    namespace Triad
    {
    	using namespace System;
    	using namespace System::Windows::Forms;
    
    	public enum class Slots{
    		Slot1,
    		Slot2,
    		Slot3,
    		Slot4,
    		Slot5,
    		Slot6,
    		Slot7,
    		Slot8,
    		Slot9,
    		Slot10,
    		Slot11,
    		Slot12,
    		Slot13,
    		Slot14,
    		Slot15,
    		Slot16,
    		Slot17,
    		Slot18,
    		Slot19,
    		Slot20
    	};
    
    	public ref class Prm{
    	public:
    		Prm(){ errorCode = 0; handle = 0; }
    		
    		virtual void Initialize(Slots slot){}
    		virtual void Reset(){}
    	};
    }
    Can anyone help me on where I'm going wrong? All this is being done in VS 2010 C++ .NET 4.0. I'm new to C++/CLI, and figuring out all the intricacies has been interesting (even though they took a while). But this particular issue is proving beyond me.

    Thanks
    Last edited by allan_pollock; 05-26-2011 at 11:03 AM.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    hmm...

    First, where are you defining Den? Is it being defined as GenDen^ Den?

    Next, if you want to initialize with the default constructor, all you need to do is GenDen^ Den = gcnew GenDen; (no parenthesis).

    Finally, the error is resulting from you trying to bring a type name into a constructor expecting a type instance. Define a Slots variable and assign it the value Slot8 and then pass that variable to the Den declaration.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    		
    Den = gcnew GenDen (Triad::Slots::Slot8); //results in error C2512: 'Instruction::GenDen' : no appropriate default constructor available
    I suggest using the correct namespace; note, this is a wild guess from an C programmer learning C++.

    Code:
    Den = gcnew Triad::GenDen (Triad::Slots::Slot8);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-06-2011, 08:08 PM
  2. Replies: 5
    Last Post: 02-21-2011, 02:19 AM
  3. Specialized Constructor call Default Constructor
    By threahdead in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2010, 03:39 PM
  4. Error: no appropriate default constructor available
    By csonx_p in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2008, 06:04 AM
  5. no default constructor error
    By 7stud in forum C++ Programming
    Replies: 8
    Last Post: 08-23-2003, 01:35 AM