c++ - C/C++ Bug gdb atof(argv[1]) 需要更改为 idb 才能工作

标签 c++

<分区>

我只是不明白为什么这个 C++ 程序不想工作? 帮助!

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main (int argc, char* argv[])
{
    // convert the text argv[1] to double using atof:
    double r = atof(argv[1]);
    double s = sin(r);
    cout << "Hello, World! sin(" << r << ")=" << s << endl;
    // success
    return 0;
}

报告:

"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory /home/aleksandar/NetBeansProjects/CppApplication_2'
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk dist/Debug/GNU_Compiler_Collection-Linux-x86/cppapplication_2
gmake[2]: Entering directory /home/aleksandar/NetBeansProjects/CppApplication_2
mkdir -p build/Debug/GNU_Compiler_Collection-Linux-x86
rm -f build/Debug/GNU_Compiler_Collection-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU_Compiler_Collection-Linux-x86/main.o.d -o build/Debug/GNU_Compiler_Collection-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU_Compiler_Collection-Linux-x86
g++ -o dist/Debug/GNU_Compiler_Collection-Linux-x86/cppapplication_2 build/Debug/GNU_Compiler_Collection-Linux-x86/main.o
gmake[2]: Leaving directory `/home/aleksandar/NetBeansProjects/CppApplication_2'
gmake[1]: Leaving directory `/home/aleksandar/NetBeansProjects/CppApplication_2'

BUILD SUCCESSFUL (total time: 613ms)

RUN FAILED (exit value 1, total time: 78ms)

/usr/local/netbeans-7.2.1/ide/bin/nativeexecution/dorun.sh:第 33 行:7673 段错误 sh“${SHFILE}” 按 [Enter] 关闭终端 ...

更新:

我已经联系了那所大学的教授,他告诉我的解决方案是:

如果我想在 unix/linux 环境中运行代码,我需要在编译和链接之后说:
c++ -o test.x test.cpp
然后运行代码为:
./test.x 0.4
现在可以了。
输出是
你好,世界! sin(0.4)=0.389418
但是有谁知道在编译器的控制台中输入 0.4 而不是这样的方法吗??

最佳答案

argv[1] 仅在 argc >= 2 时有效。您应该在代码中检查这一点。

当您使用命令行参数运行程序时,argc 仅大于1

关于c++ - C/C++ Bug gdb atof(argv[1]) 需要更改为 idb 才能工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15284074/

相关文章:

c++ - 避免在字符串中进行 if-else 分支以进行类型调度

c++ - 编译错误 : invalid use of void expression

c++ - 通过 cmake 安装后 - grpc forlders 为空

c++ - C++中UINT和LPCWSTR之间的转换

c++ - Visual Studio 交叉编译错误

c++ - 使用 switch 的自动函数返回值

c++ - 更新 C++ vector 中的所有元素

作为属性的 C++ COM 对象

c++ - 在没有额外成员的情况下重新解释转换为派生类可以吗?

javascript - ActiveX 中的 VARIANT 克隆