LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-02-2013, 02:11 AM   #1
ChaosRyder
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!

Code:
#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";
	std::cin>>filename_in;

	//Opens the input file
	file_in.open(filename_in, std::ios::in);

	//Error if input file fails
	if(file_in.fail())
	{
		//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::system("pause");
		exit(1);
	}

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

	//Checks to see if a file already exists
	if(fileExists(filename_out))
	{
		//Gives error message and aborts
		std::cout<<"\nThe filename "<<filename_out<<" already exists!\n";
		std::cout<<"ABORTING FILE COPY!\n\n";
		system("pause");
		exit(1);
	}

	//Opens the output file for writing
	file_out.open(filename_out, std::ios::out);

	//Loop that reads input and writes to output file
	char inp;
	while(!file_in.eof())
	{
		//reads the input char
		file_in.get(inp);
		//does not read eof char on last loop
		if(!file_in.eof())
		{
			file_out<<inp;
		}
	}

	//closes files
	file_in.close();
	file_out.close();

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

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

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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:

Code:
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,

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

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

Code:
#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:
Code:
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.

Code:
bool fileExists(const std::string&);
 
Old 02-02-2013, 09:51 AM   #3
ChaosRyder
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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:25 PM.

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