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. Managed C++/CLI
  4. Forward Declaration and no appropriate default constructor

Forward Declaration and no appropriate default constructor

Scheduled Pinned Locked Moved Managed C++/CLI
helpcsharpc++visual-studioquestion
15 Posts 5 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.
  • B Offline
    B Offline
    BuckBrown
    wrote on last edited by
    #1

    Hi (Luc), I am using Visual Studio 2005 C++/CLI and I am having a problem with a constructor but it looks okay to me. Can anyone tell if this is because I am using a forward declaration? namespace MySpace { ref class Class2; // Here is the forward declaration public ref class Class1 : Form { public: Class1(Form form) { Class1 constructor code here } public: void Print() { Class2^ my_class2 = gcnew Class2^(this); // This is causing error } }; // end of Class1 public ref class Class2 : PrintDocument { public: Class2(Class1^ class_arg){ Class2 constructor code here } }; // end of Class2 } // end of MySpace namespace If the single line in the Print() function is remarked out this compiles fine but if it is unremarked out then I get a "no appropriate default constructor" error. I don't see what's wrong with this constructor, does anyone else? Thanks Buck

    M 1 Reply Last reply
    0
    • B BuckBrown

      Hi (Luc), I am using Visual Studio 2005 C++/CLI and I am having a problem with a constructor but it looks okay to me. Can anyone tell if this is because I am using a forward declaration? namespace MySpace { ref class Class2; // Here is the forward declaration public ref class Class1 : Form { public: Class1(Form form) { Class1 constructor code here } public: void Print() { Class2^ my_class2 = gcnew Class2^(this); // This is causing error } }; // end of Class1 public ref class Class2 : PrintDocument { public: Class2(Class1^ class_arg){ Class2 constructor code here } }; // end of Class2 } // end of MySpace namespace If the single line in the Print() function is remarked out this compiles fine but if it is unremarked out then I get a "no appropriate default constructor" error. I don't see what's wrong with this constructor, does anyone else? Thanks Buck

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      There's an extra ^ in there :) Plus, the compiler won't be able to resolve the parameterized constructor. Can you do something like this?

      namespace MySpace {
         public ref class Class1 : Form
            {
            public: Class1(Form form) { Class1 constructor code here }
            public: void Print();
            };  // end of Class1

      public ref class Class2 : PrintDocument
            {
            public: Class2(Class1^ class_arg){ Class2 constructor code here }
            };  // end of Class2

      inline void Class1::Print()
         {
            Class2^ my_class2 = gcnew Class2(this);
         }
      }  // end of MySpace namespace

      Mark


      Last modified: 12mins after originally posted --

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      B 1 Reply Last reply
      0
      • M Mark Salsbery

        There's an extra ^ in there :) Plus, the compiler won't be able to resolve the parameterized constructor. Can you do something like this?

        namespace MySpace {
           public ref class Class1 : Form
              {
              public: Class1(Form form) { Class1 constructor code here }
              public: void Print();
              };  // end of Class1

        public ref class Class2 : PrintDocument
              {
              public: Class2(Class1^ class_arg){ Class2 constructor code here }
              };  // end of Class2

        inline void Class1::Print()
           {
              Class2^ my_class2 = gcnew Class2(this);
           }
        }  // end of MySpace namespace

        Mark


        Last modified: 12mins after originally posted --

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        B Offline
        B Offline
        BuckBrown
        wrote on last edited by
        #3

        Yes, that did allow me to get the code to compile but it started as someone elses code and now I'm just confused as to why this was supposed to help. I just wanted my Print() and PrintPreview() to have the correct information (size wise) be displayed by PrintPreview() and printed by Print(). I think I'll ditch this whole idea for awhile. Thanks Buck

        M 1 Reply Last reply
        0
        • B BuckBrown

          Yes, that did allow me to get the code to compile but it started as someone elses code and now I'm just confused as to why this was supposed to help. I just wanted my Print() and PrintPreview() to have the correct information (size wise) be displayed by PrintPreview() and printed by Print(). I think I'll ditch this whole idea for awhile. Thanks Buck

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          BuckBrown wrote:

          but it started as someone elses code and now I'm just confused as to why this was supposed to help

          Fun fun! :)

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          L 1 Reply Last reply
          0
          • M Mark Salsbery

            BuckBrown wrote:

            but it started as someone elses code and now I'm just confused as to why this was supposed to help

            Fun fun! :)

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi Mark, It seems I have been providing the fun here, I suggested Buck to look into my Sokoban article for printing, but then that's in C# and does not know about circular reference problems... :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            G 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi Mark, It seems I have been providing the fun here, I suggested Buck to look into my Sokoban article for printing, but then that's in C# and does not know about circular reference problems... :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


              G Offline
              G Offline
              George L Jackson
              wrote on last edited by
              #6

              It gets worse when you start using Generics in C++/CLI in this situation! :((

              "We make a living by what we get, we make a life by what we give." --Winston Churchill

              L 1 Reply Last reply
              0
              • G George L Jackson

                It gets worse when you start using Generics in C++/CLI in this situation! :((

                "We make a living by what we get, we make a life by what we give." --Winston Churchill

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi George, thanks for the warning. I haven't used generics much yet, no problems so far... :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                G 1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi George, thanks for the warning. I haven't used generics much yet, no problems so far... :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                  G Offline
                  G Offline
                  George L Jackson
                  wrote on last edited by
                  #8

                  I guess you know how to keep yourself away from big issues; however, I don't. In certain situations, even with formal coding techniques, the compiler will still view your class (with a base clase of Foo) as a basic ref class with just a base class of Object. The only way I was able to get the code to compile was to create an IFoo generic interface and made Foo a Template class. Geo

                  "We make a living by what we get, we make a life by what we give." --Winston Churchill

                  I 1 Reply Last reply
                  0
                  • G George L Jackson

                    I guess you know how to keep yourself away from big issues; however, I don't. In certain situations, even with formal coding techniques, the compiler will still view your class (with a base clase of Foo) as a basic ref class with just a base class of Object. The only way I was able to get the code to compile was to create an IFoo generic interface and made Foo a Template class. Geo

                    "We make a living by what we get, we make a life by what we give." --Winston Churchill

                    I Offline
                    I Offline
                    iddqd515
                    wrote on last edited by
                    #9

                    That's interesting. There's a bug where the C++/CLI compiler won't recognize that a managed array implements System::Array. The result is you can't pass a managed array to a function that takes an argument as IEnumerable^ or something similar. What you're saying makes it sound like this bug might just be a more specific case of a general problem the C++/CLI compiler has with generics. Unless I'm misunderstanding you.

                    G 1 Reply Last reply
                    0
                    • I iddqd515

                      That's interesting. There's a bug where the C++/CLI compiler won't recognize that a managed array implements System::Array. The result is you can't pass a managed array to a function that takes an argument as IEnumerable^ or something similar. What you're saying makes it sound like this bug might just be a more specific case of a general problem the C++/CLI compiler has with generics. Unless I'm misunderstanding you.

                      G Offline
                      G Offline
                      George L Jackson
                      wrote on last edited by
                      #10

                      iddqd515 wrote:

                      There's a bug where the C++/CLI compiler won't recognize that a managed array implements System::Array. The result is you can't pass a managed array to a function that takes an argument as IEnumerable^ or something similar. What you're saying makes it sound like this bug might just be a more specific case of a general problem the C++/CLI compiler has with generics.

                      I don't know if what I said applies to that. Nevertheless, translating C# code to C++/CLI may require some extensive hoop jumping. On the other hand, with the help of templates and other C++ idiosyncrasies, you can write code not possible in C#. Since I don't currently have time to write articles, I have been posting in my blog on Code Project some C++/CLI code and brief comments. I will be adding some code on this as soon as my wife allows me to. Any, any comments good, bad or indifferent are welcomed. -- modified at 10:57 Tuesday 21st August, 2007

                      "We make a living by what we get, we make a life by what we give." --Winston Churchill

                      I 1 Reply Last reply
                      0
                      • G George L Jackson

                        iddqd515 wrote:

                        There's a bug where the C++/CLI compiler won't recognize that a managed array implements System::Array. The result is you can't pass a managed array to a function that takes an argument as IEnumerable^ or something similar. What you're saying makes it sound like this bug might just be a more specific case of a general problem the C++/CLI compiler has with generics.

                        I don't know if what I said applies to that. Nevertheless, translating C# code to C++/CLI may require some extensive hoop jumping. On the other hand, with the help of templates and other C++ idiosyncrasies, you can write code not possible in C#. Since I don't currently have time to write articles, I have been posting in my blog on Code Project some C++/CLI code and brief comments. I will be adding some code on this as soon as my wife allows me to. Any, any comments good, bad or indifferent are welcomed. -- modified at 10:57 Tuesday 21st August, 2007

                        "We make a living by what we get, we make a life by what we give." --Winston Churchill

                        I Offline
                        I Offline
                        iddqd515
                        wrote on last edited by
                        #11

                        I forgot to read what I posted. I meant a function with an argument of the generic interface IEnumerable^ not IEnumerable^. That's what prompted me to connect your problem with that bug. I'm about to try fiddling with it.

                        G 1 Reply Last reply
                        0
                        • I iddqd515

                          I forgot to read what I posted. I meant a function with an argument of the generic interface IEnumerable^ not IEnumerable^. That's what prompted me to connect your problem with that bug. I'm about to try fiddling with it.

                          G Offline
                          G Offline
                          George L Jackson
                          wrote on last edited by
                          #12

                          Visit my blog. I did address something about that and it may or may not be helpful: http://www.codeproject.com/script/profile/whos_who.asp?id=207715[^]

                          "We make a living by what we get, we make a life by what we give." --Winston Churchill

                          I 1 Reply Last reply
                          0
                          • G George L Jackson

                            Visit my blog. I did address something about that and it may or may not be helpful: http://www.codeproject.com/script/profile/whos_who.asp?id=207715[^]

                            "We make a living by what we get, we make a life by what we give." --Winston Churchill

                            I Offline
                            I Offline
                            iddqd515
                            wrote on last edited by
                            #13

                            Interesting stuff, but I'm not sure its quite along the lines I'm looking for. Do you have a code example or solution that demonstrates the problem where the compiler doesn't recognize the proper base class of the derived class and just thinks its a System::Object?

                            G 1 Reply Last reply
                            0
                            • I iddqd515

                              Interesting stuff, but I'm not sure its quite along the lines I'm looking for. Do you have a code example or solution that demonstrates the problem where the compiler doesn't recognize the proper base class of the derived class and just thinks its a System::Object?

                              G Offline
                              G Offline
                              George L Jackson
                              wrote on last edited by
                              #14

                              Yes I do. I will try to post it as soon as I get time! Also, the problem you refering to is something like the following where I have to use a safe_cast to get it to work:

                              using namespace System;
                              using namespace System::Collections::Generic;

                              generic <typename T>
                              void Foo(IEnumerable<T>^ col)
                              {
                              for each (T val in col)
                              {
                              Console::WriteLine(val);
                              }
                              }

                              int main(array<System::String ^> ^args)
                              {
                              array<int>^ arr = {10, 20, 30, 40, 50};

                              Foo(safe\_cast<IEnumerable<int>^>(arr));
                              
                              return 0;
                              

                              }

                              "We make a living by what we get, we make a life by what we give." --Winston Churchill

                              I 1 Reply Last reply
                              0
                              • G George L Jackson

                                Yes I do. I will try to post it as soon as I get time! Also, the problem you refering to is something like the following where I have to use a safe_cast to get it to work:

                                using namespace System;
                                using namespace System::Collections::Generic;

                                generic <typename T>
                                void Foo(IEnumerable<T>^ col)
                                {
                                for each (T val in col)
                                {
                                Console::WriteLine(val);
                                }
                                }

                                int main(array<System::String ^> ^args)
                                {
                                array<int>^ arr = {10, 20, 30, 40, 50};

                                Foo(safe\_cast<IEnumerable<int>^>(arr));
                                
                                return 0;
                                

                                }

                                "We make a living by what we get, we make a life by what we give." --Winston Churchill

                                I Offline
                                I Offline
                                iddqd515
                                wrote on last edited by
                                #15

                                yeah that's exactly the problem. Alternatively you could call Foo(arr). The C# compiler properly recognizes that an array is System::Array so it doesn't require that cast. I don't believe there's any way to dynamically get the generic type argument with typeof or GetType(). It seems this would make it quite difficult to use generics dynamically based on user input or something similar.

                                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