2016-11-07 133 views
0

在我的代碼中,我有幾個地方相同的cout函數,我決定移出int main()。 然而,當我這樣做,並重新運行我的代碼,當有問題的功能被觸發時,我收到「調試斷言失敗」錯誤。調試斷言在cout功能失敗

我已經放在主函數之外的代碼。

string printresult(double smallest, double largest) 
{ 
    cout << "the smaller value is: " << smallest << "\n"; 
    cout << "the larger value is: " << largest << "\n"; 
    return 0; 
} 

我相信它有什麼做的return 0代碼,我在cout函數的最後增加了,沒有它然而,我的代碼不會現在連編譯。任何人都可以指向正確的方向,這樣我可以更好地識別我的錯誤?

我完整的代碼

#include "../../std_lib_facilities.h" 

// conversion of units to meters 

double funconvert(double x, string unit) 
{ 
    const double cm_m = 1.0/100.0, in_cm = 2.54, ft_in = 12; 
    if (unit == "m") 
     return x = x; 
    else if (unit == "cm") 
     return x = x * cm_m; 
    else if (unit == "in") 
     return x = x * in_cm * cm_m; 
    else if (unit == "ft") 
     return x = x * ft_in * in_cm * cm_m; 
    else 
     cout << "Unknown unit value.\n"; 
} 

// printing the large and the small variables 
string printresult(double smallest, double largest) 
{ 
    cout << "the smaller value is: " << smallest << "\n"; 
    cout << "the larger value is: " << largest << "\n"; 
    return 0; 
} 

int main() 
{ 
    vector<double>numbers; 
    double a, b, smallest, largest; 
    string unit; 
    cout << "Please enter an intiger followed by a measurment unit [e.g 10cm]:"; 

    // Take input 
    while (cin >> a >> unit) 
    { 
     // check if this is the first number entered 
     if (numbers.size() == 0) 
     { 
      a = funconvert(a, unit); 
      b = a; 
      numbers.push_back(a); 
      numbers.push_back(b); 
     } 
     else 
     { 
      //assign a and b to the last two numbers in a vector 
      a = funconvert(a, unit); 
      numbers.push_back(a); 
      a = numbers[(numbers.size() - 1)]; 
      b = numbers[(numbers.size() - 2)]; 
      // numbers.erase(numbers.begin()); // enable if desire to keep the vector empty 
     } 

     // find out which variable is larger and smaller 
     if (a > b && (a/b-1) >= 0.01) 
     { 
      smallest = b; 
      largest = a; 
      printresult(smallest, largest); 
     } 
     else if (a < b && (b/a - 1) >= 0.01) 
     { 
      smallest = a; 
      largest = b; 
      printresult(smallest, largest); 
     } 
     else if ((a/b - 1) < 0.01 && (a/b - 1) != 0.00 || (b/a - 1) < 0.01 && (a/b - 1) != 0.00) 
     { 
      cout << "the nubers are almost equal\n"; 
     } 
     else 
      cout << a << " equals " << b << "\n"; 
    } 
    return 0; 
} 
+0

您聲明'printresult'會返回一個'string'更換特質return x = x;,但你的身體返回'int'。相反,它看起來應該是'void printresult(...)',因爲你沒有任何東西可以返回? – crashmstr

+0

爲什麼要從預期返回'string'的函數返回'0'? – AndyG

+0

@AndyG即使現在回想起來也沒什麼意義,我這樣做是爲了讓我的程序能夠編譯,也許這樣我就能夠在邏輯上跟蹤我的錯誤。毋庸置疑,這並沒有奏效。感謝您的意見,它幫助了很多!現在我會知道。 – Newskooler

回答

1

當您編寫return 0時,C++標準庫嘗試使用const char*中的std::string構造函數。但是這個構造函數要求你提供一個有效的地址給一個nul結尾的字符緩衝區,否則程序的行爲是undefined

要修復,改變功能的void返回類型:

void printresult(double smallest, double largest)

,並在函數體掉落return聲明。

還要考慮與return x;

+0

對於我來說,放置int或其他類型是沒有意義的,然而'string'是最接近我的邏輯(我是編程新手)。現在我會知道在這種情況下會使用無效。謝謝! – Newskooler

1

printresult返回你用0初始化std::string它調用下面的構造

basic_string(const CharT* s, 
       const Allocator& alloc = Allocator()); 

它是undefined behavior通過它比其他任何東西有效的緩衝地址。

構造字符串,其內容初始化爲由s指向的 以null結尾的字符串的副本。 字符串的長度由第一個空字符決定。行爲是 如果s未指向至少包含 Traits :: length(s)+1元素的數組,則該行爲爲undefined,包括s爲 空指針的情況。

考慮一下你自己的幸運,你的標準庫實現聲明瞭這一點。