尝试实现运算符时出错<<?
Getting errors trying to implement an operator<<?
我是 c++ 的新手,仍在尝试使用构造函数等掌握类实现。
我有一个程序分成 3 个文件,一个头文件、一个类实现文件和驱动程序文件。
在头文件中,我收到一个错误"这行代码友元运算符缺少显式类型(假设为'int'<<(ostream &, ARRAY &);
在我的类实现文件中,我收到此友元函数的错误,说我无法访问私有成员。
在我的驱动程序文件中,我收到一个错误"无法确定哪个实例重载函数'endl'打算"对于此代码:cout<<p>我将在下面留下一些代码,首先是.h文件,然后是类实现文件,然后是驱动程序文件。任何帮助澄清这一点将不胜感激。
页眉
#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;
};
实现文件
#include "array.h"
ARRAY::ARRAY()
{
cout<<"Default Constructor has been calledn";
count = 0;
capacity = 2;
DB = new string[capacity];
}
ARRAY::ARRAY(int no_of_cells)
{
cout<<"Explicit-Value Constructor has been calledn";
count = 0;
capacity = no_of_cells;
DB = new string[capacity];
}
ARRAY::ARRAY(const ARRAY & Original)
{
cout<<"The copy constructor has been invokedn";
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 Calledn";
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;
}
驱动程序文件
#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 calledn";
cout<<myArray<<endl<<endl;
cout<<"invoking the explicit-value constructor (12)"<<endl;
ARRAY yourArray(5);
cout<<"Output after explicit-value constructor calledn";
//cout<<yourArray<<endl<<endl;
cout<<"invoking the copy constructor (3)"<<endl;
ARRAY ourArray = myArray;
cout<<"Output after copyconstructor calledn";
cout<<ourArray<<endl<<endl;
return 0;
}
您离开了返回类型:
friend ostream& operator<<(ostream &, ARRAY &);
正如Carl Norum在他的解决方案中提到的
You left off the return type:
friend ostream& operator<<(ostream &, ARRAY &);
您也从中删除了inline
inline ARRAY::~ARRAY()
{
cout<<"Destructor Calledn";
delete [ ] DB;
}
要成为
ARRAY::~ARRAY()
{
cout<<"Destructor Calledn";
delete [ ] DB;
}
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 呼叫运营商<<临时
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 为x86而非x64编译时出错
- <<操作员在下面的行中工作
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了