C++今日のエラー

C++のソースで、g++, clang++どんなエラー、警告がでるかの収集.

```c++:a.cpp
int main(void){
	return cout << "main" << std:endl;
}
```
最初の警告

```shell-session:docker/ubuntu
# clang++ a.cpp
a.cpp:2:9: error: use of undeclared identifier 'cout'
        return cout << "main" << std:endl;
               ^
a.cpp:2:27: error: use of undeclared identifier 'std'
        return cout << "main" << std:endl;
                                 ^
a.cpp:2:30: error: expected expression
        return cout << "main" << std:endl;
                                    ^
3 errors generated.

# g++ a.cpp
a.cpp: In function 'int main()':
a.cpp:2:9: error: 'cout' was not declared in this scope
    2 |  return cout << "main" << std:endl;
      |         ^~~~
a.cpp:2:30: error: found ':' in nested-name-specifier, expected '::'
    2 |  return cout << "main" << std:endl;
      |                              ^
      |                              ::
a.cpp:2:31: error: 'endl' is not a member of 'std'
    2 |  return cout << "main" << std:endl;
      |                               ^~~~
a.cpp:1:1: note: 'std::endl' is defined in header '<ostream>'; did you forget to '#include <ostream>'?
  +++ |+#include <ostream>
    1 | int main(void){
```
変更

```c++:a.cpp
#include <iostream>
int main(void){
	return cout << "main" << std::endl;
}
```
再度コンパイル

```shell-session:docker/ubuntu
# clang++ a.cpp
a.cpp:3:9: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
        return cout << "main" << std::endl;
               ^~~~
               std::cout
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/iostream:61:18: note: 'std::cout' declared
      here
  extern ostream cout;          /// Linked to standard output
                 ^
1 error generated.

# g++ a.cpp
a.cpp: In function 'int main()':
a.cpp:3:9: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
    3 |  return cout << "main" << std::endl;
      |         ^~~~
      |         std::cout
In file included from a.cpp:1:
/usr/local/include/c++/9.1.0/iostream:61:18: note: 'std::cout' declared here
   61 |   extern ostream cout;  /// Linked to standard output
      |                  ^~~~
```

```c++:a.cpp
#include <iostream>
int main(void){
	return std::cout << "main" << std::endl;
}
```

```shell-session:docker/ubuntu
# clang++ a.cpp
a.cpp:3:9: error: no viable conversion from returned value of type '__ostream_type' (aka 'basic_ostream<char,
      std::char_traits<char> >') to function return type 'int'
        return std::cout << "main" << std::endl;
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/basic_ios.h:120:7: note: candidate
      function
      operator void*() const
      ^
1 error generated.

# g++ a.cpp
a.cpp: In function 'int main()':
a.cpp:3:29: error: cannot convert 'std::basic_ostream<char>::__ostream_type' {aka 'std::basic_ostream<char>'} to 'int' in return
    3 |  return std::cout << "main" << std::endl;
      |         ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
      |                             |
      |                             std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}
```

```c++:a.cpp
#include <iostream>
int main(void){
	return (int) std::cout << "main" << std::endl;
}
```

```
# clang++ a.cpp
a.cpp:3:9: error: no matching conversion for C-style cast from 'ostream' (aka 'basic_ostream<char>') to 'int'
        return (int) std::cout << "main" << std::endl;
               ^~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/basic_ios.h:120:7: note: candidate
      function
      operator void*() const
      ^
1 error generated.
root@f32197f7564e:/home/cpp# g++ a.cpp
a.cpp: In function 'int main()':
a.cpp:3:20: error: invalid cast from type 'std::ostream' {aka 'std::basic_ostream<char>'} to type 'int'
    3 |  return (int) std::cout << "main" << std::endl;
      |                    ^~~~
```

```c++:a.cpp
#include <iostream>
int main(void){
	return static_cast<int> std::cout << "main" << std::endl;
}
```


```shell-session:docker/ubuntu
# clang++ a.cpp
a.cpp:3:25: error: expected '(' after 'static_cast'
        return static_cast<int> std::cout << "main" << std::endl;
                               ^
                               (
1 error generated.

# g++ a.cpp
a.cpp: In function 'int main()':
a.cpp:3:26: error: expected '(' before 'std'
    3 |  return static_cast<int> std::cout << "main" << std::endl;
      |                          ^~~
      |                          (
a.cpp:3:58: error: expected ')' before ';' token
    3 |  return static_cast<int> std::cout << "main" << std::endl;
      |                                                          ^
      |                                                          )
a.cpp:3:58: error: invalid static_cast from type 'std::basic_ostream<char>::__ostream_type' {aka 'std::basic_ostream<char>'} to type 'int'
```


```c++:a.cpp
#include <iostream>
int main(void){
	return static_cast<int>( std::cout << "main" << std::endl);
}
```

```shell-session:docker/ubuntu
# clang++ a.cpp
a.cpp:3:9: error: no matching conversion for static_cast from '__ostream_type' (aka 'basic_ostream<char,
      std::char_traits<char> >') to 'int'
        return static_cast<int>( std::cout << "main" << std::endl);
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/basic_ios.h:120:7: note: candidate
      function
      operator void*() const
      ^
1 error generated.

# g++ a5.cpp
a5.cpp: In function 'int main()':
a5.cpp:3:59: error: invalid static_cast from type 'std::basic_ostream<char>::__ostream_type' {aka 'std::basic_ostream<char>'} to type 'int'
    3 |  return static_cast<int>( std::cout << "main" << std::endl);
      |  
```

```c++:a.cpp
#include <iostream>
int main(void){
	return dynamic_cast<int>( std::cout << "main" << std::endl);
}
```


```shell-session:docker/ubuntu
# clang++ a.cpp
a.cpp:3:9: error: 'int' is not a reference or pointer
        return dynamic_cast<int>( std::cout << "main" << std::endl);
               ^           ~~~~~
1 error generated.
root@f32197f7564e:/home/cpp# g++ a.cpp
a.cpp: In function 'int main()':
a.cpp:3:60: error: cannot dynamic_cast '(& std::operator<< <std::char_traits<char> >(std::cout, ((const char*)"main")))->std::basic_ostream<char>::operator<<(std::endl<char, std::char_traits<char> >)' (of type 'std::basic_ostream<char>::__ostream_type' {aka 'class std::basic_ostream<char>'}) to type 'int' (target is not pointer or reference)
    3 |  return dynamic_cast<int>( std::cout << "main" << std::endl);
      |                                   
```

```c++:a.cpp
#include <iostream>
int main(void){
	return const_cast<int>( std::cout << "main" << std::endl);
}
```


```shell-session:docker/ubuntu
# clang++ a.cpp
a.cpp:3:9: error: const_cast to 'int', which is not a reference, pointer-to-object, or pointer-to-data-member
        return const_cast<int>( std::cout << "main" << std::endl);
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

# g++ a.cpp
a.cpp: In function 'int main()':
a.cpp:3:58: error: invalid use of const_cast with type 'int', which is not a pointer, reference, nor a pointer-to-data-member type
    3 |  return const_cast<int>( std::cout << "main" << std::endl);
      |                                                      
```

```c++:a.cpp
#include <iostream>
int main(void){
	return reinterpret_cast<int>( std::cout << "main" << std::endl);
}
```


```shell-session:docker/ubuntu
# clang++ a.cpp
a.cpp:3:9: error: reinterpret_cast from '__ostream_type' (aka 'basic_ostream<char, std::char_traits<char> >') to
      'int' is not allowed
        return reinterpret_cast<int>( std::cout << "main" << std::endl);
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

# g++ a.cpp
a.cpp: In function 'int main()':
a.cpp:3:64: error: invalid cast from type 'std::basic_ostream<char>::__ostream_type' {aka 'std::basic_ostream<char>'} to type 'int'
    3 |  return reinterpret_cast<int>( std::cout << "main" << std::endl);
      |                         
```

# 参考資料
コンパイル用スクリプト



```sh:csh.sh
clang++ $1.cpp -o $1l -Wall
./$1l
g++ $1.cpp -o $1g -Wall
./$1g
```
# 文書履歴(document history)
ver. 0.01 初稿 20190812
ver. 0.04 URL追記　20230228

### 最後までおよみいただきありがとうございました。

いいね　💚、フォローをお願いします。

####  Thank you very much for reading to the last sentence.

Please press the like icon 💚　and follow me for your happy life.