小编Abh*_*ane的帖子

为什么 `it1++` 有效,但 `it1=it1+1` 无效,其中 it1 是列表容器的迭代器

给定一个std::list名为 的迭代器itit1++相当于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)

c++ iterator stl list

11
推荐指数
1
解决办法
330
查看次数

三元运算符,如果我避免写“表达式 2”,它会起作用,但如果我不写“表达式 3”,则会出现错误

代码

#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 ”中有所区别?

c++ conditional-operator

9
推荐指数
1
解决办法
371
查看次数

为什么 ++(后增量运算符)不能是左值?

代码

#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。这有什么问题,为什么不能这样做?

c++ rvalue lvalue post-increment

4
推荐指数
1
解决办法
80
查看次数

namespace myspace { int x } 现在为什么 `myspace::x=3;` 给出错误?

代码

#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++ 要求所有声明都有类型说明符的错误呢?

c++ namespaces

3
推荐指数
1
解决办法
149
查看次数

错误:从“const char*”到“char*”的无效转换[-fpermissive]

首先,这不是重复的,因为之前问过同样的错误问题,但在不同的上下文中

1

代码

#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 *s1char *s1 编译罚款。但我认为这const只是说 s1 指向常量字符串意味着我们不能修改它所指向的字符串。这并不意味着它s1常数 ptr。请对此有所了解。

c++ constants syntax-error

1
推荐指数
1
解决办法
114
查看次数

`cout&lt;&lt;nullptr` 给出错误,尽管 `nullptr` 的类型来自 C++17

代码 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*即使它不是类型也能工作?

c++ void-pointers nullptr c++17

0
推荐指数
1
解决办法
62
查看次数

通过 const 限定对象调用成员函数会出现错误,因为函数未标记为 const?

代码

#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 …

c++ class constants mutable member-functions

0
推荐指数
1
解决办法
604
查看次数