繁体   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