【问题标题】:Indirection requires pointer operand间接需要指针操作数
【发布时间】:2014-10-31 00:42:40
【问题描述】:

我收到了这个错误,Indirection requires pointer operand ('int invalid') in this code,我想我在这段代码中使用了错误的 empPtr,但我不确定。

提前谢谢各位。

我还将在此链接中包含我的其他课程。 https://gist.github.com/anonymous/08ff6c5284c179c9a323

我的输入文本文件如下所示。

123,John,Brown,125 Prarie Street,Staunton,IL,62088
124,Matt,Larson,126 Hudson Road,Edwardsville,IL,62025
125,Joe,Baratta,1542 Elizabeth Road,Highland,IL,62088
126,Kristin,Killebrew,123 Prewitt Drive,Alton,IL,62026
127,Tyrone,Meyer,street,999 Orchard Lane,Livingston,62088

这是我的 main.cpp。

#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"

using namespace std;

bool openFileForReading(ifstream& fin, const string& filename);
bool openFileForWriting(ofstream& fout, const string& filename);

int readFromFile(ifstream& in, Employee empArray[]);

void writeToFile(ofstream& out, const Employee empArray[], const int numberofEmployees);

int main() {

    ifstream fin;
    ofstream fout;

    if(!openFileForReading(fin, "employeesIn.txt")) {
        cerr << "Error opening employeesIn.txt for reading." << endl;
        exit(1);
    }

    if(!openFileForWriting(fout, "employeesOut.txt")) {
        cerr << "Error opeing employeesOut.txt for writing." << endl;
        exit(1);
    }

    Employee employeeArray[50];

    int employeeCount = readFromFile(fin, employeeArray);

    fin.close();

    writeToFile(fout, employeeArray, employeeCount);

    fout.close();

    cout << "Program successful." << endl << endl;


    return 0;
}

bool openFileForReading(ifstream& fin, const string& filename) {
    fin.open("employeesIn.txt");

    return (fin.is_open());
}

bool openFileForWriting(ofstream& fout, const string& filename) {
    fout.open("employeesOut.txt");

    return (fout.is_open());
}

int readFromFile(ifstream& in, Employee empArray[]) {
    int temp = 0;
    string eidText;
    string first;
    string last;
    string street;
    string city;
    string state;
    string zipcode;

    while(!in.eof()) {
        getline(in, eidText, ',');
        getline(in, first, ',');
        getline(in, last, ',');
        getline(in, street, ',');
        getline(in, city, ',');
        getline(in, state, ',');
        getline(in, zipcode);

        empArray[temp].setEid(stoi(eidText));
        empArray[temp].setName(first, last);
        empArray[temp].setAddress(street, city, state, zipcode);

        temp++;
    }

    return temp;
}

void writeToFile(ofstream& out, const Employee empArray[], const int numberOfEmployees) {

    for (int i = 0; i < numberOfEmployees; i++){
        out << "Employee Record: " << empArray[i].getEid()
        << endl
        << "Name: " << empArray[i].getName()
        << endl
        << "Home Address: " << empArray[i].getAddress() 
        << endl
        << endl;
    }
}

【问题讨论】:

  • 那是我认为我做对了的时候:/

标签: c++ file-io outputstream pointers


【解决方案1】:

你有

int empPtr = 0;

紧随其后

(*empPtr).setEid(stoi(eidText));

这显然是导致此错误的原因。

【讨论】:

  • 您会将 int empPtr 更改为什么?另外,你觉得我的数组声明怎么样?
  • 首先,您从未使用过Employee empPtr = employeeArray[50]; 的值,所以我不确定该行的用途是什么。尽管您正在访问超出数组末尾的索引 ([50]),但这是一个运行时错误,不会导致编译器错误。其次empPtr 不是指针,它只是Employee 类型的堆栈变量。我不确定你在readFromFile 中要做什么,但你不能取消引用非指针类型,在这种情况下int,这就是empPtr 在该方法中的作用。
  • 本来不想用empPtr的,后来被别人引用了,我把上面的代码改回了empPtr之前的样子。我的输出文件打开了,但没有写入任何内容。
  • 那么您仍然收到错误消息吗?我再也看不到empPtr了。
  • 我已删除 empPtr,正在寻找其他选项为什么我的文件输出无法正常工作,它会打开文件但由于某些奇怪的原因没有写入。
【解决方案2】:

这一行是错误的。

Employee empPtr = employeeArray[50];

employeeArray 的最大有效索引为 49

要获取数组的第一项,请使用:

Employee empPtr = employeeArray[0];

要获取数组的最后一项,请使用:

Employee empPtr = employeeArray[49];

有关访问 C 和 C++ 数组的更多信息,请访问 http://www.augustcouncil.com/~tgibson/tutorial/arr.htmlhttp://www.tutorialspoint.com/cprogramming/c_arrays.htm

【讨论】:

  • 我应该把它改成什么?
  • 我已将其更改为 [49] 版本,但我的 (*empPtr).setEid(stoi(eidText)); 中仍然出现错误;
  • @MatthewTingle,正如 mclaassen 正确指出的那样,它修复了运行时错误而不是编译时错误。 mclaassen 的回答解决了编译时错误。
  • 我已经从我的代码中完全删除了empPtr,我们还没有学习指针,所以我不会使用它们。我的输出文件打开,但没有写入任何内容。
  • @MatthewTingle,我看到了你更新的代码。您是否看到更多编译器错误?
猜你喜欢
  • 2015-06-02
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 2023-04-02
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
相关资源
最近更新 更多