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. constructor failed??? [modified]

constructor failed??? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
23 Posts 6 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.
  • D dealon

    Hello all. I have this code with something error occured: //------------------------------------------ #include "iostream" using namespace std; class aa { public: aa() { aa(5); } aa(int t) { x = t; } void print() { cout << x << endl; } private: int x; }; int main() { aa t; t.print();//this can't output "5", why? return 0; } ///--------------------------- As is descriped above, thanks in advance!

    If we dream, every thing is possible!

    modified on Thursday, June 5, 2008 3:31 AM

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #3

    and WHAT IS the error occuring dude ?! :doh: BTW, i see a problem in your cout statements. cout cannot use the >> operator. it uses << operator only, so change your code into this :

    void print() {
    cout << x << endl;
    }

    and everywhere you're doing such a mistake...

    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

    C 1 Reply Last reply
    0
    • D dealon

      Hello all. I have this code with something error occured: //------------------------------------------ #include "iostream" using namespace std; class aa { public: aa() { aa(5); } aa(int t) { x = t; } void print() { cout << x << endl; } private: int x; }; int main() { aa t; t.print();//this can't output "5", why? return 0; } ///--------------------------- As is descriped above, thanks in advance!

      If we dream, every thing is possible!

      modified on Thursday, June 5, 2008 3:31 AM

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #4

      You made a mistake in the default constructor. Can you spot it? BTW: The correct method is

      class aa
      {
      public:
      aa():x(5)
      {
      }
      //...
      };

      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

      D 1 Reply Last reply
      0
      • C Cedric Moonen

        Could you please elaborate the problem ? What's wrong ? And please use the "code block" option when posting code (all of that is clearly explained in the posting guidelines).

        Cédric Moonen Software developer
        Charting control [v1.4]

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #5

        (excluding << and >> operators usage!) He creates a temporary object (calling aa(5)) inside the default constructor, probably he wanted to initialize x with 5 instead. :)

        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

        1 Reply Last reply
        0
        • T toxcct

          and WHAT IS the error occuring dude ?! :doh: BTW, i see a problem in your cout statements. cout cannot use the >> operator. it uses << operator only, so change your code into this :

          void print() {
          cout << x << endl;
          }

          and everywhere you're doing such a mistake...

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

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

          He creates a temporary object (calling aa(5)) inside the default constructor (leaving garbage inside x), probably he wanted to initialize x with 5 instead. :)

          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

          T 1 Reply Last reply
          0
          • C CPallini

            He creates a temporary object (calling aa(5)) inside the default constructor (leaving garbage inside x), probably he wanted to initialize x with 5 instead. :)

            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

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #7

            thanks dear. but i saw your answer, so no need to flood the thread ;P :cool:

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            C 1 Reply Last reply
            0
            • D dealon

              Hello all. I have this code with something error occured: //------------------------------------------ #include "iostream" using namespace std; class aa { public: aa() { aa(5); } aa(int t) { x = t; } void print() { cout << x << endl; } private: int x; }; int main() { aa t; t.print();//this can't output "5", why? return 0; } ///--------------------------- As is descriped above, thanks in advance!

              If we dream, every thing is possible!

              modified on Thursday, June 5, 2008 3:31 AM

              S Offline
              S Offline
              ShilpiP
              wrote on last edited by
              #8

              Is This program working ???

              cout>>x>>endl;
              cin<<ch;

              try using debuging than you find that why x is not giving right output :) #include "stdafx.h" #include <iostream> using namespace std;

              class aa
              {

              public:
              
              	aa(int t = 5)
              	{
              		x = t;
              	}
              	void print()
              	{
              		cout<<x <<endl;
              	
              	}
              private:
              	int x;
              

              };
              int main(int argc, char* argv[])
              {
              aa t;
              t.print();
              cout<<"---------------------"<<endl;

              aa t1(4);
              t1.print();
              char ch;
              cin>>ch;
              
              
              return 0;
              

              }

              Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

              modified on Thursday, June 5, 2008 3:41 AM

              T D 2 Replies Last reply
              0
              • T toxcct

                thanks dear. but i saw your answer, so no need to flood the thread ;P :cool:

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #9

                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ;P ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                toxcct wrote:

                so no need to flood the thread

                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ;P ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                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

                H 1 Reply Last reply
                0
                • C CPallini

                  You made a mistake in the default constructor. Can you spot it? BTW: The correct method is

                  class aa
                  {
                  public:
                  aa():x(5)
                  {
                  }
                  //...
                  };

                  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

                  D Offline
                  D Offline
                  dealon
                  wrote on last edited by
                  #10

                  Thank you Pallini.I got it... But i found this code can't work. class aa { public: aa():x(5) { }//... }; it said x(5) is not a base class. Thanks once again! :)

                  If we dream, every thing is possible!

                  T C 2 Replies Last reply
                  0
                  • S ShilpiP

                    Is This program working ???

                    cout>>x>>endl;
                    cin<<ch;

                    try using debuging than you find that why x is not giving right output :) #include "stdafx.h" #include <iostream> using namespace std;

                    class aa
                    {

                    public:
                    
                    	aa(int t = 5)
                    	{
                    		x = t;
                    	}
                    	void print()
                    	{
                    		cout<<x <<endl;
                    	
                    	}
                    private:
                    	int x;
                    

                    };
                    int main(int argc, char* argv[])
                    {
                    aa t;
                    t.print();
                    cout<<"---------------------"<<endl;

                    aa t1(4);
                    t1.print();
                    char ch;
                    cin>>ch;
                    
                    
                    return 0;
                    

                    }

                    Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                    modified on Thursday, June 5, 2008 3:41 AM

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #11

                    Shilpi Boosar wrote:

                    cout>>x>>endl;cin<<ch;

                    you're not serious, are you ? :~ X| when you don't know what you're talking about, please avoid posting crap, that will avoid the question poster to be confused at last.

                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    D S 2 Replies Last reply
                    0
                    • S ShilpiP

                      Is This program working ???

                      cout>>x>>endl;
                      cin<<ch;

                      try using debuging than you find that why x is not giving right output :) #include "stdafx.h" #include <iostream> using namespace std;

                      class aa
                      {

                      public:
                      
                      	aa(int t = 5)
                      	{
                      		x = t;
                      	}
                      	void print()
                      	{
                      		cout<<x <<endl;
                      	
                      	}
                      private:
                      	int x;
                      

                      };
                      int main(int argc, char* argv[])
                      {
                      aa t;
                      t.print();
                      cout<<"---------------------"<<endl;

                      aa t1(4);
                      t1.print();
                      char ch;
                      cin>>ch;
                      
                      
                      return 0;
                      

                      }

                      Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                      modified on Thursday, June 5, 2008 3:41 AM

                      D Offline
                      D Offline
                      dealon
                      wrote on last edited by
                      #12

                      Thans Shilpi. I'm so sorry.It's my fault. I have edited my post... :)

                      If we dream, every thing is possible!

                      S 1 Reply Last reply
                      0
                      • D dealon

                        Thank you Pallini.I got it... But i found this code can't work. class aa { public: aa():x(5) { }//... }; it said x(5) is not a base class. Thanks once again! :)

                        If we dream, every thing is possible!

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #13

                        it's because in this class, you don't define a x member...

                        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                        1 Reply Last reply
                        0
                        • T toxcct

                          Shilpi Boosar wrote:

                          cout>>x>>endl;cin<<ch;

                          you're not serious, are you ? :~ X| when you don't know what you're talking about, please avoid posting crap, that will avoid the question poster to be confused at last.

                          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                          D Offline
                          D Offline
                          dealon
                          wrote on last edited by
                          #14

                          Sorry toxcct.It's my fault. I have edited my post. Thanks youtoxcct.

                          If we dream, every thing is possible!

                          1 Reply Last reply
                          0
                          • D dealon

                            Thank you Pallini.I got it... But i found this code can't work. class aa { public: aa():x(5) { }//... }; it said x(5) is not a base class. Thanks once again! :)

                            If we dream, every thing is possible!

                            C Offline
                            C Offline
                            CPallini
                            wrote on last edited by
                            #15

                            In my sample

                            //...

                            was the concise notation for

                            aa(int t)
                            {
                            x = t;
                            }
                            void print()
                            {
                            cout << x << endl;
                            }
                            private:
                            int x;
                            };

                            :)

                            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

                            1 Reply Last reply
                            0
                            • T toxcct

                              Shilpi Boosar wrote:

                              cout>>x>>endl;cin<<ch;

                              you're not serious, are you ? :~ X| when you don't know what you're talking about, please avoid posting crap, that will avoid the question poster to be confused at last.

                              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                              S Offline
                              S Offline
                              ShilpiP
                              wrote on last edited by
                              #16

                              I think now you got the answer why i write that code. :)

                              Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                              T D 2 Replies Last reply
                              0
                              • D dealon

                                Thans Shilpi. I'm so sorry.It's my fault. I have edited my post... :)

                                If we dream, every thing is possible!

                                S Offline
                                S Offline
                                ShilpiP
                                wrote on last edited by
                                #17

                                Its ok dealon :) He also dont know about this.

                                Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                                1 Reply Last reply
                                0
                                • S ShilpiP

                                  I think now you got the answer why i write that code. :)

                                  Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                                  T Offline
                                  T Offline
                                  toxcct
                                  wrote on last edited by
                                  #18

                                  nope. i still don't understand why you used the wrong operators... cout use <<, not >>

                                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                  S 1 Reply Last reply
                                  0
                                  • S ShilpiP

                                    I think now you got the answer why i write that code. :)

                                    Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                                    D Offline
                                    D Offline
                                    dealon
                                    wrote on last edited by
                                    #19

                                    Thank you very much, Shilpi. :)

                                    If we dream, every thing is possible!

                                    S 1 Reply Last reply
                                    0
                                    • T toxcct

                                      nope. i still don't understand why you used the wrong operators... cout use <<, not >>

                                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                      S Offline
                                      S Offline
                                      ShilpiP
                                      wrote on last edited by
                                      #20

                                      Actually he write the wrong code and I just do write his wrong code to explain him where he is wrong.:) I just explain him that his code is not working. LOL :laugh:

                                      Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                                      1 Reply Last reply
                                      0
                                      • D dealon

                                        Thank you very much, Shilpi. :)

                                        If we dream, every thing is possible!

                                        S Offline
                                        S Offline
                                        ShilpiP
                                        wrote on last edited by
                                        #21

                                        Most Welcome :)

                                        Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

                                        1 Reply Last reply
                                        0
                                        • C CPallini

                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ;P ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                                          toxcct wrote:

                                          so no need to flood the thread

                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ ;P ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                                          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

                                          H Offline
                                          H Offline
                                          Hamid Taebi
                                          wrote on last edited by
                                          #22

                                          Oh what happend for your thread I give you 5 but I think it needs to repair. :laugh:

                                          C 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