【问题标题】:Expression must have integral or enum type表达式必须具有整数或枚举类型
【发布时间】:2012-09-18 05:29:28
【问题描述】:

好的,所以我正在尝试做一个简单的程序来读取 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,'\0');
            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,'\0');
            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 时出现两个错误:

非法,右操作数的类型为'const char [1]'

操作符无效;具有副作用的预期运算符

我知道这很简单,但我不知道问题出在哪里。该错误似乎源于grade [i][j] &lt;&lt; " "; 行。任何帮助将不胜感激。

【问题讨论】:

    标签: c++ visual-c++


    【解决方案1】:

    错误告诉你需要类似的东西

    std::cout << grade [i][j] << " ";
    

    grade [i][j]char" "const char[1],并且没有operator&lt;&lt; 可以在这样的 RHS 和 LHS 组合上运行。

    【讨论】:

      【解决方案2】:

      您正在尝试输出grade[i][j] 的值,但您没有使用std::cout。试试这样:

      std::cout << grade [i][j] << " ";
      

      &lt;&lt;left shift operator。由于没有为 char 定义(如grade[i][j]),所以会出现错误。

      【讨论】:

        猜你喜欢
        • 2013-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多