簡體   English   中英

在 Visual Studio 中創建 C++ 代碼並在 g++ 中運行它

[英]Creating c++ code in visual studio and running it in g++

我在 Visual Studio 中創建了 c++ 代碼,但大學規范是它需要在 g++ 編譯器中運行。

代碼在 Visual Studio 中完美運行,但在 g++ 中根本不起作用。 為什么是這樣? 有沒有一種簡單的方法可以讓代碼在 g++ 中工作?

這是我的代碼。

#include <iostream> 
#include <vector> 
#include <cmath>
#include <iomanip>
#include <fstream> 

using namespace std;

int main ()
{
    vector<long double> G;
    vector<long double> R;
    int n=1;
    long double result =1;

    R.assign(2,0);
    G.assign(2,123);
    G.at(1)=321;

    while (result >= 0.0000000000000001)
    {

    n++;    
    G.push_back((G.at(n-2) - G.at(n-1)));
    R.push_back(G.at(n)/G.at(n-1));

    result = abs(R.at(n) - R.at(n-1));

    ofstream file;
    file.open("series.txt", std ::ios_base ::app);
    file << n <<"\t" << setw(14) << G.at(n)<<"\t<<setprecision(15)<<R.at(n)<<endl;
    //cout << n <<"\t" <<setw(14)<<G.at(n)<<"\t"<<setprecision(15)<<R.at(n)<<endl;
    file.close();
    }

    system("pause");
    }

在我看來,交叉編譯沒有問題,盡管據我所知,Linux 系統上沒有system("pause")

錯誤缺少終止字符。 這是由於在第二個\\t處遺漏了引號

這個

file << n <<"\t" << setw(14) << G.at(n)<<"\t<<setprecision(15)<<R.at(n)<<endl;

應該是這個

file << n <<"\t" << setw(14) << G.at(n)<<"\t"<<setprecision(15)<<R.at(n)<<endl;

要使用system()函數,您必須將#include <stdlib.h>添加到您的項目並更改

file << n <<"\t" << setw(14) << G.at(n)<<"\t<<setprecision(15)<<R.at(n)<<endl;

file << n <<"\t" << setw(14) << G.at(n)<<"\t"<<setprecision(15)<<R.at(n)<<endl;

然后它用 g++ 編譯 100%。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM