Thread: ios_base functions not recognised in G++

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    133

    ios_base functions not recognised in G++

    Code:
    output.precision(2);
    output << prod_Id << '\t';
    output << setw(12) << left << prod_Name << '\t';
    output << fixed << price << '\t';
    output << tax_Cat << endl;
    I have worked this code in windows and i have to make this work in an UNIX environment using G++. However, the compiler doesnt seem to recognise "fixed"(to specify the precision of float variable price) and "left"(to left justify prod_Name). Both are member functions of base class ios_base. Can someone suggest me an alternative that can give me the same effect?

    i.e
    12345 <productName> 3.50 C

    Also, is there any free compiler that i can use in windows which ensure the code can compile in G++ under UNIX environment?

    Thanks for any assistance.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    To nearly ensure compilation in an Linux environment, use MingW or Cygwin. Both use a Windows-compatible version of GCC.

    Can I see more of the code, especially the start of the file?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Code:
    #include <iostream>
    #include <iomanip>


    Dave

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    Code:
    // libraries i included
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    // 
    void display(ostream &output) const
    {              output.precision(2);
    	output << prod_Id << '\t';
    	output.setf(ios::left);
    	output << setw(12) << prod_Name.c_str() << '\t';
    	
    	output.setf(ios::left);
    	output.setf(ios::fixed);
    	output << price << '\t';
    	output << tax_Cat << endl;
    }// end of display()
    I forgot to mention output is an ostream. Anyway, i managed to get it to work using this revised code. Trying to make a code that is portable in both windows and Unix is such a difficult task.

    I wonder what difference does setf() make. While trying to resolve the problem, i even encounter setioflag() which makes me wonder why are there so many variations for output manipulation.

    Lastly, thanks for recommending the compiler, i will try to look for it.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Could it have been a namespace issue? You were using the newer header files and you didn't say if you had any using namespace std; or using std::ostream; related lines in your code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    
    // this is a member function of a class
    void Product::display(ostream &output) const
    {
    	output << prod_Id << '\t';
    	output.setf(ios::left);
    	output << setw(12) << prod_Name.c_str() << '\t';
    	
    	output.setf(ios::left);
    	output.setf(ios::fixed);
    	output << price << '\t';
    	output << tax_Cat << endl;
    }// end of display()
    I did use namespace std. Maybe another thing worth mentioning. When i compile under Unix G++, it cant recognise the <limits> library.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Compiles fine on my Linux, using gcc 3.3.2
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM