以下程序使用警告在C中编译良好,但在C++中编译失败.为什么?是什么原因?
#include <stdio.h>
int main(void)
{
char a[5]="Hello";
a[0]='y';
puts(a);
for(int i=0;i<5;i++)
printf("%c",a[i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
警告:
Warning:[Error] initializer-string for array of chars is too long [-fpermissive] enabled by default
Run Code Online (Sandbox Code Playgroud)
但是如果程序编译为C++程序,则C++编译器会出现以下错误:
[Error] initializer-string for array of chars is too long [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
我正在使用GCC 4.8.1编译器.
我正在读这个.我在代码块13.12 IDE上测试了这个程序,它支持C++11但是在编译时失败并且编译器显示多个错误.看看这个节目.它适用于在线编译罚款看到这
// bad_array_new_length example
#include <iostream> // std::cout
#include <exception> // std::exception
#include <new> // std::bad_array_new_length
int main() {
try {
int* p = new int[-1];
} catch (std::bad_array_new_length& e) {
std::cerr << "bad_array_new_length caught: " << e.what() << '\n';
} catch (std::exception& e) { // older compilers may throw other exceptions:
std::cerr << "some other standard exception caught: " << e.what() << '\n';
}
}
Run Code Online (Sandbox Code Playgroud)
编译器错误:
7 12 [Error] expected type-specifier
7 37 …Run Code Online (Sandbox Code Playgroud) 为什么整个字符串显示为结果?为什么第一个字符的地址没有被打印?如何打印第一个字符的地址?请帮我.
#include <iostream>
int main()
{
char x[6]="hello";
std::cout<<&x[0];
}
Run Code Online (Sandbox Code Playgroud) 我正在读这个。它说
仅当函数很小(例如 10 行或更少)时才定义内联函数。
但是 Bjarne Stroustrup 在他的《使用 C++ 编程原则和实践》一书中说:
第 9.4 节:定义成员函数
“显而易见的经验法则是:除非您知道需要通过内联小函数来提高性能,否则不要将成员函数体放在类声明中。大函数,比如五行代码,不会从内联中受益。我们很少内联由一两个以上表达式组成的函数。
那么定义一个具有 10 行或至少 10 行的函数作为内联函数是否合适且有帮助。这不是使程序可执行文件变大,甚至编译器都可以忽略内联如此大函数的请求吗?谷歌 C++ 风格是否给出了关于在 C++ 中使用内联函数的错误指南?
考虑以下2个程序.
#include <iostream>
using std::cout;
class Base {
public:
virtual void f()=0;
void g() {
f();
}
virtual ~Base() { }
};
class Derived : public Base
{
public:
void f() {
cout<<"Derived::f() is called\n";
}
~Derived() {}
};
class Derived1 : public Base
{
public:
void f() {
cout<<"Derived1::f() is called\n";
}
~Derived1() { }
};
int main() {
Derived1 d;
Base& b=d;
b.g();
b.f();
}
Run Code Online (Sandbox Code Playgroud)
编译并运行良好并给出预期的结果..
#include <iostream>
using std::cout;
class Base {
public:
virtual void f()=0;
Base() …Run Code Online (Sandbox Code Playgroud) 我想extern对用户定义的类型使用关键字.这意味着我已经在一个文件中声明了对象并在其他文件中定义了它.我已经读过extern关键字用于声明变量而不定义它.当程序被拆分为多个源文件并且每个都需要使用全局变量时,extern关键字很有用.如果我错了,请纠正我.
这是我写的程序,但不幸的是我做错了什么或遗漏了一些东西,所以我得到了编译错误.
Prog1.cpp
#include <iostream>
using std::cout;
class test
{
public:
void fun();
};
void test::fun()
{
cout<<"fun() in test\n";
}
test t;
Run Code Online (Sandbox Code Playgroud)
Prog2.cpp
#include <iostream>
using std::cout;
extern test t;
int main()
{
t.fun();
}
Run Code Online (Sandbox Code Playgroud)
现在我编译这两个使用时
g++ -o prog1.cpp prog2.cpp
编译器在prog2.cpp中给出了以下错误
error: 'test' does not name a type
error: 't' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
请帮我知道我在这里做错了什么.
我最近编写了一个简单的程序,我错误地使用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()函数?
C++ 专家兼 D 语言创始人Walter Bright表示:
切片问题很严重,因为它可能导致内存损坏,并且很难保证程序不会受到这种情况的影响。为了从语言中设计它,支持继承的类应该只能通过引用(而不是通过值)访问。D 编程语言具有这个特性。
如果有人通过给出一个 C++ 示例来解释它会更好,其中对象切片问题导致内存损坏?而这个问题用D语言是如何解决的呢?
根据此,获取无效指针的值是C++中实现定义的行为.现在考虑以下C程序:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int* p=(int*)malloc(sizeof(int));
*p=3;
printf("%d\n",*p);
printf("%p\n",(void*)p);
free(p);
printf("%p\n",(void*)p); // Is this undefined or implementation defined in behaviour C?
}
Run Code Online (Sandbox Code Playgroud)
但是C中的行为也一样吗?上述C程序的行为是否未定义或实现定义?C99/C11标准对此有何看法?请告诉我C99和C11的行为是否不同.
我正在阅读http://www.cplusplus.com/doc/tutorial/typecasting/.它说:
否则,如果转换是在相同类型的数字类型之间(整数到整数或浮动到浮动),则转换有效,但该值是特定于实现的(并且可能不是可移植的).
但我真的不明白上面引用的意思是什么?有人请用一个简单的例子解释一下吗?为什么在相同类型的数字类型之间进行转换会导致特定于实现的值?是什么原因?