• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Need some help using structs and classes and *.h files URGENT

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Octoman

Member
Joined
Jul 26, 2001
Location
Houston, TX
Any help or pointers in the right direction would be appreciated.

Ok I can get this program to work if I put it all into 1 *.cpp file and compile it, it works perfect. But I am having trouble if I try to seperate it into a Proj1_prototype.cpp file and a Proj1_prototype.h file. The main problem I am havinf is that it does not seem to recognize the "string" types in my struct. Here are some of the errors I am getting.


--------------------Configuration: CS_Project - Win32 Debug--------------------
Compiling...
Proj1_prototype.cpp
F:\CS 2310\Proj1\Proj1_prototype.h(13) : error C2146: syntax error : missing ';' before identifier 'Name'
F:\CS 2310\Proj1\Proj1_prototype.h(13) : error C2501: 'string' : missing storage-class or type specifiers
F:\CS 2310\Proj1\Proj1_prototype.h(13) : error C2501: 'Name' : missing storage-class or type specifiers
F:\CS 2310\Proj1\Proj1_prototype.h(14) : error C2146: syntax error : missing ';' before identifier 'ssn'
F:\CS 2310\Proj1\Proj1_prototype.h(14) : error C2501: 'string' : missing storage-class or type specifiers
F:\CS 2310\Proj1\Proj1_prototype.h(14) : error C2501: 'ssn' : missing storage-class or type specifiers
F:\CS 2310\Proj1\Proj1_prototype.cpp(71) : error C2039: 'Name' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(75) : error C2039: 'ssn' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(108) : error C2039: 'Name' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(112) : error C2039: 'ssn' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(145) : error C2039: 'Name' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(149) : error C2039: 'ssn' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(182) : error C2039: 'Name' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(186) : error C2039: 'ssn' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(219) : error C2039: 'Name' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(223) : error C2039: 'ssn' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(302) : error C2039: 'Name' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(306) : error C2039: 'ssn' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(340) : error C2039: 'Name' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(344) : error C2039: 'ssn' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(378) : error C2039: 'Name' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(382) : error C2039: 'ssn' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(416) : error C2039: 'Name' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(420) : error C2039: 'ssn' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(505) : error C2039: 'Name' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.cpp(506) : error C2039: 'ssn' : is not a member of 'StudentRecord'
F:\CS 2310\Proj1\Proj1_prototype.h(12) : see declaration of 'StudentRecord'
Error executing cl.exe.

Proj1_prototype.obj - 26 error(s), 0 warning(s)


Here are the files

Proj1_prototype.cpp


// CS2310 Project 1 Implementation file
//*******************************************




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

using namespace std;



//***********************************************************************
//***********************************************************************


void StuRecords::InsertRecord(StudentRecord& newRecord)
{
data[length] = newRecord;
length++;
}


void StuRecords::printByGrades() const
{
// Postcondition:
// Print out student records by the letter grades, with the records
// of 'A' first, 'B' the second, ..., 'F' the last.

//********************************************
//************* Opens file for writing *******
//********************************************

ofstream fout;
fout.open ("f:/gfile.txt");

//********************************************
if ( fout.fail() )
{
cout<< "Output file opening failed.\n";
exit(1);
}
//********************************************

int i;
int j;

fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<"Name";

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<"Student ID";

fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Rank";
fout<<"Grade"<<endl;

for ( i = 0; i < 5; i++)
{
if (i == 0)
for (j = 0; j < length; j++)
{
if (data[j].letterGrade == A)
{
fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<data[j].Name;

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<data[j].ssn;

switch (data[j].rank)
{
case Freshman: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Freshman";
break;
case Sophomore: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Sophomore";
break;
case Junior: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Junior";
break;
case Senior: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Senior";
break;
}

fout<<'A'<<endl;
}
}

if ( i == 1)
for (j = 0; j < length; j++)
{
if (data[j].letterGrade == B)
{
fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<data[j].Name;

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<data[j].ssn;

switch (data[j].rank)
{
case Freshman: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Freshman";
break;
case Sophomore: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Sophomore";
break;
case Junior: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Junior";
break;
case Senior: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Senior";
break;
}

fout<<'B'<<endl;
}
}

if ( i ==2)
for (j = 0; j < length; j++)
{
if (data[j].letterGrade == C)
{
fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<data[j].Name;

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<data[j].ssn;

switch (data[j].rank)
{
case Freshman: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Freshman";
break;
case Sophomore: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Sophomore";
break;
case Junior: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Junior";
break;
case Senior: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Senior";
break;
}

fout<<'C'<<endl;
}
}

if (i == 3)
for (j = 0; j < length; j++)
{
if (data[j].letterGrade == D)
{
fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<data[j].Name;

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<data[j].ssn;

switch (data[j].rank)
{
case Freshman: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Freshman";
break;
case Sophomore: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Sophomore";
break;
case Junior: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Junior";
break;
case Senior: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Senior";
break;
}

fout<<'D'<<endl;
}
}

if (i == 4)
for (j = 0; j < length; j++)
{
if (data[j].letterGrade == F)
{
fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<data[j].Name;

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<data[j].ssn;

switch (data[j].rank)
{
case Freshman: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Freshman";
break;
case Sophomore: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Sophomore";
break;
case Junior: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Junior";
break;
case Senior: fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Senior";
break;
}

fout<<'F'<<endl;
}
}
}
fout<<endl;
fout.close();
}

void StuRecords::printByRanks() const
{
// Postcondition:
// Print out student records by their classification, with the records
// of "Freshman" first, "Junior" second, ..., Senior the last.

//********************************************
//******* Opens file for writing & appends ***
//********************************************

ofstream fout;
fout.open ("f:/gfile.txt", ios::app);

//********************************************
if ( fout.fail() )
{
cout<< "Output file opening failed.\n";
exit(1);
}
//********************************************



int i;
int j;

fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<"Name";

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<"Student ID";

fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Rank";
fout<<"Grade"<<endl;


for ( i = 0; i < 4; i++)
{
if (i == 0)
for (j = 0; j < length; j++)
{
if (data[j].rank == Freshman)
{
fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<data[j].Name;

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<data[j].ssn;

fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Freshman";

switch (data[j].letterGrade)
{
case A: fout.setf(ios::left);
fout<<'A'<<endl;
break;
case B: fout.setf(ios::left);
fout<<'B'<<endl;
break;
case C: fout.setf(ios::left);
fout<<'C'<<endl;
break;
case D: fout.setf(ios::left);
fout<<'D'<<endl;
break;
case F: fout.setf(ios::left);
fout<<'F'<<endl;
break;
}
}
}

if ( i == 1)
for (j = 0; j < length; j++)
{
if (data[j].rank == Sophomore)
{
fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<data[j].Name;

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<data[j].ssn;

fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Sophomore";

switch (data[j].letterGrade)
{
case A: fout.setf(ios::left);
fout<<'A'<<endl;
break;
case B: fout.setf(ios::left);
fout<<'B'<<endl;
break;
case C: fout.setf(ios::left);
fout<<'C'<<endl;
break;
case D: fout.setf(ios::left);
fout<<'D'<<endl;
break;
case F: fout.setf(ios::left);
fout<<'F'<<endl;
break;
}
}
}

if ( i ==2)
for (j = 0; j < length; j++)
{
if (data[j].rank == Junior)
{
fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<data[j].Name;

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<data[j].ssn;

fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Junior";

switch (data[j].letterGrade)
{
case A: fout.setf(ios::left);
fout<<'A'<<endl;
break;
case B: fout.setf(ios::left);
fout<<'B'<<endl;
break;
case C: fout.setf(ios::left);
fout<<'C'<<endl;
break;
case D: fout.setf(ios::left);
fout<<'D'<<endl;
break;
case F: fout.setf(ios::left);
fout<<'F'<<endl;
break;
}
}
}

if (i == 3)
for (j = 0; j < length; j++)
{
if (data[j].rank == Senior)
{
fout.width(LEN_SPACING_NAME);
fout.setf(ios::left);
fout<<data[j].Name;

fout.width(LEN_SPACING_SSN);
fout.setf(ios::left);
fout<<data[j].ssn;

fout.width(LEN_SPACING_RANK);
fout.setf(ios::left);
fout<<"Senior";

switch (data[j].letterGrade)
{
case A: fout.setf(ios::left);
fout<<'A'<<endl;
break;
case B: fout.setf(ios::left);
fout<<'B'<<endl;
break;
case C: fout.setf(ios::left);
fout<<'C'<<endl;
break;
case D: fout.setf(ios::left);
fout<<'D'<<endl;
break;
case F: fout.setf(ios::left);
fout<<'F'<<endl;
break;
}
}
}
}
fout.close();

}

StuRecords::StuRecords() {
// Default constructor: Zero the length of the array.
length = 0;
}
//***********************************************************************
//******************** Main Function ************************************
//***********************************************************************

int main() {

StudentRecord recordTemp;

StuRecords CurrentRecords;

int numberOfRecords;

int i;

int classRank;

string string1, string2, string3, string4;

ifstream fin;

ofstream fout;

//********************************************
//********* Opens file for reading ***********
//********************************************

fin.open ("stuflie.txt");

//********************************************
if ( fin.fail() )
{
cout<< "Input file opening failed.\n";
exit(1);
}
//********************************************




fin>>numberOfRecords;

//******************************************************************
//********************* Assignment of records **********************
//******************************************************************
if (numberOfRecords > 0)
for ( i = 0; i < numberOfRecords; i++)
{
fin>> string1;
fin>> string2;
string3 = string1 + ' ' + string2;
recordTemp.Name = string3;
fin>> recordTemp.ssn;
fin>> classRank;
fin>> recordTemp.grades[0] >>recordTemp.grades[1] >> recordTemp.grades[2];

recordTemp.average = (recordTemp.grades[0] + recordTemp.grades[1] + recordTemp.grades[2])/3.0;


switch (classRank)
{
case 1: recordTemp.rank = Freshman;
break;
case 2: recordTemp.rank = Sophomore;
break;
case 3: recordTemp.rank = Junior;
break;
case 4: recordTemp.rank = Senior;
break;
}

if (recordTemp.average >= 90)
recordTemp.letterGrade = A;
else if (recordTemp.average < 90 && recordTemp.average >= 80)
recordTemp.letterGrade = B;
else if (recordTemp.average < 80 && recordTemp.average >= 75)
recordTemp.letterGrade = C;
else if (recordTemp.average < 75 && recordTemp.average >= 70)
recordTemp.letterGrade = D;
else recordTemp.letterGrade = F;

CurrentRecords.InsertRecord(recordTemp);

}
//******************************************************************
//******************* Function Calls *******************************
//******************************************************************

CurrentRecords.PrintByGrades();
CurrentRecords.PrintByRanks();





fin.close();

return 0;

}



Proj1_prototype.h

// CS2310 Project 1 Specification file

const int MAX_NUM_STUDENTS = 15;
const int LEN_SPACING_NAME = 23;
const int LEN_SPACING_SSN = 14;
const int LEN_SPACING_RANK = 12;

enum letRank {Freshman, Sophomore, Junior, Senior};
enum letGrade {A, B, C, D, F};

struct StudentRecord
{
string Name;
string ssn;
letRank rank;
letGrade letterGrade;
int grades[3];
int average;
};


class StuRecords
{

public:
void InsertRecord(StudentRecord& newRecord);
// Precondition:
// A new student record is passed in by newRecord.
// Postcondition:
// The new record is inserted into the bottom of the record array.
// This function assume the array is not sorted.
void PrintByGrades() const;
// Postcondition:
// Print out student records by the letter grades, with the records
// of 'A' first, 'B' the second, ..., 'F' the last.
void PrintByRanks() const;
// Postcondition:
// Print out student records by their classification, with the records
// of "Freshman" first, "Junior" second, ..., Senior the last.
StuRecords();
// Default constructor: Zero the length of the array.

private:
int length; // Keep track the number of students.
StudentRecord data[MAX_NUM_STUDENTS];
};



Thanks!!!
 
Well, I got some syntax errors at the start as well. Chances are that once the syntax errors are corrected, the whole thing will work. -- Paul

*edit*
Okay, the syntax errors were caused by your long

Code:
// ***********************************

lines that tried to wrap strangely when copying to

Code:
// **************
******

After I fixed those and moved the namespace line above the

Code:
#include "blah.h"

line, it compiled just fine, although I got a warning:

In line 514 of the cpp file, you're assigning a double to an integer. I would change it to this:

Code:
recordTemp.average = (int) ( (recordTemp.grades[0] + recordTemp.grades[1] + recordTemp.grades[2])/3.0 );

-- Paul

*/edit*
 
Last edited:
What might be a better solution is to add the namespace scope to your types in your header file like below:
std::string

It's a little more typing, but it will save you a lot of headaches whith namespace resolution when you are using multiple files.
 
Thanks guys, I really appreciate the help. I tried both methods and both worked good for me. However, I think I will go with Shane's method. Just curious, can someone explain why I would have to use the "std::" for "string" and not for the others? Is it because "int" is a built in type and my use of enums comes right before the struct declaration?

Just in case anyone was wandering, I am taking CS 2310 at a university. Thus far the hardest part about the classes has been, realizing that there is only so much that the professors can teach us. There is so much involved in programming, that it really needs to be supplemented by additional sources. i.e. groups like this one and other books. Ya'll have really pulled me out of some tight situations! Thanks again!


P.S.
A really good book that I have found to be very good as a reference has been, "Sams Teach Yourself: C++ for Linux" It has a wealth of information! I got it at Fry's for $20 in the bargain book section. It has really made a good supplement to my class text book, it really would have made a better text book.
 
Yeah, Shane's method is definantly the better one. I've been stuck doing C programming for the last few years so I couldn't remember exactly how it was done.

You should also put #include <string> in the header, that way you don't have to remember it everytime you include it. It probally won't be a problem while you are still in school, but once you start working in a project with hundreds of files and a dozen other programmers the habbit can save you lots of headaches.

I believe int is in the global namespace. When you did your enums without a "using" declaration before them, they were put in the global namespace as well. That just means they can be accessed anywhere regardless of what the current namespace is.
 
int is a built-in type, carried over from C; string is a class and part of STL. These two types couldn't be more different.
 
Back