Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-02-2013, 02:11 AM   #1
LQ Newbie
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
Having problems compiling my .cpp file on Linux

For some reason my code is not compiling on a linux server. It gives me several ofstream, ifstream, and fstream errors. It compiles perfectly fine on a windows machine. I have never tried to compile a c++ program on linux before so this could be a dumb mistake.

This is just a simple program that copies one file to another file and checks for different errors.

Thanks for any help!

#include <iostream>
#include <fstream>
#include <string>

bool fileExists(const std::string);

//Bool function to test if output file already exists
bool fileExists(const std::string fileName)
	std::ifstream infile(fileName);
	return infile.good();

int main()
	std::ifstream file_in;//declares the input file
	std::ofstream file_out;//declares the output file
	std::string filename_in, filename_out;//declares the file strings
	//Name prompt for input file
	std::cout<<"What is the name of the input file?\n";

	//Opens the input file, std::ios::in);

	//Error if input file fails
		//Gives error message and aborts
		std::cout<<"\nThe filename "<<filename_in<<" could not be read.\n";
		std::cout<<"Possible errors include:\n";
		std::cout<<"A - The file-path was not found or\n";
		std::cout<<"B - The file does not exist.\n\n";

	std::cout<<"What is the name of the output file?\n";

	//Checks to see if a file already exists
		//Gives error message and aborts
		std::cout<<"\nThe filename "<<filename_out<<" already exists!\n";
		std::cout<<"ABORTING FILE COPY!\n\n";

	//Opens the output file for writing, std::ios::out);

	//Loop that reads input and writes to output file
	char inp;
		//reads the input char
		//does not read eof char on last loop

	//closes files

	std::cout<<"\nCopy Successful!\n";//Success message

	return 0;
Old 02-02-2013, 03:13 AM   #2
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
I have to admmit that the c++ compiler errors are sometimes somewhat hard to read, especially because very long type names and function prototypes. They are, however quite clear:

main.cpp: In function ‘bool fileExists(std::string)’:
main.cpp:10:31: error: no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(const string&)’
main.cpp:10:31: note: candidates are:
In file included from main.cpp:2:0:
/usr/lib64/gcc/x86_64-slackware-linux/4.7.1/../../../../include/c++/4.7.1/fstream:460:7: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
/usr/lib64/gcc/x86_64-slackware-linux/4.7.1/../../../../include/c++/4.7.1/fstream:460:7: note:   no known conversion for argument 1 from ‘const string {aka const std::basic_string<char>}’ to ‘const char*’
fstream has no constructor that takes a string as an argument. It takes const char* instead. The compiler does not know how to convert string to const char*.
You can do this by calling the method string::c_str(). So,

std::ifstream infile(fileName.c_str());
should solve the problem. The same bug appears in several other places in the code. Another error is:

main.cpp:36:3: error: ‘system’ is not a member of ‘std’
main.cpp:37:9: error: ‘exit’ was not declared in this scope
These require

#include <cstdlib>
Anyway, system("PAUSE"); is a non-portable windows bad habit that will not work in linux. There are many threads around that discuss this, so just search for them.

Another note:
bool fileExists(const std::string);
I would recommend to pass strings (and other large objects) by reference, rather than by value to avoid unnecessary copying.

bool fileExists(const std::string&);
Old 02-02-2013, 09:51 AM   #3
LQ Newbie
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Perfect! That fixed it.

And thanks for the reference suggestion. It's been a while since i've programmed in c++ so I didn't think that through all of the way.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue in compiling cpp file partha82 Linux - Software 1 06-24-2010 05:04 PM
Compiling to make a "Linux dll" using windows cpp's, .h's, and .LIB a1drich Linux - Software 4 05-11-2007 04:41 AM
Compiling Problems (error: C preprocessor "/lib/cpp" fails sanity check) closetosomethingreal Slackware 3 01-25-2006 01:22 PM
problems with compiling kopete (/lib/cpp error) mac1234mac Programming 1 09-14-2005 01:26 PM
compiling .h and .cpp files with g++ arnab_be Linux - Newbie 2 03-03-2004 02:23 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration