我目前正在用c ++编写一个类的数组包装器,我不知道错误是什么意思。
这些是源文件和头文件
#include "List.h"
#include <cstdlib>
#include <stdexcept>
#include <iostream>
using namespace std;
List::List(int x){
if(x>10){
arrSize=x;
}else{
arrSize=10;
}
array = new int[arrSize];
}
List::List(List& list){
array = new int[list.size()];
arrSize = list.size();
for(int x=0;x<arrSize;x++){
array[x]=list[x];
}
}
List::~List(){
for(int x=0;x<arrSize;x++){
if(array[x]){
delete(&array[x]);
}
}
}
int List::size(){
return arrSize;
}
int& List::operator [](int& index){
if(index>arrSize-1){
throw out_of_range("Array Index Out of Bounds");
}else{
return array[index];
}
}
int& List::operator [](const int& index){
if(index>arrSize-1){
throw out_of_range("Array Index Out of Bounds");
}else{
return array[index];
}
}
List& List::operator +(List& list){
List *retList = new List(list.size()+arrSize);
for(int x=0;x<arrSize;x++){
(*retList)[x]=array[x];
}
for(int x=arrSize;x<list.size()+arrSize;x++){
(*retList)[x]=list[x];
}
return *retList;
}
void List::operator =(List& list){
delete(array);
arrSize=list.size();
array = new int[arrSize];
for(int x=0;x<arrSize;x++){
array[x]=list[x];
}
}
#ifndef LIST_H_
#define LIST_H_
#include <iostream>
using namespace std;
class List{
private:
int arrSize;
int *array;
public:
List(int x=10);
List(List&);
~List();
int size();
void operator=(List&);
List& operator+(List&);
int& operator[](const int&);
int& operator[](int&);
};
void operator << (ostream io,List& list){
io << "{";
for(int x=0;x<list.size()-1;x++){
io << list[x] + ",";
}
io << list[list.size()-1]+"}";
}
#endif /* LIST_H_ */
主要
#include "List.h"
#include <iostream>
using namespace std;
int main(){
List list;
for(int x=0;x<10;x++){
list[x]=x;
}
cout<<list;
return 0;
}
现在这是我得到的错误,我不理解
In file included from /usr/include/c+
+/4.8/ios:42:0,
from
/usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from List.h:10,
from
Main.cpp:8:
/usr/include/c++/4.8/bits/ios_base.h:
In copy constructor
‘std::basic_ios<char>::basic_ios(const
std::basic_ios<char>&)’:
/usr/include/c+
+/4.8/bits/ios_base.h:786:5: error:
‘std::ios_base::ios_base(const std::ios_base&)’
is private
ios_base(const ios_base&);
^
In
file included from /usr/include/c+
+/4.8/ios:44:0,
from
/usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from List.h:10,
from
Main.cpp:8:
/usr/include/c+
+/4.8/bits/basic_ios.h:66:11: error: within this
context
class basic_ios : public ios_base
^
In file included from /usr/include/c+
+/4.8/iostream:39:0,
from
List.h:10,
from Main.cpp:8:
/usr/include/c++/4.8/ostream: In copy constructor
‘std::basic_ostream<char>::basic_ostream(const
std::basic_ostream<char>&)’:
/usr/include/c+
+/4.8/ostream:58:11: note: synthesized method
‘std::basic_ios<char>::basic_ios(const
std::basic_ios<char>&)’ first required here
class basic_ostream : virtual public
basic_ios<_CharT, _Traits>
^
Main.cpp:
In function ‘int main()’:
Main.cpp:17:8: note:
synthesized method
‘std::basic_ostream<char>::basic_ostream(const
std::basic_ostream<char>&)’ first required here
cout<<list;
^
In file included from
Main.cpp:8:0:
List.h:29:6: error: initializing
argument 1 of ‘void operator<<(std::ostream,
List&)’
void operator << (ostream io,List& list)
{
^
答案 0 :(得分:0)
按以下方式编写操作符
ostream & operator << ( ostream &io, List& list ){
io << "{";
for(int x=0;x<list.size()-1;x++){
io << list[x] + ",";
}
io << list[list.size()-1]+"}";
return io;
}
类ostream没有公共拷贝构造函数。所以第一个参数必须有一个引用类型。
最好将第二个参数声明为常量引用
ostream & operator << ( ostream &io, const List& list ){
io << "{";
for(int x=0;x<list.size()-1;x++){
io << list[x] + ",";
}
io << list[list.size()-1]+"}";
return io;
}
考虑到当运算符的大小等于0时,此运算符有一个错误,即此语句
io << list[list.size()-1]+"}";
无效。