LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Having problems compiling my .cpp file on Linux (https://www.linuxquestions.org/questions/programming-9/having-problems-compiling-my-cpp-file-on-linux-4175448297/)

ChaosRyder 02-02-2013 02:11 AM

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;
}


millgates 02-02-2013 03:13 AM

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

ChaosRyder 02-02-2013 09:51 AM

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.


All times are GMT -5. The time now is 10:13 PM.