我读过Bjarne Stroustrup博士的书"C++编程语言"第3版内置类型在第10.4.2节中也有C++中的构造函数.
但是接下来的链接说POD类型不能有构造函数:
http://www.parashift.com/c++-faq-lite/pod-types.html
这是真的吗?原始类型在C++中也有构造函数吗?
最近我测试了以下程序&我期待运行时错误,但它显示" nan "作为输出.为什么和如何?我正在使用g ++编译器.
#include <iostream>
int main()
{
float f=0.0f/0.0f;
std::cout<<f;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
同样的方式我也在Java和C#中尝试过类似的程序,它再次显示"nan"作为输出.
class Test
{
public static void main(String args[])
{
float f=0.0f/0.0f;
System.out.println(f);
}
}
class Test
{
public static void Main(string[] args)
{
float f=0.0f/0.0f;
Console.Write(f);
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何实现浮点运算以及它与整数运算的区别.在C++的情况下它是不确定的行为?如果是,为什么?请帮我.
我很想知道为什么类'数据成员不能使用()语法初始化?考虑以下示例:
#include <iostream>
class test
{
public:
void fun()
{
int a(3);
std::cout<<a<<'\n';
}
private:
int s(3); // Compiler error why???
};
int main()
{
test t;
t.fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
程序编译失败并出现以下错误.
11 9 [Error] expected identifier before numeric constant
11 9 [Error] expected ',' or '...' before numeric constant
Run Code Online (Sandbox Code Playgroud)
为什么?是什么原因?关于类数据成员初始化的C++标准是什么意思?非常感谢您的帮助.谢谢
我正在使用Orwell Dev C++ IDE.最近我测试了以下简单的程序,其中我忘了放分号(;)但它仍然在C中编译好但在C++中没编译.为什么?是什么原因?
// C program compiles & runs fine, even ; missing at end of struct
#include <stdio.h>
struct test
{ int a,b}; // missing semicolon
int main()
{
struct test d={3,6};
printf("%d",d.a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
// Following is compilation error in C++
#include <stdio.h>
struct test
{ int a,b}; // missing semicolon
int main()
{
struct test d={3,6};
printf("%d",d.a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试在代码块13.12 IDE中使用相同的C程序,但它显示以下错误消息
为什么不同的实现给出不同的错误消息?
我正在遍历带有auto(附加代码)的向量.在遍历时,我还在后面添加了一些元素.我没想到我得到的输出.
#include <iostream>
#include <vector>
using namespace std;
vector <int> dynamic_vector;
void access( )
{
for ( auto i : dynamic_vector ) {
if ( i == 3 ) {
dynamic_vector.push_back( 4 );
dynamic_vector.push_back( 5 );
}
cout << i << endl;
}
}
int main() {
dynamic_vector.push_back( 1 );
dynamic_vector.push_back( 2 );
dynamic_vector.push_back( 3 );
access( );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
1
2
3
Run Code Online (Sandbox Code Playgroud)
我期待从1到5的所有数字都会被打印出来.我无法理解如何遍历汽车工作?
我刚刚开始学习D.在C++中有::(范围解析运算符)从函数中访问全局变量,如果全局和局部变量都具有相同的名称.但是如何用D语言做到这一点?考虑这个计划.
import std.stdio;
int a;
int main(string[] args)
{
int a=3;
writeln("D is nice");
static int i;
writeln("value of i is: ",i);
writeln("value of a is: ",a);
// writeln("value of ::a is: ",::a); compiler error here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何从main()函数中打印全局变量a的值?D是否提供这种运营商?
我瞪着它并试图在SO上找到类似的问题,但没有找到任何有用的东西.所以,在这里发布我的问题.
考虑这个程序:
#include <iostream>
void foo(const std::string &) {}
int main()
{
foo(false);
}
Run Code Online (Sandbox Code Playgroud)
[Warning] converting 'false' to pointer type for argument 1 of 'std::basic_string::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]' [-Wconversion-null]
为什么C++在没有显式强制转换的情 我原以为会遇到编译错误.由于以下异常显示,程序在运行时异常终止:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Run Code Online (Sandbox Code Playgroud)
关于这种隐式转换的标准是什么?
除非用户在文件或项目设置中明确提到,否则是否有任何C编译器将默认类型的char作为unsigned?
/ Kanu_
我只是想知道为什么像Turbo c ++ 3.0(蓝屏IDE)和Borland Turbo C++ 4.5等古代编译器在下面的程序中没有报告任何错误.
#include <iostream.h>
int main()
{
int& a=10;
cout<<a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现代C++编译器不会接受上述程序,但为什么古代编译器允许这样做呢?他们只是在上述程序中显示单个警告.
我最近编写了一个简单的程序,我错误地使用scanf()而不是printf()来在控制台上显示消息.我希望得到编译时错误,但它编译好,没有警告和运行时崩溃.我知道scanf()用于从键盘输入.我不应该在以下程序中出错吗?
#include <stdio.h>
int main()
{
scanf("Hello world"); // oops, It had to be printf()
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它是否调用未定义的行为(UB)?在C标准中有没有提到这个?为什么在编译时不检查是否将正确有效的参数传递给scanf()函数?