我正在编写一个程序,在编译代码时需要编译器版本的信息.
为了简化问题,我的代码是这样的
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout<<"The C++ compiler version is: "<<__STDC_VERSION__<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望一旦它被编译并运行,它将输出:
C++编译器版本是:gcc 5.3.0
我试图编译它,并得到一个错误:
$ g++ main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:24:11: error: ‘__STDC_VERSION__’ was not declared in this scope
cout<<__STDC_VERSION__<<endl;
^
Run Code Online (Sandbox Code Playgroud)
如何在我的代码中正确获取编译器版本?
我正在编写一个C++程序,它将数据输出到文件,生成python脚本并调用pyplot以进行绘图.
但是,当我按指针传递参数时,它可以正确编译,但无法运行.它返回错误.当我使用Xcode调试模式并逐步执行它时,它会偶然提供正确的结果,但并非总是如此.有时它也会返回错误.
我怀疑它可能是由一些内存分配问题引起的,但我无法确定究竟是什么问题.
我的代码如下:
1)主要
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include "PyCPlot.h"
using namespace std;
double pi = 3.1415926;
int main(int argc, const char * argv[]) {
int nline = 100;
double * np_list = new double(nline);
double * pack_fraction_np = new double (nline);
for (int i=0; i<nline; i++){
np_list[i] = double(i)/double(nline)*2*pi;
pack_fraction_np[i] = cos(np_list[i]);
}
PyCPlot_data_fout("RandomPacking", nline, np_list, pack_fraction_np);
PyCPlot_pythonscript("RandomPacking", "Random Packing");
PyCPlot_pyplot("RandomPacking", "Random Packing");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
2)头文件
#ifndef …Run Code Online (Sandbox Code Playgroud)