Why does my program run fine on Windows but not linux?

I've tried compiling the following program in a Linux Mint Cinnamon VM and get many errors, yet it runs perfectly fine on Windows.The command I'm using is gcc Main.cpp -o Main

#include "Main.h"
#include <fstream>
#include <iostream>
#include <string>
#include <vector>


using namespace std;
int Main::count = 0;
string festival = "";
std::vector<string> v;

Main::Main()
{
}


Main::~Main()
{
}


int main() {
Main main;
cout << "Please enter the festival name.";
getline(cin, festival);
main.nameFile();
main.readFile();
system("PAUSE");
return 0;
}

void Main::nameFile() {
ifstream inFile;
inFile.open("names.txt");
if (inFile.fail()) {
            cerr << "Error Opening "names.txt"" << endl;
            exit(1);
        }
string line;
while (getline(inFile, line)) {
    v.push_back(line);
    ofstream outFile(line + ".txt");
}
}



void Main::readFile()
{
while (v.size() != 0) {
    string name;
    name = v.at(v.size() - 1);
    v.pop_back();
    std::ofstream out(name + ".txt");
    ifstream file;
    file.open("letter.txt");

    string line, nameHolder = "@name@", festivalHolder = "@festival@";
    while (std::getline(file, line))
    {
        std::size_t n_i = line.find(nameHolder);
        if (n_i != std::string::npos)
            line.replace(n_i, nameHolder.size(), name);

        std::size_t f_i = line.find(festivalHolder);
        if (f_i != std::string::npos)
            line.replace(f_i, festivalHolder.size(), festival);

        out << line << 'n';
    }
}
}

 #pragma once
 class Main
{
private: 
static int count;
//static string festival;
public:
Main();
~Main();
void readFile();
void nameFile();
};

Here are the errors I get on Linux:

Main.cpp: In function 'int main()':
Main.cpp:30:16: error: 'system' was not declared in this scope system("PAUSE");

Main.cpp: In member function 'void Main::nameFile()':
Main.cpp:39:11: error: 'exit' was not declared in this scope exit(1);

Main.cpp:44:33: error: no matching function for call to 'std::basic_ofstream::basic_ofstream(std::basic_string)'
ofstream outFile(line + ".txt");

Main.cpp:44:33: note: candidates are:
In file included from Main.cpp:2:0: /usr/include/c++/4.8/fstream:640:7: note: std::basic_ofstream<_CharT, _ Traits>::basic_ofstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode] basic_ofstream(const char* __s,

/usr/include/c++/4.8/fstream:640:7: note: no known conversion for argument 1 from 'std::basic_string' to 'const char*' /usr/include/c++/4.8/fstream:625:7: note: std::basic_ofstream<_CharT, _ Traits>::basic_ofstream() [with _CharT = char; _Traits = std::char_traits] basic_ofstream(): __ostream_type(), _M_filebuf()

/usr/include/c++/4.8/fstream:625:7: note: candidate expects 0 arguments, 1 provided
/usr/include/c++/4.8/fstream:599:11: note: std::basic_ofstream::basic_ofstream(const std::basic_ofstream&) class basic_ofstream : public basic_ostream<_CharT,_Traits>

/usr/include/c++/4.8/fstream:599:11: note: no known conversion for argument 1 from 'std::basic_string' to 'const std::basic_ofstream&'
Main.cpp: In member function 'void Main::readFile()':

Main.cpp:56:34: error: no matching function for call to ' std::basic_ofstream::basic_ofstream(std::basic_string)' std::ofstream out(name + ".txt");

Main.cpp:56:34: note: candidates are:
In file included from Main.cpp:2:0: /usr/include/c++/4.8/fstream:640:7: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits; std::ios_base::openmode = std::_Ios_Openmode] basic_ofstream(const char* __s,

/usr/include/c++/4.8/fstream:640:7: note: no known conversion for argument 1 from 'std::basic_string' to 'const char*'
/usr/include/c++/4.8/fstream:625:7: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char; _Traits = std::char_traits] basic_ofstream(): __ostream_type(), _M_filebuf()

/usr/include/c++/4.8/fstream:625:7: note: candidate expects 0 arguments, 1 provided
/usr/include/c++/4.8/fstream:599:11: note: std::basic_ofstream::basic_ofstream(const std::basic_ofstream&) class basic_ofstream : public basic_ostream<_CharT,_Traits>

/usr/include/c++/4.8/fstream:599:11: note: no known conversion for argument 1 from 'std::basic_string' to 'const std::basic_ofstream&'

And here's the output I get if I compile + run on Windows:

names.txt:

John Boehner

Barack Obama

John Doe

Jim Shoe

Bill Ding

letter.txt:

Jane Doe

Room 213-A

Generic Old Building

School of Information Technology

Programming State University

New York NY 12345-0987

USA

To: @name@

Subject: Season's greetings: @festival@

Dear @name@,

A very @festival@ to you and your family!

Your sincerely,

Jane


Without seeing the errors that you get, I'm just guessing that it's the problem that jumps out at me. The system function just hands your command to the operating system. Linux doesn't recognize the "PAUSE" command.

Don't use system for cross-platform code, because different operating systems recognize different commands.

C++: Equivalent of system("pause"); from Windows in Linux

Edit: Looking at your errors, I would guess that you need to include some extra headers in Linux. (The error: 'exit' was not declared in this scope and errors like that usually indicate a missing header.) In my experience, some of the Microsoft header files will include about half of the standard library headers, which can make maintaining cross-platform code annoying when developers try to clean up un-used headers without checking the other operating system.

If you look up the not declared functions, you should be able to find out which headers need to be included.

链接地址: http://www.djcxy.com/p/95544.html

上一篇: Ubuntu上的Ogre3D基础框架问题

下一篇: 为什么我的程序在Windows上运行正常,但不是Linux?