Jump to content

G++ Compilation Error Doesn't Make Sense

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
deltatux

deltatux

    Newbie

  • Members
  • PipPip
  • 17 posts
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:

Quote

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

deltatux

#2
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
That means very little to us; It essentially just tells us that your trying to pass a FILE handle to a ifstream as its constructor... If you can, please post your code, that way we can post a solution.

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
One thing to be aware of is that Visual Studio creates a project file that helps coordinate the compilation of all the files. If you have a multi-file project, Visual Studio automates what you have to do manually with G++.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
deltatux

deltatux

    Newbie

  • Members
  • PipPip
  • 17 posts
Hey guys, thanks for the quick reply. Here's the file:


/*  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;

}


It shouldn't error out since I'm quite sure that there's no VS specific code in here since I followed basic C++ here.

Thanks in advance,
deltatux

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You haven't specified that you're using the std namespace, so ifstream resolves to a (nonexistent) local declaration of ifstream.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
deltatux

deltatux

    Newbie

  • Members
  • PipPip
  • 17 posts
stdheader.h imports everything including to tell it to use namespace std;.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <cstring>
#include <cctype>
#include <sstream>
#include <strstream>
using namespace std;

Thanks,
deltatux

#7
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
Well, that's a bit of a lie :) Standard C++ does NOT allow the use of a FILE handle as a constructor to an ifstream, hence:
ifstream in(fp);
fails. I'm kinda confused as to why your even mixing FILE and fstream's ?_?

#8
deltatux

deltatux

    Newbie

  • Members
  • PipPip
  • 17 posts

TkTech said:

Well, that's a bit of a lie :) Standard C++ does NOT allow the use of a FILE handle as a constructor to an ifstream, hence:

ifstream in(fp);

fails. I'm kinda confused as to why your even mixing FILE and fstream's ?_?

Well, the prof requires to use the C-style FILE* in our prototype.

Any suggest fixes?

Thanks,
deltatux

#9
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
your going to have to use the standard fopen/fclose/fread/fwrite/fseek...ect functions instead of using fstreams.