如何修复表达式必须具有对象类型 C++
how to fix expression must have object type c++
我的编码或数据类型有问题。错误显示"表达式必须具有指向对象的指针类型"。我不知道如何解决这个问题。
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
int id, year, dates;
cout << "Enter the ID number ";
cin >> id;
year = id[0] + id[1] + 1900;
cout << year;
getch();
return 0;
}
拜托,有人知道你的解决方案吗?
您可以做一些简单的数学运算来访问 ID 的各个数字:
#include <iostream>
using namespace std;
int main(void)
{
int id = 189;
cout << id / 100 % 10 << endl;
cout << id / 10 % 10 << endl;
cout << id / 1 % 10 << endl;
return 0;
}
耶尔兹:189
你的id
是一个整数,但你把它当作一个数组使用:
id[0]+id[1]
您将id
声明为 int
,但随后尝试在其上使用 []
运算符。 编译器对此感到"困惑",并且正在尽最大努力弄清楚您在int
上使用运算符的含义 - 它能做的最好的事情是告诉您表达式必须是指向对象的指针(换句话说,它告诉您它不能在 int
类型上使用运算符 - 而是指针或对象)。
相关文章:
- 在 C++ 中将一个模板类型的对象类型转换为另一个模板类型
- 了解 Python 中的对象类型
- 具有纯虚函数和指针数组对象类型的父类的指针数组
- 调用的对象类型 'double' 不是 report() 函数的函数或函数指针
- 基准、值、值类型、对象和对象类型(C++)
- JNI 如何将 Java 对象数组传递给相同对象类型的 C++ 数组
- 在构造过程中更改的对象类型
- 如何构建程序以避免查询对象类型?
- 如何使用Dynamic_cast获得对象类型
- 什么更有效率?在重载函数中或通过在基类函数中检查对象类型来实现
- static_cast实际上不是对象类型的类型是未定义的行为吗?
- 为什么 std::variant 不能容纳数组对象类型,而联合可以?
- 为什么类型转换对象不会更改其地址?有关对象类型的信息存储在哪里?
- 类返回对象类型,但如何返回和检查 null
- 是否可以在辅助功能中概括对象类型
- 表达式必须在C 中具有指针对象类型
- 下标需要数组或指针类型表达式必须具有指针对象类型
- 当将对象传递给具有参数作为引用类型的函数以及当其类对象类型时,会得到不同的输出
- 如何在运行时指定对象类型
- 我可以获取在重载的新运算符中使用新运算符的对象类型吗?