C++ 为什么我的程序在Windows上运行良好,但在linux上运行不好?

C++ 为什么我的程序在Windows上运行良好,但在linux上运行不好?,c++,linux,C++,Linux,我曾尝试在Linux和Linux虚拟机中编译以下程序,但出现了许多错误,但它在Windows上运行得非常好 #include "Main.h" #include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; int Main::count = 0; string festival = ""; std::vector&l

我曾尝试在Linux和Linux虚拟机中编译以下程序,但出现了许多错误,但它在Windows上运行得非常好

#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';
    }
}
}
以下是我在Linux上遇到的错误:

Main.cpp:在函数“int Main()”中:
Main.cpp:30:16:错误:“系统”未在此范围内声明 系统(“暂停”)

Main.cpp:在成员函数“void Main::nameFile()”中:
Main.cpp:39:11:错误:未在此作用域中声明“退出” 出口(1)

Main.cpp:44:33:错误:调用“std::basic_of stream::basic_of stream(std::basic_string)”时没有匹配函数
出流管(线+“.txt”)

Main.cpp:44:33:注:候选人为:
在Main.cpp中包含的文件中:2:0: /usr/include/c++/4.8/fstream:640:7:注:std::basic_of stream::basic_of stream(const char*,std::ios_base::openmode)[带_CharT=char;_Traits=std::char_Traits;std::ios_base::openmode=std:_ios_openmode] 基本流(常量字符*)

/usr/include/c++/4.8/fstream:640:7:注意:参数1没有从“std::basic_string”到“const char*”的已知转换 /usr/include/c++/4.8/fstream:625:7:注意:std::basic_of stream::basic_of stream()[带_CharT=char;_Traits=std::char_Traits] 流()的基本类型:u ostream_type(),_M_filebuf()

/usr/include/c++/4.8/fstream:625:7:注意:候选者需要0个参数,提供1个参数
/usr/include/c++/4.8/fstream:599:11:注:std::basic_of stream::basic_of stream(const std::basic_of stream&) 流的基本类:公共基本类

/usr/include/c++/4.8/fstream:599:11:注意:参数1从'std::basic_string'到'const std::basic_of stream&'的转换未知
Main.cpp:在成员函数“void Main::readFile()”中:

Main.cpp:56:34:错误:调用“std::basic_of stream::basic_of stream(std::basic_string)”时没有匹配函数 std::ofstreamout(name+“.txt”)

Main.cpp:56:34:注:候选人为:
在Main.cpp中包含的文件中:2:0: /usr/include/c++/4.8/fstream:640:7:注:std::basic_of stream::basic_of stream(const char*,std::ios_base::openmode)[带_CharT=char;_Traits=std::char_Traits;std::ios_base::openmode=std:_ios_openmode] 基本流(常量字符*)

/usr/include/c++/4.8/fstream:640:7:注意:参数1没有从“std::basic_string”到“const char*”的已知转换
/usr/include/c++/4.8/fstream:625:7:注意:std::basic_of stream::basic_of stream()[带_CharT=char;_Traits=std::char_Traits] 流()的基本类型:u ostream_type(),_M_filebuf()

/usr/include/c++/4.8/fstream:625:7:注意:候选者需要0个参数,提供1个参数
/usr/include/c++/4.8/fstream:599:11:注:std::basic_of stream::basic_of stream(const std::basic_of stream&) 流的基本类:公共基本类

/usr/include/c++/4.8/fstream:599:11:注意:参数1没有从'std::basic_string'到'const std::basic_of stream&'的已知转换

下面是我在Windows上编译并运行时得到的输出:

names.txt:

约翰·博纳

巴拉克·奥巴马

无名氏

吉姆鞋

丁比尔

letter.txt:

无名氏

213-A房间

普通旧建筑

信息技术学院

编程州立大学

纽约州纽约市12345-0987

美国

致:@name@

主题:节日问候:@节日@

亲爱的@name@

祝你和你的家人节日快乐

您诚挚的,


在没有看到您遇到的错误的情况下,我只是猜测是问题突然出现在我面前。
系统
函数只是将您的命令交给操作系统。Linux不识别
“暂停”
命令

不要将
system
用于跨平台代码,因为不同的操作系统识别不同的命令

编辑: 看看您的错误,我想您需要在Linux中包含一些额外的头。(此范围中未声明
错误:“exit”,类似的错误通常表示缺少头。)根据我的经验,一些Microsoft头文件将包含大约一半的标准库头文件,当开发人员试图在不检查其他操作系统的情况下清理未使用的头文件时,这会使维护跨平台代码变得烦人


如果您查找
未声明的
函数,您应该能够找到需要包含的标题。

您认为提供您收到的错误是合适的,还是认为任何愿意帮助您的人都应该自己发现它们是什么?提供names.txt和如果您寻求的帮助是让程序编译,那么您希望在运行时使用的d letter.txt是毫无用处的。需要查看错误…我认为,linuxes上没有
PAUSE
命令。好的,谢谢,这是一个开始的小更正:,ever@Tas,我同意,但是
system
对于您知道您需要的小型测试程序非常有用我永远不会发布,这就是为什么我不会对此采取强硬态度。如果你想将代码提供给其他人,那么是的,永远不要使用
system
 #pragma once
 class Main
{
private: 
static int count;
//static string festival;
public:
Main();
~Main();
void readFile();
void nameFile();
};