I'm getting an error that says: this function was not declared in this scope error with g++ compiler.
** Error message at the end of the question.**
I'm trying to make a separete header file for my simple program. For this I created a new class called mread
and kept it in the header file. Being new to c++ and after extensive searches online, I figured out how to correctly configure header files. But on my way to learning I might have missed some importand concepts.
So here's my entire code:
main.cpp
#include"mread.h"
using namespace std;
//function prototypes
int main(int argc,char* argv[])
{
//if invalid command format used.
if(argc!=2)
{
cout<<"Please enter the correct command format: "<<"\n";
cout<<argv[0]<<" <filename>"<<"\n";
return 1;
}
//initialize all required variables
string mbox = argv[1];
ifstream reader;
reader.open(mbox.c_str()); // stream the file to the ifstream 'reader'
if(!reader) // check if the file does exist
{
cerr<<"Sorry,the file:"<< mbox<<" is not in this directory."<<"\n";
reader.close();
return 1;
}
list<string> data_container;
mread::read_messages(data_container,reader);
data_container.sort();
/*
sorting the contents of the the list using the linked list actually sorts
even the numeric strings without having to extract them as numbers
*/
mread::show_messages(data_container);
return 0;
}
mread.cpp
#include"mread.g"
// this function returns the message number in string format
string mread::get_pkt_num(string message)
{
string ch = message.substr(0,3); // extract the interpart from the message
return ch;
}
//this function decapsulates the message from the packets and returns it
string mread::extract_message(string message)
{
int len = message.length();
message = message.substr(8,len);
return message;
}
void mread::show_messages(list<string> &packet)
{
list<string>::iterator itr;
string pkt_num="0";
for(itr=packet.begin();itr!=packet.end();itr++) // iterate through the list
{
if(pkt_num=="0") //this checks for the first message
{
pkt_num = get_pkt_num(*itr);
cout<<"\n"<<"Message "<<pkt_num<<"\n";
}
// change a new line and move on to another line for a new message
if(get_pkt_num(*itr)!=pkt_num && pkt_num!="0")
{
pkt_num = get_pkt_num(*itr);
cout<<"\n"<<"Message "<<pkt_num<<"\n";
}
cout<<"\t"<<extract_message(*itr);
}
}
void mread::read_messages(list<string>&packet,ifstream& reader)
{
string temp;
char ch;
while(!reader.eof()) //iterate until the reader reaches the EOF.
{
reader.get(ch);
temp=temp+ch;
if(ch=='\n') //condition to check for end of each line
{
//store the string retrieved till now and set the temp to NULL
packet.push_back(temp);
s temp.clear();
}
/*
if end of message reached before reaching the EOF,then break the loop.
Everything after the END will be discarded even if there was a message.
*/
if(temp=="END")
{
break;
}
}
reader.close();
}
and finally, the mread.h file:
#ifndef _mread_
#define _mread_
#include<iostream>
#include<string>
#include<fstream> // library for reading and writing files
#include<list> // for using list data types
#include<stdlib.h>
using namespace std;
public class mread
{
void mread()
{
};
public string get_pkt_num(string);
public string extract_message(string);
public void show_messages(list<string> &);
public void read_messages(list<string>&,ifstream&);
};
#endif
I'm using g++ compiler and whenever I try to compile the code with
g++ main.cpp
, I get two errors:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:42: error: ‘read_messages’ was not declared in this scope
main.cpp:48: error: ‘show_messages’ was not declared in this scope
I'm new to c++. Please Help.
Aucun commentaire:
Enregistrer un commentaire