I'm new to c++ and still trying to grasp class implementation with constructors etc.
I have a program split into 3 files,a header file, a class implementation file, and driver file.
In the header file I get an error "explicit type is missing('int' assumed) for this line of code friend operator<<(ostream &, ARRAY &);
In my class implementation file i get errors for this friend function saying i don't have access to the private members.
And in my driver file I get an error "can't determine which instance overloaded function 'endl' intended" for this code : cout<
I'll leave some of the code below,first is the .h file, then the class implementation file, and then the driver file. Any help clearing this up is much appreciated.
Header
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class ARRAY
{
public:
ARRAY();
ARRAY(int );
ARRAY(const ARRAY &);
~ARRAY();
friend operator<<(ostream &, ARRAY &);
private:
string *DB;
int count;
int capacity;
};
Implementation file
#include "array.h"
ARRAY::ARRAY()
{
cout<<"Default Constructor has been called\n";
count = 0;
capacity = 2;
DB = new string[capacity];
}
ARRAY::ARRAY(int no_of_cells)
{
cout<<"Explicit-Value Constructor has been called\n";
count = 0;
capacity = no_of_cells;
DB = new string[capacity];
}
ARRAY::ARRAY(const ARRAY & Original)
{
cout<<"The copy constructor has been invoked\n";
count = Original.count;
capacity = Original.capacity;
DB = new string[capacity];
for(int i=0; i<count; i++)
{
DB[i] =Original.DB[i];
}
}
inline ARRAY::~ARRAY()
{
cout<<"Destructor Called\n";
delete [ ] DB;
}
ostream & operator<<(ostream & out, ARRAY & Original)
{
for(int i=0; i< Original.count; i++)
{
out<<"DB[" << i <<"] = "<< Original.DB[i]<<endl;
}
return out;
}
Driver file
#include <iostream>
#include <string>
#include "array.h"
using namespace std;
int main()
{
cout<<"invoking the default constructor (11)"<<endl;
ARRAY myArray;
cout<<"Output after default constructor called\n";
cout<<myArray<<endl<<endl;
cout<<"invoking the explicit-value constructor (12)"<<endl;
ARRAY yourArray(5);
cout<<"Output after explicit-value constructor called\n";
//cout<<yourArray<<endl<<endl;
cout<<"invoking the copy constructor (3)"<<endl;
ARRAY ourArray = myArray;
cout<<"Output after copyconstructor called\n";
cout<<ourArray<<endl<<endl;
return 0;
}
您放弃了返回类型:
friend ostream& operator<<(ostream &, ARRAY &);
as Carl Norum mentioned in his solution
You left off the return type:
friend ostream& operator<<(ostream &, ARRAY &);
also you have remove inline
from
inline ARRAY::~ARRAY()
{
cout<<"Destructor Called\n";
delete [ ] DB;
}
to be
ARRAY::~ARRAY()
{
cout<<"Destructor Called\n";
delete [ ] DB;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.