第 23 行是:
List[i] = x;
当我尝试编译时:
g++ w3.cpp list.cpp line.cpp
list.cpp: In member function void List::set(int):
list.cpp:23:8: error: expected unqualified-id before [ token
这是main.cpp:
#include <iostream>
using namespace std;
#include "list.h"
int main() {
int no;
List list;
cout << "List Processor\n==============" << endl;
cout << "Enter number of items : ";
cin >> no;
list.set(no);
list.display();
}
这是list.h:
#include "line.h"
#define MAX_LINES 10
using namespace std;
struct List{
private:
struct Line line[MAX_LINES];
public:
void set(int no);
void display() const;
};
这是line.h:
#define MAX_CHARS 10
struct Line {
private:
int num;
char numOfItem[MAX_CHARS + 1]; // the one is null byte
public:
bool set(int n, const char* str);
void display() const;
};
这是 list.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "list.h"
//#include "line.h" - commented this line because it was giving me a struct redefinition error
void List::set(int no) {
int line;
char input[20];
if (no > MAX_LINES)
no = MAX_LINES;
for (int i = 0; i < no; i++) {
Line x;
cout << "Enter line number : ";
cin >> line;
cout << "Enter line string : ";
cin >> input;
if (x.set(line, input)){
List[i] = x;
cout << "Accepted" << endl;
}
else
cout << "Rejected" << endl;
}
}
void List::display() const {
}