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 {
}