Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C2512: no appropriate default constructor available

C2512: no appropriate default constructor available

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++regexquestion
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manfr3d
    wrote on last edited by
    #1

    Hello guys, I'm trying to write a simple class in c++ including a short constructor. Despite the fact that there are no obvious mistakes, there's always the same error message C2512: no appropriate default constructor available I do not call the default constructor, but my self written one, also the parameter lists match. Does anyone already know this problem? If not I can post the listings. Thanks and best wishes.

    D _ CPalliniC N 4 Replies Last reply
    0
    • M Manfr3d

      Hello guys, I'm trying to write a simple class in c++ including a short constructor. Despite the fact that there are no obvious mistakes, there's always the same error message C2512: no appropriate default constructor available I do not call the default constructor, but my self written one, also the parameter lists match. Does anyone already know this problem? If not I can post the listings. Thanks and best wishes.

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      If you've written a constructor with parameters, it is not called default constructor. Default constructor is one with no parameters. The object you're creating from the class probably needs a default constructor.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      P 1 Reply Last reply
      0
      • M Manfr3d

        Hello guys, I'm trying to write a simple class in c++ including a short constructor. Despite the fact that there are no obvious mistakes, there's always the same error message C2512: no appropriate default constructor available I do not call the default constructor, but my self written one, also the parameter lists match. Does anyone already know this problem? If not I can post the listings. Thanks and best wishes.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Austrian_Programmer wrote:

        Does anyone already know this problem?

        Yes, provide a default constructor (one accepting no arguments). If that does not work, see here.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Man who follows car will be exhausted." - Confucius

        1 Reply Last reply
        0
        • _ _Superman_

          If you've written a constructor with parameters, it is not called default constructor. Default constructor is one with no parameters. The object you're creating from the class probably needs a default constructor.

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          A default constructor is one provided by the compiler -- if you write one that takes no parameters it's not a default constructor. Edit: Oh, I see, I wound up in the C++ forum somehow... :~ In C#, "default constructor" actually means something more than "parameterless constructor": " 10.10.4 Default constructors If a class contains no instance constructor declarations, a default instance constructor is automatically provided. "

          modified on Tuesday, April 27, 2010 6:00 PM

          M CPalliniC 2 Replies Last reply
          0
          • P PIEBALDconsult

            A default constructor is one provided by the compiler -- if you write one that takes no parameters it's not a default constructor. Edit: Oh, I see, I wound up in the C++ forum somehow... :~ In C#, "default constructor" actually means something more than "parameterless constructor": " 10.10.4 Default constructors If a class contains no instance constructor declarations, a default instance constructor is automatically provided. "

            modified on Tuesday, April 27, 2010 6:00 PM

            M Offline
            M Offline
            Moak
            wrote on last edited by
            #5

            The C++ standard defines that "a default constructor is a constructor that can be called without arguments". (ISO/IEC 14882)

            Webchat in Europe :java: Now with 26% more Twitter

            1 Reply Last reply
            0
            • M Manfr3d

              Hello guys, I'm trying to write a simple class in c++ including a short constructor. Despite the fact that there are no obvious mistakes, there's always the same error message C2512: no appropriate default constructor available I do not call the default constructor, but my self written one, also the parameter lists match. Does anyone already know this problem? If not I can post the listings. Thanks and best wishes.

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Austrian_Programmer wrote:

              I do not call the default constructor, but my self written one, also the parameter lists match.

              Since you've written one constructor the compiler provide no more the default one (that is sometimes required). Probably you should write the default constructor for your class (i.e. please report the exact error message and the offending line content). :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • P PIEBALDconsult

                A default constructor is one provided by the compiler -- if you write one that takes no parameters it's not a default constructor. Edit: Oh, I see, I wound up in the C++ forum somehow... :~ In C#, "default constructor" actually means something more than "parameterless constructor": " 10.10.4 Default constructors If a class contains no instance constructor declarations, a default instance constructor is automatically provided. "

                modified on Tuesday, April 27, 2010 6:00 PM

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                You are wrong. The default constructor is the constructor without arguments. Incidentally, if you don't write any constructor for your class then the compiler provides the default constructor for you. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • M Manfr3d

                  Hello guys, I'm trying to write a simple class in c++ including a short constructor. Despite the fact that there are no obvious mistakes, there's always the same error message C2512: no appropriate default constructor available I do not call the default constructor, but my self written one, also the parameter lists match. Does anyone already know this problem? If not I can post the listings. Thanks and best wishes.

                  N Offline
                  N Offline
                  Nisamudheen
                  wrote on last edited by
                  #8

                  The other side of the story is that, you can prevent a constructor being called (implicitly or explicitly) other than what you have provided.

                  M 1 Reply Last reply
                  0
                  • N Nisamudheen

                    The other side of the story is that, you can prevent a constructor being called (implicitly or explicitly) other than what you have provided.

                    M Offline
                    M Offline
                    Manfr3d
                    wrote on last edited by
                    #9

                    I wrote a default constructor (empty and parameterless) and now it works. However, I do not know why; the default constructor is never called. By the way, I know that a default constructor is not any self written one^^

                    N 1 Reply Last reply
                    0
                    • M Manfr3d

                      I wrote a default constructor (empty and parameterless) and now it works. However, I do not know why; the default constructor is never called. By the way, I know that a default constructor is not any self written one^^

                      N Offline
                      N Offline
                      Nisamudheen
                      wrote on last edited by
                      #10

                      If you can provide a sample code, it will help to understand the problem well. Hope, your problem might be like the following one. class CTest { public: CTest( int a ) // since there is a constructor, the compiler won't generate // the default (no-argument) constructor. { } }; int main() { CTest Object; // Here, we are trying to invoke the no-argument constructor. // So the error C2512 comes. // To solve the problem, define a no-artument constructor. // CTest Object( 1 ); Otherwise, you should declare the object like this. return 0; }

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups