Ich habe versucht, das folgende Programm in einem Linux Mint Cinnamon VM kompilieren und viele Fehler bekommen, aber es läuft völlig in Ordnung, auf Windows.The Befehl Ich bin mit gcc Main.cpp -o HauptWarum läuft mein Programm unter Windows, aber nicht unter Linux?
#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 = "@[email protected]", festivalHolder = "@[email protected]";
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();
};
hier sind die Fehler, die ich auf Linux erhalten:
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&’
Und hier ist die Ausgabe erhalte ich, wenn ich + kompilieren unter Windows laufen:
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: @[email protected]
Subject: Season's greetings: @[email protected]
Dear @[email protected],
A very @[email protected] to you and your family!
Your sincerely,
Jane
Denkst du, es wäre angemessen, die Fehler zu liefern, die du empfängst, oder denkst du stattdessen, dass jeder, der dir helfen könnte, herausfinden sollte, was er selbst ist? Die names.txt und letter.txt, die Sie zur Laufzeit verwenden möchten, ist ziemlich nutzlos, wenn die Hilfe, die Sie suchen, darin besteht, Ihr Programm kompilieren zu lassen. – mah
Müssen die Fehler sehen ... – Dialecticus
Ich denke, es gibt keinen 'PAUSE' Befehl auf Linux. – krOoze