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. Cross reference between classes

Cross reference between classes

Scheduled Pinned Locked Moved Managed C++/CLI
questionc++helpdata-structures
6 Posts 4 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.
  • S Offline
    S Offline
    Sci_fie
    wrote on last edited by
    #1

    Hello folks, Can someone help me with this question: How can I make this work? #include "stdafx.h" using namespace System; public ref class classX { private: classY ^m_refClassY; public: classX() { this->m_refClassY = gcnew classY(); } }; public ref class classY { private: classX ^m_refClassX; public: classY() { this->m_refClassX = gcnew classX(); } }; int main(array<System::String ^> ^args) { Console::WriteLine(L"Hello World"); return 0; } Always when I try to compile this source, this error comes up.

    cross_reference.cpp(10) : error C2143: syntax error : missing ';' before '^'
    cross_reference.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    cross_reference.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    cross_reference.cpp(15) : error C2039: 'm_refClassY' : is not a member of 'classX'
    cross_reference.cpp(8) : see declaration of 'classX'
    cross_reference.cpp(15) : error C2061: syntax error : identifier 'classY'

    N 1 Reply Last reply
    0
    • S Sci_fie

      Hello folks, Can someone help me with this question: How can I make this work? #include "stdafx.h" using namespace System; public ref class classX { private: classY ^m_refClassY; public: classX() { this->m_refClassY = gcnew classY(); } }; public ref class classY { private: classX ^m_refClassX; public: classY() { this->m_refClassX = gcnew classX(); } }; int main(array<System::String ^> ^args) { Console::WriteLine(L"Hello World"); return 0; } Always when I try to compile this source, this error comes up.

      cross_reference.cpp(10) : error C2143: syntax error : missing ';' before '^'
      cross_reference.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
      cross_reference.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
      cross_reference.cpp(15) : error C2039: 'm_refClassY' : is not a member of 'classX'
      cross_reference.cpp(8) : see declaration of 'classX'
      cross_reference.cpp(15) : error C2061: syntax error : identifier 'classY'

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Forward declaration is the solution for circular dependencies. See the corrected code

      ref class classY; // forward declaring classY

      public ref class classX
      {
      private:
      classY ^m_refClassY;

      public:
      classX();
      };

      public ref class classY
      {
      private:
      classX ^m_refClassX;
      public:
      classY();
      };

      classX::classX(){
      this->m_refClassY = gcnew classY();
      }

      classY::classY(){
      this->m_refClassX = gcnew classX();
      }

      In a real application, try to move the classes into separate files. :)

      Navaneeth How to use google | Ask smart questions

      S T 2 Replies Last reply
      0
      • N N a v a n e e t h

        Forward declaration is the solution for circular dependencies. See the corrected code

        ref class classY; // forward declaring classY

        public ref class classX
        {
        private:
        classY ^m_refClassY;

        public:
        classX();
        };

        public ref class classY
        {
        private:
        classX ^m_refClassX;
        public:
        classY();
        };

        classX::classX(){
        this->m_refClassY = gcnew classY();
        }

        classY::classY(){
        this->m_refClassX = gcnew classX();
        }

        In a real application, try to move the classes into separate files. :)

        Navaneeth How to use google | Ask smart questions

        S Offline
        S Offline
        Sci_fie
        wrote on last edited by
        #3

        In fact, my real source is in separate files, but I got the same problem. Anyway, with your answer I figured out what to do in this case too. Thanks. :thumbsup:

        1 Reply Last reply
        0
        • N N a v a n e e t h

          Forward declaration is the solution for circular dependencies. See the corrected code

          ref class classY; // forward declaring classY

          public ref class classX
          {
          private:
          classY ^m_refClassY;

          public:
          classX();
          };

          public ref class classY
          {
          private:
          classX ^m_refClassX;
          public:
          classY();
          };

          classX::classX(){
          this->m_refClassY = gcnew classY();
          }

          classY::classY(){
          this->m_refClassX = gcnew classX();
          }

          In a real application, try to move the classes into separate files. :)

          Navaneeth How to use google | Ask smart questions

          T Offline
          T Offline
          thenutz72
          wrote on last edited by
          #4

          Navaneeth (or anyone :), I posted this under "Circular Form Reference" subject but it's a very similar question. I had tried to add 'ref class Form2;' but now get a 'no appropriate default constructor available'. I was able to recreate my issue by simply creating two forms and adding a button to each that call the other form. So most of the code is the automaticly generated code from MS. I'm trying to keep the example simple by only incl. some code. /*Form1.h*/ #pragma once #ifndef First #define First #include "Form2.h" ref class Form2; private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } #endif /*Form2.h*/ #pragma once #ifndef Second #define Second #include "Form1.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); } #endif

          modified on Tuesday, March 24, 2009 1:30 PM

          M S 2 Replies Last reply
          0
          • T thenutz72

            Navaneeth (or anyone :), I posted this under "Circular Form Reference" subject but it's a very similar question. I had tried to add 'ref class Form2;' but now get a 'no appropriate default constructor available'. I was able to recreate my issue by simply creating two forms and adding a button to each that call the other form. So most of the code is the automaticly generated code from MS. I'm trying to keep the example simple by only incl. some code. /*Form1.h*/ #pragma once #ifndef First #define First #include "Form2.h" ref class Form2; private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } #endif /*Form2.h*/ #pragma once #ifndef Second #define Second #include "Form1.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); } #endif

            modified on Tuesday, March 24, 2009 1:30 PM

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

            You'll probably need to separate your code out of the H files into CPP files for at least one of your classes.

            /*Form1.h*/
            #pragma once

            ref class Form2;

            ref class Form1 : public System::Windows::Forms::Form
            {
            private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e);
            }

            /*Form1.cpp*/
            #include "Form1.h"
            #include "Form2.h"
            private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
            {
            Form2 ^ frm = gcnew Form2;
            frm->Show();
            this->Close();
            }

            /*Form2.h*/
            #pragma once
            #include "Form1.h"

            private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
            {
            Form1 ^ frm = gcnew Form1;
            frm->Show();
            this->Close();
            }

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

            1 Reply Last reply
            0
            • T thenutz72

              Navaneeth (or anyone :), I posted this under "Circular Form Reference" subject but it's a very similar question. I had tried to add 'ref class Form2;' but now get a 'no appropriate default constructor available'. I was able to recreate my issue by simply creating two forms and adding a button to each that call the other form. So most of the code is the automaticly generated code from MS. I'm trying to keep the example simple by only incl. some code. /*Form1.h*/ #pragma once #ifndef First #define First #include "Form2.h" ref class Form2; private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } #endif /*Form2.h*/ #pragma once #ifndef Second #define Second #include "Form1.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); } #endif

              modified on Tuesday, March 24, 2009 1:30 PM

              S Offline
              S Offline
              Sci_fie
              wrote on last edited by
              #6

              Hi, To do what do you want, you´ll have to do forward declaration, in the same way explained by Navaneeth. /*Form1.h*/ #pragma once ref class Form2; // forward declaration /* prototype */ private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e); /*Form1.cpp*/ #include "Form1.h" #include "Form2.h" // here, you have to add the real declaration. System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } /*Form2.h*/ #pragma once #include "Form1.h" // Can be here becouse Form1.h don´t have a referece to this file. /* Can be inline */ private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); }

              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