表达式必须具有整数或枚举类型
Expression must have integral or enum type
好的,所以我正在尝试做一个简单的程序,读取2个输入文件(名称和等级),然后显示并将它们打印到输出文件中。到目前为止,我有这个:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
void ReadNames();
void ReadGrades();
void ReadNames()
{
char names [15][5];
ifstream myfile("names.txt");
if(myfile.is_open())
{
while(!myfile.eof())
{
for (int i = 0; i < 11; i++)
{
myfile.get(names[i],15,' ');
cout << names[i];
}
}
cout << endl;
}
else cout << "Error loadng file!" << endl;
}
void ReadGrades()
{
char grades [15][5];
ifstream myfile2("grades.txt");
if(myfile2.is_open())
{
while(!myfile2.eof())
{
for (int k = 0; k < 11; k++)
{
myfile2.get(grades[k],15,' ');
cout << grades[k];
}
}
cout << endl;
}
else cout << "Error loadng file!" << endl;
}
int main()
{
char Name [10];
int grade [10][10];
ReadNames();
ReadGrades();
for (int i = 0;i < 5; i++)
{
cout << Name[i];
for ( int j = 0; j < 5; j++)
grade [i][j] << " ";
cout << endl;
}
cout << endl;
system("pause");
return 0;
}
当我尝试编译Visual Studio时,给了我两个错误:
非法,右操作数的类型为"常量字符 [1]"
运算符没有效果;预期的运算符有副作用
我知道这很简单,但我不知道问题是什么。错误似乎源于grade [i][j] << " ";
行。任何帮助将不胜感激。
错误告诉您需要类似的东西
std::cout << grade [i][j] << " ";
grade [i][j]
是char
," "
是const char[1]
,并且没有operator<<
在这样的RHS和LHS组合上运行。
您正在尝试输出 grade[i][j]
的值,但未使用 std::cout
。尝试如下:
std::cout << grade [i][j] << " ";
<<
是左移运算符。由于它不是为 char 定义的(例如 grade[i][j]
),因此会出现错误。
相关文章:
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 如何注册枚举类型到QJSEngine,以便在脚本环境中使用?
- 通过包装器从 C 访问C++ API 时,如何访问枚举类型
- C++/错误:表达式必须具有整数或无作用域枚举类型
- 如何理解c++中在命名空间内部定义的枚举类型enum
- 在类访问混淆中声明的枚举类型
- 在枚举类型上使用std::max是不是一种糟糕的做法
- C++ 在方程中使用变量;错误:表达式必须具有整数或无作用域枚举类型及其他
- 是否可以使用泛型枚举类型作为函数的参数?
- Visual Studio 2017 生成工具与枚举类型的问题
- 使用枚举类型管理存储顺序
- 如何检查类中定义的枚举类型变量是否被分配给?
- 如何从枚举类型中获取枚举值?
- 为什么C++不为枚举类型提供默认"operator>>"函数?
- 确保枚举类型的键/值在软件Revs之间永远不会更改
- MSVC 1 位枚举类型等于 -1,并且相等性测试失败
- 我们可以专业化类模板的枚举(类型)成员吗?
- 错误:以增量方式填充向量时,表达式必须具有整数或无作用域枚举类型
- 枚举类型的重载>>运算符(工作日)
- C 中的枚举类型字符串类别