首页 文章

为什么我的程序在Windows上运行正常而不是linux? [关闭]

提问于
浏览 765
-6

我尝试在Linux Mint Cinnamon VM中编译以下程序并获得许多错误,但它在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:error:'exit'未在此范围exit(1)中声明; Main.cpp:44:33:错误:没有匹配函数来调用'std :: basic_ofstream :: basic_ofstream(std :: basic_string)'of out outFile(line“.txt”); Main.cpp:44:33:注意:候选人是:Main.cpp中包含的文件:2:0:/ usr / include / c /4.8/fstream:640:7:注意: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:注意:'std :: basic_string'中参数1没有已知的转换到'const char *'/ usr / include / c /4.8/fstream:625:7:注意:std :: basic_ofstream <CharT, Traits> :: basic_ofstream()[with _CharT = char; _Traits = std :: char_traits ] basic_ofstream(): odream_type(),_ M_filebuf()/ usr / include / c /4.8/fstream:625:7:注意:候选人需要0个参数,1提供/ usr / include / c /4.8/fstream:599:11 :注意: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:注意:参数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“.tx t“); Main.cpp:56:34:注意:候选人是:Main.cpp中包含的文件:2:0:/ usr / include / c /4.8/fstream:640:7:注意: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:注意:'std :: basic_string'中参数1没有已知的转换'const char *'/ usr / include / c /4.8/fstream:625:7:注意:std :: basic_ofstream <CharT,Traits> :: basic_ofstream()[with _CharT = char; _Traits = std :: char_traits] basic_ofstream(): odream_type(), M_filebuf()/ usr / include / c /4.8/fstream:625:7:注意:候选人需要0个参数,1提供/ usr / include / c /4.8/fstream:599:11:注意: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:注意:参数1没有已知的转换'std :: basic_string'到'const std :: basic_ofstream&'

如果我在Windows上编译运行,这是我得到的输出:

names.txt中:

John Boehner Barack Obama John Doe Jim Shoe Bill Ding

letter.txt:

Jane Doe Room 213-A通用旧建筑学院信息技术学院纽约州立大学纽约分校12345-0987美国致:@ name @主题:季节问候:@节日@亲爱的@ name @,一个非常@节日@给你和你的家人!你的真诚,简

1 回答

  • 1

    如果没有看到你得到的错误,我就会突然出现问题 . system 函数只是将您的命令交给操作系统 . Linux无法识别 "PAUSE" 命令 .

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

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

    Edit: 看看你的错误,我猜你需要在Linux中加入一些额外的 Headers . ( error: ‘exit’ was not declared in this scope 和类似的错误通常表示缺少 Headers . )根据我的经验,一些Microsoft头文件将包含大约一半的标准库头,这可能会使开发人员尝试清理un时维护跨平台代码烦人 - 使用标头而不检查其他操作系统 .

    如果您查找 not declared 函数,您应该能够找出需要包含哪些 Headers .

相关问题