为什么我的程序在Windows上运行正常,但不是Linux?
我试过在Linux Mint Cinnamon虚拟机中编译下面的程序,并得到很多错误,但它在Windows上运行得非常好。我使用的命令是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();
};
这里是我在Linux上遇到的错误:
  Main.cpp:在函数'int main()'中: 
  Main.cpp:30:16:错误:'system'未在此范围系统中声明(“PAUSE”); 
  Main.cpp:在成员函数'void Main :: nameFile()'中: 
  Main.cpp:39:11:错误:'exit'未在此范围内声明exit(1); 
  Main.cpp:44:33:错误:没有匹配函数调用'std :: basic_ofstream :: basic_ofstream(std :: basic_string)' 
  outstream outFile(line +“.txt”); 
  Main.cpp:44:33:注意:候选人是: 
  在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:注意:没有将参数1从'std :: basic_string'转换为'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:注:候选人需要0个参数,1个提供 
  /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:没有将参数1从'std :: basic_string'转换为'const std :: basic_ofstream'的已知转换 
  Main.cpp:在成员函数'void Main :: readFile()'中: 
Main.cpp:56:34:error:没有匹配函数调用'std :: basic_ofstream :: basic_ofstream(std :: basic_string)'std :: ofstream out(name +“.txt”);
  Main.cpp:56:34:注意:候选人是: 
  在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:没有将参数1从'std :: basic_string'转换为'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:注:候选人需要0个参数,1个提供 
  /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:没有将参数1从'std :: basic_string'转换为'const std :: basic_ofstream'的已知转换
如果我编译+在Windows上运行,这里是我得到的输出:
names.txt中:
约翰·博纳
巴拉克奥巴马
John Doe
吉姆鞋
丁丁
letter.txt:
Jane Doe
213-A室
通用老建筑
信息技术学院
编程州立大学
纽约NY 12345-0987
美国
命名@
主题:季节问候:@ festival @
亲爱的@名字@,
一个非常@节日@给你和你的家人!
忠实于你的,
简
  在没有看到你得到的错误的情况下,我只是猜测这是跳到我身上的问题。  system功能只是将您的命令传递给操作系统。  Linux不识别"PAUSE"命令。 
  不要将system用于跨平台代码,因为不同的操作系统可以识别不同的命令。 
C ++:系统等效(“暂停”); 从Linux中的Windows
  编辑:看看你的错误,我想你需要在Linux中包含一些额外的头文件。  ( error: 'exit' was not declared in this scope像这样的错误通常表明缺少头文件。)根据我的经验,一些Microsoft头文件将包含大约一半的标准库头文件,当开发人员尝试在不检查其他操作系统的情况下清理未使用的头文件时,平台代码很烦人。 
  如果查找not declared函数,则应该能够找出需要包含哪些头文件。 
上一篇: Why does my program run fine on Windows but not linux?
下一篇: Platform Independence with C++. Detection and Compilation
