473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class addtab
  7. {
  8.  
  9.         addtab()
  10.         {
  11.                 cout << "constructer has called" << endl;
  12.         }
  13.         ~addtab()
  14.         {
  15.                 cout << "destructer has called" << endl;
  16.         }
  17.         public:
  18.         friend ostream& operator<<(ostream& ,int&);
  19. };
  20. ostream& operator<<(ostream &os,int &n)
  21. {
  22.         return os ;
  23. }
  24.  
  25. int main()
  26. {
  27.  
  28.         int n;
  29.         n  = 7;
  30.         cout <<  "this " << addtab(n) << endl;
  31. }
  32.  
  33. when I run this program, I am getting below mentioned error? please help me to clear my error.
  34.  
  35.  
  36. create_own_manu.cpp: In function `int main()':
  37. create_own_manu.cpp:30: no matching function for call to `addtab::addtab(int&)'
  38. create_own_manu.cpp:7: candidates are: addtab::addtab(const addtab&)
  39. create_own_manu.cpp:10:                 addtab::addtab(
  40. )
Feb 8 '10 #1
9 2469
newb16
687 512MB
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.
Feb 8 '10 #2
johny10151981
1,059 1GB
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
Feb 8 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
A manipulator must take only one argument, an ostream& and return and ostream&.

Expand|Select|Wrap|Line Numbers
  1. ostream& MyManipulator(ostream& os)
  2. {
  3.  
  4.   //do my stuff:
  5.   os << etc....
  6.  
  7.    return os.
  8.  
  9. }
Feb 8 '10 #4
johny10151981
1,059 1GB
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 :)
Feb 9 '10 #5
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..
Feb 9 '10 #6
weaknessforcats
9,208 Expert Mod 8TB
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()().


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&:

Expand|Select|Wrap|Line Numbers
  1. ostream& StarBar(ostream& os)
  2. {
  3. os.write("*****", 5);
  4. return os;
  5. }
  6.  
  7.  
  8. cout << StarBar << "--" << StarBar<< endl;
Feb 10 '10 #7
can we write a own manipulater with single argument.
Feb 11 '10 #8
weaknessforcats
9,208 Expert Mod 8TB
Yes.

However, your manipulator must have this prototype:

Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 11 '10 #9
Thanks for all your reply
Feb 15 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Steven T. Hatton | last post by:
I tried to create my own manipulator that would both set the width of the subsequent output field, and cast an unsigned char to unsigned in. I came up with the following rather ugly hack. Notice...
8
by: stoptv | last post by:
Hello group, this is my dilemma: ------------------------------------------------------------------------ #include <iostream> using namespace std; // a regular manipulator ostream & hello(...
6
by: jack | last post by:
I have a class which overloads the insertion operator '<<' for every type that ostream handles. I do this so that I can use my class a direct replacement for cout and cerr where the insertion...
13
by: Xah Lee | last post by:
there's this one i can't figure out. I'm trying to kick out image.google.com's top frame when it goes to my page. If you go to http://images.google.com/images?q=milk+tits click on the page...
12
by: Xah Lee | last post by:
Frameset Infinity! http://xahlee.org/js/frame2/frameset.html HTML Frame tutorial + Infinity! http://xahlee.org/js/frame/0.html Xah xah@xahlee.org ∑ http://xahlee.org/
2
by: youpak2000 | last post by:
What you can't find in programing text books Professional software development needs more knowledge than language syntax, OOP, styles, etc. There are many things which people usually learn by...
2
Colloid Snake
by: Colloid Snake | last post by:
Hello, I was attempting to configure log4cpp to monitor some logs on my *nix box, and when I run 'make' I get this error message: # make g++ -DHAVE_CONFIG_H -I. -I. -I../include...
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
9
by: barcaroller | last post by:
When I use an iostream manipulator in a cout statement, it seems to affect all subsequent cout statements. cout << x << endl; // dec by default cout << hex << x << endl; // hex cout <<...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.