给定一个std::list名为 的迭代器it,it1++相当于it1=it1+1。那么为什么it1++工作正常,但it1=it1+1在下面的代码中却出现错误呢?
代码
#include <iostream>
#include <list>
int main()
{
std::list<int> l1{19, 2, 3, 21, 5, 19, 7, 11};
std::list<int>::iterator it1;
std::cout << "1st\n";
it1 = l1.begin();
it1++;
it1 = it1 + 1; // This giving error
}
Run Code Online (Sandbox Code Playgroud)
输出
Invalid operands to binary expression
('std::list<int>::iterator' (aka '_List_iterator<int>') and 'int')
Run Code Online (Sandbox Code Playgroud) 代码
#include<iostream>
int main()
{
int a=4,b,c;
a==3 ? : b=a*a ; // Works fine
a==4 ? c=a*a : ; // ERROR Expected Primary expression before ;
}
Run Code Online (Sandbox Code Playgroud)
第一个条件语句
我没有写“表达式 2 ”,但它不会产生错误
第二个条件语句
我没有写“表达式 3 ”,它给出了一个错误
那么为什么它在“表达式 2 ”和“表达式 3 ”中有所区别?
代码
#include<iostream>
int main()
{
int a=3;
a++=5;
std::cout<<a;
}
Run Code Online (Sandbox Code Playgroud)
输出(如预期)
[Error] lvalue required as left operand of assignment
Run Code Online (Sandbox Code Playgroud)
1.后增量运算符 ( a++) 在表中具有最高优先级。所以它肯定会在赋值运算符 ( =)之前执行。并且根据后增量规则,变量的值a只有在执行该语句后才会增加。
那么当后增量运算符 ( ++) 在赋值运算符 ( =)之前执行时究竟会发生什么?
2. 在C 中,前自增运算符和后自增运算符都产生右值,但C++将前自增运算符更新为左值,同时将后自增运算符仅保留为右值。原因是我们不能让它成为左值,因为它只有旧值,而不是更新的值。但我不能正确理解这个原因。
现在a++看到右值是 3,而不是变量本身,对吧?但是如果它带来一个拥有左值的变量,那么 5 将插入其中,并且在语句结束后它的值将是 6。这有什么问题,为什么不能这样做?
代码
#include <iostream>
namespace myspace
{
int x;
}
myspace::x=3; // This line is giving error.
int main()
{
myspace::x=5;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
Error: C++ requires a type specifier for all declarations
Run Code Online (Sandbox Code Playgroud)
那么为什么会出现myspace::x=3;C++ 要求所有声明都有类型说明符的错误呢?
首先,这不是重复的,因为之前问过同样的错误问题,但在不同的上下文中
代码
#include<iostream>
#include<cstring>
int main()
{
const char *s1;
const char *s2="bonapart";
s1=new char[strlen(s2)+1];
strcpy(s1,s2);
std::cout<<s1;
}
Run Code Online (Sandbox Code Playgroud)
输出
[Error] invalid conversion from 'const char*' to 'char*' [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
为什么会出现这样的错误?
如果我取代const char *s1由char *s1 编译罚款。但我认为这const只是说 s1 指向常量字符串意味着我们不能修改它所指向的字符串。这并不意味着它s1是常数 ptr。请对此有所了解。
代码 1
#include <iostream>
int main()
{
std::cout << nullptr;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
Error: Use of overloaded operator '<<' is ambiguous (with operand types 'std::ostream' (aka 'basic_ostream<char>') and 'nullptr_t')
Run Code Online (Sandbox Code Playgroud)
甚至有特定类型nullptr显示错误的原因。但
代码 2
#include <iostream>
int main()
{
std::cout << (void*)nullptr;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
0
Run Code Online (Sandbox Code Playgroud)
工作正常。为什么void*即使它不是类型也能工作?
代码
#include <iostream>
class A
{
public:
mutable int x;
mutable int y;
A(int k1 = 0, int k2 = 0) :x(k1), y(k2) {}
void display()
{
std::cout << x << "," << y << "\n";
}
};
int main()
{
const A a1;
a1.x = 3;
a1.y = 8;
a1.display();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
Error: 'this' argument to member function 'display'
has type 'const A', but function is not marked const
Run Code Online (Sandbox Code Playgroud)
我只是A::display()通过const限定对象调用成员函数a1。那么为什么 line …