programing error in writing own manipulator.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sridhard2406
    New Member
    • Dec 2007
    • 52

    programing error in writing own manipulator.

    I am trying to create own manipulator, which will add the extra tab with input. Please find when I run the below program I got below mentioned error. could you please anybody help me, what is the wrong with the code?

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class addtab
    {
    
            addtab()
            {
                    cout << "constructer has called" << endl;
            }
            ~addtab()
            {
                    cout << "destructer has called" << endl;
            }
            public:
            friend ostream& operator<<(ostream& ,int&);
    };
    ostream& operator<<(ostream &os,int &n)
    {
            return os ;
    }
    
    int main()
    {
    
            int n;
            n  = 7;
            cout <<  "this " << addtab(n) << endl;
    }
    
    when I run this program, I am getting below mentioned error? please help me to clear my error.
    
    
    create_own_manu.cpp: In function `int main()':
    create_own_manu.cpp:30: no matching function for call to `addtab::addtab(int&)'
    create_own_manu.cpp:7: candidates are: addtab::addtab(const addtab&)
    create_own_manu.cpp:10:                 addtab::addtab(
    )
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    First, you do not 'run' it - it doesn't even compile, and in order to run it, you need to compile it. As the error message states, you want to use addtab::addtab( int) constructor in line 30, but there is no such constructor declared.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      Hi,
      Not a C++ programmer even though I am trying a bit.

      int main()
      {

      int n;
      n = 7;
      cout << "this " << addtab(n) << endl;
      }
      look at cout
      you tried addtab(n). Even if you made a constructor that receive an integer as parameter, you will get an error.
      why? cause if you call a function or whatever in cout it must have to return something(but not void)(and if I am not wrong). And you cant define any return type for a constructor.

      Best Regards,
      Johny

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        A manipulator must take only one argument, an ostream& and return and ostream&.

        Code:
        ostream& MyManipulator(ostream& os)
        {
        
          //do my stuff:
          os << etc....
        
           return os.
        
        }

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          oh I forgot to add in the first place

          int main()
          {

          int n;
          n = 7;
          cout << "this " << addtab(n) << endl;
          }
          addtab is a class not a object of any class. But It seems that you are tring to use it as variable! Not that clear to me :)

          What do you think :)

          Comment

          • sridhard2406
            New Member
            • Dec 2007
            • 52

            #6
            any body can tell me , can we write the own manipulator in c++, if we can , can you provide some links for Example/ some sample program..

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by johny10151981
              addtab is a class not a object of any class. But It seems that you are tring to use it as variable! Not that clear to me :)
              This is just fine. All objects are variables but some objects can be used as functions. These are called functors and they are classes that implement the function operator, operator()().


              Originally posted by sridhard2406
              can we write the own manipulator in c++, if we can
              Yes, but it has have one argument, ostream&, and it must return am ostream&:

              Code:
              ostream& StarBar(ostream& os)
              {
              os.write("*****", 5);
              return os;
              }
              
              
              cout << StarBar << "--" << StarBar<< endl;

              Comment

              • sridhard2406
                New Member
                • Dec 2007
                • 52

                #8
                can we write a own manipulater with single argument.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Yes.

                  However, your manipulator must have this prototype:

                  Code:
                  ostream&   MyManipulator(ostream&);

                  If your manipulator needs input values then you need to get them from elsewhere than through function arguments. You would normally use a Singleton object to get the values or maybe call a function that gets the values for you.

                  Comment

                  • sridhard2406
                    New Member
                    • Dec 2007
                    • 52

                    #10
                    Thanks for all your reply

                    Comment

                    Working...