我只是不明白爲什麼這個C++程序不想工作? 幫助!C/C++錯誤gdb atof(argv [1])需要更改爲idb才能正常工作
#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: line 33: 7673 Segmentation fault sh "${SHFILE}"
Press [Enter] to close the terminal ...
更新:上大學
我已經聯繫教授和解決方案,他告訴我的是:
如果我想在unix/linux環境下運行代碼,我需要aft呃編譯和鏈接用說:
c++ -o test.x test.cpp
然後運行代碼爲:
./test.x 0.4
而現在它的工作原理。
輸出是
Hello, World! sin(0.4)=0.389418
但有誰知道在編譯器中輸入0.4的控制檯的方式,而不是像這樣?
當我的C++程序不想工作,我拔出我的鞭子。無論如何,我猜'argv [1]'不存在。 – chris 2013-03-07 23:43:28
您確定您的程序是崩潰的程序,因爲您只共享了構建工具的輸出。此外,你的main()不會檢查它是否收到足夠的參數來獲得有效的argv [1]。更多信息將有所幫助。 – 2013-03-07 23:45:32