c++ - 智能感知 : expression must have class type (prefix postfix error)

标签 c++

所以我有一个包含两个数字的区间类,我想使用++ 前缀和后缀,但我收到错误:1 IntelliSense:表达式必须具有类类型

代码如下:

class intervallum
{
    int a;
    int b;
public:
    intervallum();
    intervallum(int x,int y);
    intervallum operator++();
    void operator++(int);
    void kiir();
};

intervallum intervallum::operator++()
{
    a -= 1;
    b += 1;
    return intervallum(a,b);
}

void intervallum::operator++(int)
{ 
    operator++();
}

void intervallum::kiir()
{
    cout << "[" << a << "," << b << "]" << endl;
}

void main()
{
    intervallum i(2,4);
    i.kiir();
    (++i).kiir();
    i.kiir();
    (i++).kiir; // <- the error is with this
    i.kiir();
}

最佳答案

看一下表达式

(i++).kiir;

首先应该是(i++).kiir(); , 正确的?即便如此,intervallum::operator++(int)返回 void ,而您正在尝试调用 kiirvoid对象(如果存在这样的东西)。不好。

有几点需要注意:

  • 如评论中所建议,intervallum::operator++()应该返回对正在操作的对象的引用,否则你最终会使用一个拷贝。
  • 你应该真的告诉编译器 main返回 int ,即使你没有。

关于c++ - 智能感知 : expression must have class type (prefix postfix error),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22995224/

相关文章:

c++ - 在恒定时间内追加两个容器

c++ - 在 Boost Spirit 中解析嵌套键值对

c++ - 如何编写用于rapidjson反序列化的嵌套处理程序?

c++ - QGraphicsScene 子类忽略鼠标按下事件

c++如何对位集 vector 进行排序?

模板函数参数的 C++17 invoke_result 和 static_assert

c++ - 如何将 SendMessage() 发送到在另一个线程上创建的窗口?

c++ - C++ 友元声明

c++ - 如果只使用没有单独头文件的 cpp 文件有什么缺点吗?

c++ - 如何对 vector 中的字符串进行模式匹配?