Trouble with compilation
Jun 27, 2008 at 3:14pm UTC
Hi,
I'm new to c++ and am currently working through AcceleratedCPP. I'm having difficulty compiling my code and get the following compiler message:
1 2 3 4
student_info.cpp(26) : error C2065: 'name' : undeclared identifier
student_info.cpp(26) : error C2065: 'midterm' : undeclared identifier
student_info.cpp(26) : error C2065: 'final' : undeclared identifier
student_info.cpp(27) : error C2065: 'homework' : undeclared identifier
This is my first attempt at writing classes so I'm unfamiliar with how classes are laid out, but my header file is defined as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#ifndef STUDENT_INFO_H
#define STUDENT_INFO_H
#include <string>
#include <vector>
class Student_info {
private :
std::string name;
double midterm, final;
std::vector<double > homework;
public :
Student_info();
Student_info(std::istream&);
double Student_info::grade() const ;
std::istream& read(std::istream&);
std::string get_name() const {
return name;
}
bool valid() const {
return !homework.empty();
}
};
bool compare(const Student_info&, const Student_info&);
std::istream& read_hw(std::istream&, std::vector<double >);
#endif
and the corresponding source file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <vector>
#include <iostream>
#include "Student_info.h"
#include "grade.h"
using std::vector;
using std::istream;
Student_info::Student_info(): midterm(0), final(0)
{
}
Student_info::Student_info(istream& in)
{
read(in);
}
double Student_info::grade() const
{
return ::grade(midterm, final, homework);
}
istream& read(istream& in)
{
in >> name >> midterm >> final;
read_hw(in, homework);
return in;
}
bool compare(const Student_info& x, const Student_info& y)
{
return x.get_name() > y.get_name();
}
istream& read_hw(istream& in, vector<double >& hw)
{
if (in) {
hw.clear();
double x;
while (in >> x) {
hw.push_back(x);
}
in.clear();
}
return in;
}
This code follows the text, so I'm somewhat baffled as to why it won't compile.
I'd appreciate any help!
Thanks.
George.
Jun 27, 2008 at 4:42pm UTC
Ok, I fixed the above by declaring
istream& Student_info::read(istream& in)
only to get another compilation error:
Student_info.obj : error LNK2019: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl read_hw(class std::basic_istream<char,struct std::char_traits<char> > &,class std::vector<double,class std::allocator<double> >)" (?read_hw@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV12@V?$vector@NV?$allocator@N@std@@@2@@Z) referenced in function "public: class std::basic_istream<char,struct std::char_traits<char> > & __thiscall Student_info::read(class std::basic_istream<char,struct std::char_traits<char> > &)" (?read@Student_info@@QAEAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV23@@Z)
I'm finding it hard to pinpoint the problem.
Thanks.
George.
Jun 27, 2008 at 5:24pm UTC
From the looks of the grs201's error message would say that you would need to do the same thing to the other function read_hw
istream& Student_info::read_hw(istream& in, vector<double >& hw)
Just a thought...haven't had time to compile it myself.
Jun 28, 2008 at 6:12am UTC
Ok, managed to fix the problem by removing the declaration of read_hw from the header and repositioning the read_hw definition before the call to read.
Topic archived. No new replies allowed.