Declaration seems to succeed, but eclipse/mingw C++ is giving 'could not be resolved' error anyway

103 views Asked by At

I am following Accelerated C++ by Koenig and Moo to learn C++, using Eclipse as my IDE and the MinGW tool chain. Chapter 4 teaches about the struct concept using a pretty simple multi-file example program which emulates reading in a series of student grades and outputs the averages. The struct it defines is called Student_info. Here is Student_info.h:

#ifndef STUDENT_INFO_H_GUARD
#define STUDENT_INFO_H_GUARD

#include <iostream>
#include <string>
#include <vector>

struct Student_info {
    std::string name;
    double midterm, final;
    std::vector<double> homework;
};

bool compare(const Student_info&, const Student_info&);
std::istream& read(std::istream&, Student_info);
std::istream& read_hw(std::istream&, std::vector<double>&);

#endif /* STUDENT_INFO_H_GUARD */

There are no errors associated with compare, read or read_hw.

The problem I am having is that even though the simple variable declaration below seems to work,

#include "Student_info.h"

int main() {
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;
....
}

in the sense that the Eclipse code editor signals via i) its color coding that it has found the data type; ii) when I mouseover, it shows me the underlying Student_info definition in the hover box; and iii) the code associated with the variable "record" doesn't generate errors.

However, when I try to use the students vector within main(), I get 'could not be resolved' errors on the property names or 'invalid arguments' for functions which try to pass the variables. For example,

for(vector<Student_info>::size_type i=0; i!=students.size(); ++i) {
    cout << students[i].name
            << string(maxlen+1 - students[i].name.size(), ' ');
....
}

produces a "Method 'size' could not be resolved" error and a "Field 'name' could be resolved" error.

It's very odd to me, as I said, because mouseover of the vector students declaration at the beginning of main() produces a hover box with the correct struct definition, indicating the include file is being found and contains the intended definition with the 'name' property defined as a string.

I've been trying to solve this for a couple of days and have searched the web and this site, as well as going over all the code many times. The code seems to be exactly what is in the book and it's not too difficult to figure out what it is supposed to be doing. I haven't been able to find a similar error description here or elsewhere by Googling. These errors prevent the project from compiling and so I can't precede. I'm hoping someone can describe what might possibly cause such a thing.

Here's a shortened version of the file with main in it:

#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include "Student_info.h"

//say what standard library names we use
using std::cin;             using std::setprecision;
using std::cout;            using std::sort;
using std::domain_error;    using std::streamsize;
using std::endl;            using std::string;
using std::max;             using std::vector;


int main() { // begin main
    vector<Student_info> students;
    Student_info record;
    string::size_type maxlen = 0;

    //read and store all the records, and find the length
    // of the longest name
    while(read(cin, record)) {
        maxlen = max(maxlen, record.name.size());
        students.push_back(record);
    }

    //alphabetize the records
    sort(students.begin(), students.end(), compare);


    for(vector<Student_info>::size_type i=0; i!=students.size(); ++i) {
        // write the name, padded on the right to maxlen+1 chars
        cout << students[i].name
            << string(maxlen+1 - students[i].name.size(), ' ');
        //compute and write the grade
        cout << endl;
    }
    return 0;
} // end main

Here are the 'read' functions, which are in a separate file from main and don't seem to be producing any errors:

#include "Student_info.h"

using std::istream; using std::vector;

istream& read_hw(istream& in, vector<double>& hw) {
    if (in) {
        // get rid of previous contents
        hw.clear();

        // read homework grades
        double x;
        while (in >> x)
            hw.push_back(x);

        // clear the stream so that input will work for the
        // next student
        in.clear();
    }
    return in;
}

istream& read(istream& is, Student_info s) {
    // read and store the student's name and midterm and final exam
    is >> s.name >> s.midterm >> s.final;

    read_hw(is, s.homework);
    return is;
}

bool compare(const Student_info& x, const Student_info& y) {
        return x.name < y.name;
}
0

There are 0 answers