Sign in to follow this  
prasannahariharan

Error while trying to compile two class which need to refer each others pointers

Recommended Posts

I am using Two class which need to refer each others pointers. When I include the Header of one class into the other class, it gives me the following error: Compiling... MainFile.cpp n:\rashmi\cse562\project2\datapage.h(30) : error C2061: syntax error : identifier 'Record' n:\rashmi\cse562\project2\datapage.h(52) : error C2143: syntax error : missing ';' before '*' n:\rashmi\cse562\project2\datapage.h(52) : error C2501: 'Record' : missing storage-class or type specifiers n:\rashmi\cse562\project2\datapage.h(52) : error C2501: 'get_First_Record' : missing storage-class or type specifiers n:\rashmi\cse562\project2\datapage.h(54) : warning C4183: 'get_First_Record': member function definition looks like a ctor, but name does not match enclosing class n:\rashmi\cse562\project2\datapage.h(56) : error C2143: syntax error : missing ';' before '*' n:\rashmi\cse562\project2\datapage.h(56) : error C2501: 'Record' : missing storage-class or type specifiers n:\rashmi\cse562\project2\datapage.h(56) : error C2501: 'restore_File' : missing storage-class or type specifiers n:\rashmi\cse562\project2\datapage.h(60) : warning C4183: 'restore_File': member function definition looks like a ctor, but name does not match enclosing class n:\rashmi\cse562\project2\datapage.h(97) : error C2143: syntax error : missing ';' before '*' n:\rashmi\cse562\project2\datapage.h(97) : error C2501: 'Record' : missing storage-class or type specifiers n:\rashmi\cse562\project2\datapage.h(97) : error C2501: 'start' : missing storage-class or type specifiers n:\rashmi\cse562\project2\datapage.h(98) : error C2143: syntax error : missing ';' before '*' n:\rashmi\cse562\project2\datapage.h(98) : error C2501: 'Record' : missing storage-class or type specifiers n:\rashmi\cse562\project2\datapage.h(98) : error C2501: 'current' : missing storage-class or type specifiers Here are my two classes: Record Class: #ifndef _RECORD_H_ #define _RECORD_H_ #pragma warning(disable:4786) #include <iostream> #include <string> #include <vector> #include "DataPage.h" //class DataPage; using namespace std; class Record { public: //constructor Record(vector< string> attr) { rid = 0; values = attr; next = NULL; } vector< string > parse_line(string s) { vector< string> attribs; //TODO - fill up the code return attribs; } void set_Next(Record *r) { // this record pointer has to be passed from the data page next = r; } Record* get_Next(){ return next; } int size() { return sizeof(this->rid) + 4*values.size(); } void set_RID(long id) { rid = id; } long get_RID() { return rid; } vector< string > get_Values() { return values; } private: long rid; vector< string > values; //pointer to the data variable Record *next; }; #endif DataPage Class: #ifndef _DATAPAGE_H_ #define _DATAPAGE_H_ #include <iostream> #include <fstream> #include "DirOfPages.h" #include "Record.h" //class Record; using namespace std; class DataPage { public: //constructor DataPage(string fileName) { ofstream outfile; const char *name = fileName.c_str(); outfile.open (name, ofstream::out | ofstream::app); outfile.close(); start = NULL; cout << "Inside the constructor" << endl; } int insert_Record(Record *r) { //TODO fill up the algo for inserting the record cout << "Inside DataPage: " << endl; if( start == NULL ) { cout << "Inside DataPage: " << endl; start = r; } else { Record *temp = start; while( temp->get_Next() != NULL ){ temp = temp->get_Next(); } temp->set_Next(r); r->set_Next(NULL); } //update the free_Bytes variable after inserting the record return 0; } Record* get_First_Record() { return start; } Record* restore_File(string fileName) { //TODO : fill up the code to construct the link list from the flat file return start; } void dump_File(string fileName) { //TODO : fill up the code to convert link list into file contents ofstream outfile; const char *name = fileName.c_str(); outfile.open (name, ofstream::out | ofstream::app); Record *temp = start; while( temp != NULL ) { cout << "Contents of the node: " << endl; outfile << temp->get_RID() << "~"; cout << "Attributes: " << endl; for (int i=0; i<temp->get_Values().size(); ++i) { outfile << temp->get_Values().at(i) << "~"; } temp = temp->get_Next(); outfile << endl; } outfile.close(); } void print_Node() { Record *temp = start; while( temp != NULL ) { cout << "Contents of the node: " << endl; cout << "RID: " << temp->get_RID() << endl; cout << "Attributes: " << endl; for (int i=0; i<temp->get_Values().size(); ++i) { cout << temp->get_Values().at(i) << endl; } temp = temp->get_Next(); } } private: Record *start; Record *current; }; #endif I need this to be done for a submission on Monday. Thank you so much:)

Share this post


Link to post
Share on other sites
It doesn't look like your Record class depends on your DataPage class at all, so why is it including the DataPage header?

If it did depend on the DataPage class, then you can resolve the situation by using forward declarations in the headers, and separating the implementation into source files. See this article, for a more detailed explanation.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this