错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
ERROR: expected unqualified-id before '[' and ERROR: 'users' was not declared in this scope
我试图制作一个程序,您可以在其中写入登录名和密码,并且,如果用户存在于类中,它会显示权限(例如Carl可以读取和执行文件(。现在我有两个错误:
22:9: error: expected unqualified-id before '[' token
36:23: error: 'users' was not declared in this scope
我是 c++ 的新手,所以我不确定我该怎么办。谁能帮忙?
#include <iostream>
using namespace std;
int main()
{
class User {
public:
string login;
string password;
bool R;
bool W;
bool X;
User() = default;
User(string login, string password, bool r, bool w, bool x);
};
//setlocale(LC_ALL, "Russian");
string login, password;
string User[] users = new User[]
{
new User("Alice", "Cooper", false, true, true),
new User("Bob", "Dylan", false, true, false),
new User("Carl", "Perkins", true, false, true)
};
cout << "nEnter login: " << endl;
cin >> login;
cout << "nEnter password: " << endl;
cin >> password;
bool userFound = false;
User user = users[i];
for (int i=0; i < users.Length; i++)
{
if (user.login == login && user.password == password)
{
userFound = true;
cout << "nCan read: " << user.R;
cout << "nCan write: " << user.W;
cout << "nCan execute: " << user.X;
break;
}
}
if (!userFound)
{
cout << "nUser not found" << endl;
}
}
string User[] users = new User[]
{
new User("Alice", "Cooper", false, true, true),
new User("Bob", "Dylan", false, true, false),
new User("Carl", "Perkins", true, false, true)
};
这C++无效(看起来您以某种方式混合了C++和 Java 语法(。首先,您在声明中意外地string
(并且您没有声明string
- 您需要#include <string>
!其次,C 数组声明如下:
Type var[N];
第三,你不能用指针初始化 C 数组(从new
返回(。
第四,new Type[]
需要数组大小。
第五,你不能用指针初始化一个User
数组(new User
创建一个User*
(。
所有这些问题都可以通过使用std::vector
来解决:
std::vector<User> users{
User("Alice", "Cooper", false, true, true),
User("Bob", "Dylan", false, true, false),
User("Carl", "Perkins", true, false, true)
};
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- Atom gpp编译器给出"'C:UsersadminUser' is not recognized as an internal or..."错误