G++ Compilation Error Doesn't Make Sense


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: G++ Compilation Error Doesn't Make Sense

  1. #1
    Join Date
    Jun 2008
    Posts
    4

    G++ Compilation Error Doesn't Make Sense

    Hey guys,

    I'm quite confused here. My program works perfectly when compiled on Visual Studio 2008 but it doesn't compile on G++.

    I don't even understand the error. I know that the ifstream is part of the C++ standard library so it makes even less sense:

    isbnprefix.cpp: In function 'int registered(FILE*, int)':
    isbnprefix.cpp:32: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(FILE*&)'
    /usr/include/c++/4.1.2/fstream:442: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.1.2/fstream:428: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.1.2/iosfwd:89: note: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)
    isbnprefix.cpp: In function 'int minNoDigits(FILE*, int)':
    isbnprefix.cpp:42: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(FILE*&)'
    /usr/include/c++/4.1.2/fstream:442: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.1.2/fstream:428: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.1.2/iosfwd:89: note: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)
    isbnprefix.cpp: In function 'int registered(FILE*, int, int)':
    isbnprefix.cpp:65: error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(FILE*&)'
    /usr/include/c++/4.1.2/fstream:442: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.1.2/fstream:428: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/include/c++/4.1.2/iosfwd:89: note: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)
    Here's my isbnprefix.cpp:
    Code:
    /*  isbnprefix.cpp
     *  Assignment 1's ISBN prefix program file.
     *  Copyright (C) 2009 Adrien Kwok.
     *
     *  This program is free software: you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License Version 3
     *  as published by the Free Software Foundation.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program.  If not, see <[LINK REMOVED]>.
     *
     */
    #include "stdheader.h"
    #include "prefix.h"
    
    void seekStart(FILE* fp) {
    	fseek(fp, 0, SEEK_SET);
    }
    
    FILE* open(const char* filename){
    	FILE* fp = new FILE;
    	fp = fopen (filename, "r");
    	return fp;
    }
    int registered(FILE* fp, int area){
    	seekStart(fp);
    	ifstream in(fp);
    	while (!in.eof()) {
    		int area0, minPublisher, maxPublisher;
    		in >> area0 >> minPublisher >> maxPublisher;
    		if (area0 == area) return true;
    	}
    	return false;
    }
    int minNoDigits(FILE* fp, int area){
    	seekStart(fp);
    	ifstream in(fp);
    	while (!in.eof()) {
    		// read one line
    		int area0, minPublisher, maxPublisher;
    		in >> area0 >> minPublisher >> maxPublisher;
    		cout << maxPublisher;
    
    		// determine the max number of digits
    		if (area == area0) {
    			if (maxPublisher <= 9) return 1;
    			if (maxPublisher <= 99) return 2;
    			if (maxPublisher <= 999) return 3;
    			if (maxPublisher <= 9999) return 4;
    			if (maxPublisher <= 99999) return 5;
    			if (maxPublisher <= 999999) return 6;
    			return 7;
    		}	
    	}
    	
    	return 0;
    }
    int registered(FILE* fp, int area, int publisher){
    	seekStart(fp);
    	ifstream in(fp);
    	while (in) {
    		int area0, minPublisher, maxPublisher;
    		in >> area0 >> minPublisher >> maxPublisher;
    		if (area0 == area) {
    			if (minPublisher <= publisher && publisher <= maxPublisher) return true;
    		}
    	}
    	return false;
    }
    int close (FILE* fp){
    	int err = fclose(fp);
    	return err == 0 ? true : false;
    }
    and here's my stdheader.h:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <cmath>
    #include <fstream>
    #include <cstring>
    #include <cctype>
    #include <sstream>
    #include <strstream>
    using namespace std;
    Thanks,
    deltatux

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    C++ filestreams do not have constructors that take a C FILE*.

    however, the library is extensible; we can easily write our own stream/streambuf derived classes which support this.

    Boost.Iostreams library http://www.boost.org/doc/libs/1_37_0...doc/index.html does contain stream/streambuf derived classes which can be constructed using a file descriptor (HANDLE in windows). Boost is widely portable, so we could just use it instead of rolling out our own classes. here is an example:

    Code:
    #include <stdio.h>
    #include <boost/iostreams/device/file_descriptor.hpp>
    #include <boost/iostreams/stream.hpp>
    #include <iostream>
    #include <string>
    
    int main()
    {
          FILE* file = fopen( __FILE__, "r" ) ;
          int fd = fileno( file ) ; // posix
    
         using namespace boost::iostreams ;
         file_descriptor_source fdsource(fd) ;
         stream< file_descriptor_source > ifstm( fdsource ) ;
    
         std::istream& std_istm = ifstm ;
         std::cout << std_istm.rdbuf() ;
    }
    // link with the libboost_iostreams library
    // g++ -Wall -std=c++98 -pedantic -Werror -I /usr/local/include -L /usr/local/lib -lboost_iostreams myprogram.cc

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    Alternatively, you might want to consider replacing the FILE* interface with pure <fstream> classes, if that's an option.
    Danny Kalev

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    Quote Originally Posted by vijayan View Post
    snips...
    using a file descriptor (HANDLE in windows).

    Actually, HANDLE is a typedef for void*, and I refuse to acknowledge them (I blatently put in void *x = gethandle() in my code) as anything else. They can force me to use void pointers, but they cannot force me to hide from the fact.

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    This is odd. File descriptors are meant to be small integers such as 0,1,2 not pointers. Am I missing something here?
    Danny Kalev

  6. #6
    Join Date
    Dec 2007
    Posts
    401
    windows kernel abstractions and system resources (files, events, threads etc). are represented as kernel objects (EFILE, KEVENT, KTHREAD etc.)
    a kernel object is identified in a process by an entry in the per process handle table. opening a kernel object results in adding an entry for the object in the process handle table.

    each handle table entry is a tuple: ( pointer to kernel object, granted access mask ).
    bottom two bits of kernel HANDLEs are always initialized to zero; their numeric value is always a multiple of 4. the integer ULONG_PTR(HANDLE) / 4 is an index into this table (this index is a small integer value).
    though we have typedef void* HANDLE; a HANDLE is *not* a pointer. it is just a number that has the same size as a pointer.

    Code:
    // from ntdef.h:
    // -------------
    //
    // Low order two bits of a handle are ignored by the system and available
    // for use by application code as tag bits.  The remaining bits are opaque
    // and used to store a serial number and table index.
    //
    
    #define OBJ_HANDLE_TAGBITS  0x00000003L
    most programmers should treat a HANDLE as an opaque value used to identify a kernel object. tag bits are there to support people who implement low-level class libraries and want to wrap kernel objects inside a framework.

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    Well that explains some of the mystery behind the way they are used sometimes. I had always assumed it was a true pointer to an internal object that wrapped the low level stuff, of course not knowing what the void* 's type really is, I never tried to dereference one or set it to null or anything either. For the most part I have avoided them (as I avoid all OS specific code as much as I can), but once in a while its the only way I have found to do something (setting a process to realtime and drawing directly into a window come to mind).

    Thanks vijayan for clearing that up though. What were you quoting there (if anything), I will go read up on that if its an MSDN or other resource?

  8. #8
    Join Date
    Dec 2007
    Posts
    401
    each version of windows is quite different in how handle tables are structured. and these are not officially documented by microsoft (except for the pieces we can put together by going through the headers supplied with WINDDK; eg. ntdef.h (from which i posted an extract earlier).

    "Inside Microsoft Windows 2000" by David Solomon and Mark Russinovich http://www.amazon.com/Inside-Microso.../dp/0735610215 has 2 or 3 pages on handle tables.

    "Undocumented Windows 2000 Secrets: A Programmer's Cookbook" by Sven Schreiber http://www.amazon.com/exec/obidos/AS...330009-8512811 has more details.

    www.sysinternals.com used to be a great source of information; the forum is still good, but much of the informative source code, articles and documentation have vanished since microsoft acquired it.

Similar Threads

  1. Make sure to vote Bush (out of power!) in 2004
    By Fed-up unemployed in forum Careers
    Replies: 4
    Last Post: 01-22-2003, 12:28 PM
  2. How to make consistant site
    By tibbs in forum Architecture and Design
    Replies: 9
    Last Post: 03-01-2002, 03:12 AM
  3. Replies: 0
    Last Post: 03-24-2000, 11:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center