友元类
// "handlefile.h"
#include "baseFileFunction.h"
class handleFile
{
public:
class FileFunction;
handleFile();
~handleFile();
void retrievedOperatorFile();
void retrievedSourceFile();
void retrievedReservedFile();
void writeBackFile();
private:
FileFunction *operatorFile;
FileFunction *sourceFile;
FileFunction *reservedFile;
/*std::vector<std::string> *lineOfOperatorFile;
std::vector<std::string> *signalOperatorFile;
std::vector<std::string> *lineOfSourceFile;
std::vector<std::string> *signalSourceFile;
std::vector<std::string> *lineOfRetrievedFile;
std::vector<std::string> *signalRetrievedFle;*/
};
//"baseFileFunction.h"
#ifndef BASEFILE_H
#define BASEFILE_H
#include <fstream>
#include <vector>
#include <string>
#include <utility>
//#include "handlefile.h"
class FileFunction
{
friend class handleFile;
public:
FileFunction();
~FileFunction();
void retrievedFile();
void splitLineWord();
/*void writeBackLineFile();
void writeBackSignalFile();*/
// virtual void writeBackFile();
private:
std::vector<std::string> *lineWord;
std::vector<std::string> *signalWord;
std::vector<int> *lineNumber;
std::vector<int> *signalNumber;
typedef std::pair<std::vector<int> *,std::vector<std::string> *> location;
location *signalLocation;
location *lineLocation;
};
#endif
//handlefile.cpp
#include "handlefile.h"
#include "baseFileFunction.h"
handleFile::handleFile()
:operatorFile(NULL),
sourceFile(NULL),
reservedFile(NULL)
{
operatorFile=new FileFunction;
sourceFile=new FileFunction;
reservedFile=new FileFunction;
}
handleFile::~handleFile()
{
delete operatorFile;
delete sourceFile;
delete reservedFile;
}
void handleFile::retrievedSourceFile()
{
sourceFile->retrievedFile();
}
void handleFile::retrievedOperatorFile()
{
operatorFile->retrievedFile();
}
void handleFile::retrievedReservedFile()
{
reservedFile->retrievedFile();
}
void handleFile::writeBackFile()
{
using namespace std;
ofstream outfile;
}
///baseFileFunction.cpp
#include "baseFileFunction.h"
#include <iostream>
FileFunction::FileFunction()
:lineWord(NULL),signalWord(NULL),
lineLocation(NULL),signalLocation(NULL)
{
lineWord=new std::vector<std::string>;
signalWord=new std::vector<std::string>;
lineNumber=new std::vector<int>;
signalNumber=new std::vector<int>;
}
FileFunction::~FileFunction()
{
delete lineWord;
delete signalWord;
delete lineLocation;
delete signalLocation;
}
//void FileFunction::writeBackFile()
//{
//
//}
void FileFunction::retrievedFile()
{
using namespace std;
std::string filePath;
bool isSureOpenFile=false;
while(!isSureOpenFile)
{
std::cout<<"Please input file path:";
std::cin>>filePath;
if (filePath.length()==0)
std::cout<<"Can`t get file path,please input file path again."<<endl ;
else
{
std::cout<<"confirm this file path:";
string ch;
cin>>ch;
if ( ch!="No" && ch!="no" && ch!= "n" )
isSureOpenFile=true;
}
}
ifstream infile(filePath.c_str(),ios::in);
if (!infile)
{
cerr<<"Open file failed!!"<<endl;
exit(1);
}
string strTemp;
int LineNumber=1 ;
while (!infile.eof())
{
getline(infile,strTemp,'\n');
lineWord->push_back(strTemp);
lineNumber->push_back(LineNumber);
LineNumber++;
}
infile.close();
lineLocation->first=lineNumber;
lineLocation->second=lineWord;
}
void FileFunction::splitLineWord()
{
using namespace std;
vector<string>::iterator it=lineWord->begin(),
itEnd=lineWord->end();
vector<int>::iterator itLo=lineNumber->begin(),
itLoEnd=lineNumber->end();
string str;
string temp;
for ( ; it!=itEnd ;it++,itLo++ )
{
str=*it;
temp="";
int strLength=str.length();
for (int i=0;i<strLength;i++)
{
if ((str[i]<='9'&&str[i]>='0') ||
(str[i]<='z' &&str[i]>='a')|| (str[i]<='Z'&&str[i]>='A') )
{
temp+=str[i];
}
else
{
if (temp!="")
{
signalWord->push_back(temp);
signalNumber->push_back(*itLo);
temp="";
}
if (str[i]!='' && str[i]!=' ')
{
signalWord->push_back(temp);
signalNumber->push_back(*itLo);
}
}
}
if (temp!="")
{
signalWord->push_back(temp);
signalNumber->push_back(*itLo);
}
}
signalLocation->first=signalNumber;
signalLocation->second=signalWord;
}
//main.cpp
#include "baseFileFunction.h"
#include "handlefile.h"
#include <iostream>
using namespace std;
int main()
{
/*FileFunction basefile;
basefile.retrievedFile();*/
handleFile han;
han.retrievedSourceFile();
return 0;
}
错误:
1> main.cpp
1> handlefile.cpp
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(13): error C2512: 'handleFile::FileFunction' : no appropriate default constructor available
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(14): error C2512: 'handleFile::FileFunction' : no appropriate default constructor available
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(15): error C2512: 'handleFile::FileFunction' : no appropriate default constructor available
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(20): warning C4150: deletion of pointer to incomplete type 'handleFile::FileFunction'; no destructor called
1> d:\code\completecode\basefilefunction\basefilefunction\handlefile.h(10) : see declaration of 'handleFile::FileFunction'
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(21): warning C4150: deletion of pointer to incomplete type 'handleFile::FileFunction'; no destructor called
1> d:\code\completecode\basefilefunction\basefilefunction\handlefile.h(10) : see declaration of 'handleFile::FileFunction'
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(22): warning C4150: deletion of pointer to incomplete type 'handleFile::FileFunction'; no destructor called
1> d:\code\completecode\basefilefunction\basefilefunction\handlefile.h(10) : see declaration of 'handleFile::FileFunction'
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(27): error C2027: use of undefined type 'handleFile::FileFunction'
1> d:\code\completecode\basefilefunction\basefilefunction\handlefile.h(10) : see declaration of 'handleFile::FileFunction'
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(27): error C2227: left of '->retrievedFile' must point to class/struct/union/generic type
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(32): error C2027: use of undefined type 'handleFile::FileFunction'
1> d:\code\completecode\basefilefunction\basefilefunction\handlefile.h(10) : see declaration of 'handleFile::FileFunction'
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(32): error C2227: left of '->retrievedFile' must point to class/struct/union/generic type
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(37): error C2027: use of undefined type 'handleFile::FileFunction'
1> d:\code\completecode\basefilefunction\basefilefunction\handlefile.h(10) : see declaration of 'handleFile::FileFunction'
1>d:\code\completecode\basefilefunction\basefilefunction\handlefile.cpp(37): error C2227: left of '->retrievedFile' must point to class/struct/union/generic type
1> baseFileFunction.cpp
1> Generating Code...
1>
1>Build FAILED.
friend class handleFile;声明一个外部类是友元类