简体   繁体   中英

c++ cannot convert 'std::basic_ostream<char>::__ostream_type' {aka 'std::basic_ostream<char>'} to 'int' in assignment

Trying to find log base 2 of a number in c++ but am stuck on this error.

cannot convert 'std::basic_ostream::__ostream_type' {aka 'std::basic_ostream'} to 'int' in assignment

int main()
{
    int n, result;
    cout << " enter a positive integer: ";
    cin >> n;
    
    result = (n >= 0) ? cout << log(n)/log(2) : cout << "Invalid";
    cout << "Log base 2 of " << n << " is " << result;
    cout << endl;
    
    system("pause");
    return EXIT_SUCCESS;
            
}

cout does not have return value. cout << has return value, but it is not int. cout << return value is a reference to count, which enables us link variables like court << a << b <<... .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM